Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / javax / print / SimpleDoc.java
bloba49406bcb767e286045d74de437f614978241e58
1 /* SimpleDoc.java --
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.print;
41 import java.io.ByteArrayInputStream;
42 import java.io.CharArrayReader;
43 import java.io.IOException;
44 import java.io.InputStream;
45 import java.io.Reader;
46 import java.io.StringReader;
48 import javax.print.attribute.AttributeSetUtilities;
49 import javax.print.attribute.DocAttributeSet;
51 /**
52 * Simple implementation of the <code>Doc</code> interface capable of handling
53 * the predefined document flavors of <code>DocFlavor</code>.
54 * <p>
55 * This implementation can construct a reader or stream for the service from
56 * the print data and ensures that always the same object is returned on each
57 * method call. It does simple checks that the supplied data matches the
58 * specified flavor of the doc object and supports thread safe access.
59 * </p>
61 * @author Wolfgang Baer (WBaer@gmx.de)
63 public final class SimpleDoc implements Doc
65 private final Object printData;
66 private final DocFlavor flavor;
67 private final DocAttributeSet attributes;
69 private InputStream stream;
70 private Reader reader;
72 /**
73 * Constructs a SimpleDoc with the specified print data, doc flavor and doc attribute set.
74 * @param printData the object with the data to print.
75 * @param flavor the document flavor of the print data.
76 * @param attributes the attributes of the doc (may be <code>null</code>).
78 * @throws IllegalArgumentException if either <code>printData</code> or
79 * <code>flavor</code> are <code>null</code>, or the print data is not
80 * supplied in the document format specified by the given flavor object.
82 public SimpleDoc(Object printData, DocFlavor flavor,
83 DocAttributeSet attributes)
85 if (printData == null || flavor == null)
86 throw new IllegalArgumentException("printData/flavor may not be null");
88 if (! (printData.getClass().getName().equals(
89 flavor.getRepresentationClassName())
90 || flavor.getRepresentationClassName().equals("java.io.Reader")
91 && printData instanceof Reader
92 || flavor.getRepresentationClassName().equals("java.io.InputStream")
93 && printData instanceof InputStream))
95 throw new IllegalArgumentException("data is not of declared flavor type");
98 this.printData = printData;
99 this.flavor = flavor;
101 if (attributes != null)
102 this.attributes = AttributeSetUtilities.unmodifiableView(attributes);
103 else
104 this.attributes = null;
106 stream = null;
107 reader = null;
111 * Returns the unmodifiable view of the attributes of this doc object.
112 * <p>
113 * The attributes of this doc's attributes set overrides attributes of
114 * the same category in the print job's attribute set. If an attribute
115 * is not available in this doc's attributes set or <code>null</code>
116 * is returned the attributes of the same category of the print job are
117 * used.
118 * </p>
120 * @return The unmodifiable attributes set, or <code>null</code>.
122 public DocAttributeSet getAttributes()
124 return attributes;
128 * Returns the flavor of this doc objects print data.
130 * @return The document flavor.
132 public DocFlavor getDocFlavor()
134 return flavor;
138 * Returns the print data of this doc object.
139 * <p>
140 * The returned object is an instance as described by the associated
141 * document flavor ({@link DocFlavor#getRepresentationClassName()})
142 * and can be cast to this representation class.
143 * </p>
145 * @return The print data in the representation class.
146 * @throws IOException if representation class is a stream and I/O
147 * exception occures.
149 public Object getPrintData() throws IOException
151 return printData;
155 * Returns a <code>Reader</code> object for extracting character print data
156 * from this document.
157 * <p>
158 * This method is supported if the document flavor is of type:
159 * <ul>
160 * <li><code>char[]</code></li>
161 * <li><code>java.lang.String</code></li>
162 * <li><code>java.io.Reader</code></li>
163 * </ul>
164 * otherwise this method returns <code>null</code>.
165 * </p>
167 * @return The <code>Reader</code> object, or <code>null</code>.
169 * @throws IOException if an error occurs.
171 public Reader getReaderForText() throws IOException
173 synchronized (this)
175 // construct the reader if applicable on request
176 if (reader == null)
178 if (flavor instanceof DocFlavor.CHAR_ARRAY)
179 reader = new CharArrayReader((char[]) printData);
180 else if (flavor instanceof DocFlavor.STRING)
181 reader = new StringReader((String) printData);
182 else if (flavor instanceof DocFlavor.READER)
183 reader = (Reader) printData;
186 return reader;
191 * Returns an <code>InputStream</code> object for extracting byte print data
192 * from this document.
193 * <p>
194 * This method is supported if the document flavor is of type:
195 * <ul>
196 * <li><code>byte[]</code></li>
197 * <li><code>java.io.InputStream</code></li>
198 * </ul>
199 * otherwise this method returns <code>null</code>.
200 * </p>
202 * @return The <code>InputStream</code> object, or <code>null</code>.
204 * @throws IOException if an error occurs.
206 public InputStream getStreamForBytes() throws IOException
208 synchronized (this)
210 // construct the stream if applicable on request
211 if (stream == null)
213 if (flavor instanceof DocFlavor.BYTE_ARRAY)
214 stream = new ByteArrayInputStream((byte[]) printData);
215 else if (flavor instanceof DocFlavor.INPUT_STREAM)
216 stream = (InputStream) printData;
219 return stream;