KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > print > SimpleDoc


1 /*
2  * @(#)SimpleDoc.java 1.5 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.print;
9
10 import java.io.ByteArrayInputStream JavaDoc;
11 import java.io.CharArrayReader JavaDoc;
12 import java.io.StringReader JavaDoc;
13 import java.io.InputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.Reader JavaDoc;
16 import javax.print.attribute.AttributeSetUtilities JavaDoc;
17 import javax.print.attribute.DocAttributeSet JavaDoc;
18
19 /**
20  * This class is an implementation of interface <code>Doc</code> that can
21  * be used in many common printing requests.
22  * It can handle all of the presently defined "pre-defined" doc flavors
23  * defined as static variables in the DocFlavor class.
24  * <p>
25  * In particular this class implements certain required semantics of the
26  * Doc specification as follows:
27  * <ul>
28  * <li>constructs a stream for the service if requested and appropriate.
29  * <li>ensures the same object is returned for each call on a method.
30  * <li>ensures multiple threads can access the Doc
31  * <li>performs some validation of that the data matches the doc flavor.
32  * </ul>
33  * Clients who want to re-use the doc object in other jobs,
34  * or need a MultiDoc will not want to use this class.
35  * <p>
36  * If the print data is a stream, or a print job requests data as a
37  * stream, then <code>SimpleDoc</code> does not monitor if the service
38  * properly closes the stream after data transfer completion or job
39  * termination.
40  * Clients may prefer to use provide their own implementation of doc that
41  * adds a listener to monitor job completion and to validate that
42  * resources such as streams are freed (ie closed).
43  */

44
45 public final class SimpleDoc implements Doc JavaDoc {
46
47     private DocFlavor JavaDoc flavor;
48     private DocAttributeSet JavaDoc attributes;
49     private Object JavaDoc printData;
50     private Reader JavaDoc reader;
51     private InputStream JavaDoc inStream;
52
53     /**
54      * Constructs a <code>SimpleDoc</code> with the specified
55      * print data, doc flavor and doc attribute set.
56      * @param printData the print data object
57      * @param flavor the <code>DocFlavor</code> object
58      * @param attributes a <code>DocAttributeSet</code>, which can
59      * be <code>null</code>
60      * @throws IllegalArgumentException if <code>flavor</code> or
61      * <code>printData</code> is <code>null</code>, or the
62      * <code>printData</code> does not correspond
63      * to the specified doc flavor--for example, the data is
64      * not of the type specified as the representation in the
65      * <code>DocFlavor</code>.
66      */

67     public SimpleDoc(Object JavaDoc printData,
68                      DocFlavor JavaDoc flavor, DocAttributeSet JavaDoc attributes) {
69
70        if (flavor == null || printData == null) {
71            throw new IllegalArgumentException JavaDoc("null argument(s)");
72        }
73
74        Class JavaDoc repClass = null;
75        try {
76             repClass = Class.forName(flavor.getRepresentationClassName());
77        } catch (Throwable JavaDoc e) {
78            throw new IllegalArgumentException JavaDoc("unknown representation class");
79        }
80   
81        if (!repClass.isInstance(printData)) {
82            throw new IllegalArgumentException JavaDoc("data is not of declared type");
83        }
84
85        this.flavor = flavor;
86        if (attributes != null) {
87            this.attributes = AttributeSetUtilities.unmodifiableView(attributes);
88        }
89        this.printData = printData;
90     }
91
92    /**
93      * Determines the doc flavor in which this doc object will supply its
94      * piece of print data.
95      *
96      * @return Doc flavor.
97      */

98     public DocFlavor JavaDoc getDocFlavor() {
99         return flavor;
100     }
101
102     /**
103      * Obtains the set of printing attributes for this doc object. If the
104      * returned attribute set includes an instance of a particular attribute
105      * <I>X,</I> the printer must use that attribute value for this doc,
106      * overriding any value of attribute <I>X</I> in the job's attribute set.
107      * If the returned attribute set does not include an instance
108      * of a particular attribute <I>X</I> or if null is returned, the printer
109      * must consult the job's attribute set to obtain the value for
110      * attribute <I>X,</I> and if not found there, the printer must use an
111      * implementation-dependent default value. The returned attribute set is
112      * unmodifiable.
113      *
114      * @return Unmodifiable set of printing attributes for this doc, or null
115      * to obtain all attribute values from the job's attribute
116      * set.
117      */

118     public DocAttributeSet JavaDoc getAttributes() {
119         return attributes;
120     }
121
122     /*
123      * Obtains the print data representation object that contains this doc
124      * object's piece of print data in the format corresponding to the
125      * supported doc flavor.
126      * The <CODE>getPrintData()</CODE> method returns an instance of
127      * the representation class whose name is given by
128      * {@link DocFlavor#getRepresentationClassName() getRepresentationClassName},
129      * and the return value can be cast
130      * from class Object to that representation class.
131      *
132      * @return Print data representation object.
133      *
134      * @exception IOException if the representation class is a stream and
135      * there was an I/O error while constructing the stream.
136      */

137     public Object JavaDoc getPrintData() throws IOException JavaDoc {
138         return printData;
139     }
140
141     /**
142      * Obtains a reader for extracting character print data from this doc.
143      * The <code>Doc</code> implementation is required to support this
144      * method if the <code>DocFlavor</code> has one of the following print
145      * data representation classes, and return <code>null</code>
146      * otherwise:
147      * <UL>
148      * <LI> <code>char[]</code>
149      * <LI> <code>java.lang.String</code>
150      * <LI> <code>java.io.Reader</code>
151      * </UL>
152      * The doc's print data representation object is used to construct and
153      * return a <code>Reader</code> for reading the print data as a stream
154      * of characters from the print data representation object.
155      * However, if the print data representation object is itself a
156      * <code>Reader</code> then the print data representation object is
157      * simply returned.
158      * <P>
159      * @return a <code>Reader</code> for reading the print data
160      * characters from this doc.
161      * If a reader cannot be provided because this doc does not meet
162      * the criteria stated above, <code>null</code> is returned.
163      *
164      * @exception IOException if there was an I/O error while creating
165      * the reader.
166      */

167     public Reader JavaDoc getReaderForText() throws IOException JavaDoc {
168     
169         if (printData instanceof Reader JavaDoc) {
170             return (Reader JavaDoc)printData;
171         }
172
173         synchronized (this) {
174             if (reader != null) {
175                 return reader;
176             }
177
178             if (printData instanceof char[]) {
179                reader = new CharArrayReader JavaDoc((char[])printData);
180             }
181             else if (printData instanceof String JavaDoc) {
182                 reader = new StringReader JavaDoc((String JavaDoc)printData);
183             }
184         }
185         return reader;
186     }
187
188     /**
189      * Obtains an input stream for extracting byte print data from
190      * this doc.
191      * The <code>Doc</code> implementation is required to support this
192      * method if the <code>DocFlavor</code> has one of the following print
193      * data representation classes; otherwise this method
194      * returns <code>null</code>:
195      * <UL>
196      * <LI> <code>byte[]</code>
197      * <LI> <code>java.io.InputStream</code>
198      * </UL>
199      * The doc's print data representation object is obtained. Then, an
200      * input stream for reading the print data
201      * from the print data representation object as a stream of bytes is
202      * created and returned.
203      * However, if the print data representation object is itself an
204      * input stream then the print data representation object is simply
205      * returned.
206      * <P>
207      * @return an <code>InputStream</code> for reading the print data
208      * bytes from this doc. If an input stream cannot be
209      * provided because this doc does not meet
210      * the criteria stated above, <code>null</code> is returned.
211      *
212      * @exception IOException
213      * if there was an I/O error while creating the input stream.
214      */

215     public InputStream JavaDoc getStreamForBytes() throws IOException JavaDoc {
216       
217         if (printData instanceof InputStream JavaDoc) {
218             return (InputStream JavaDoc)printData;
219         }
220
221         synchronized (this) {
222             if (inStream != null) {
223                 return inStream;
224             }
225
226             if (printData instanceof byte[]) {
227                inStream = new ByteArrayInputStream JavaDoc((byte[])printData);
228             }
229         }
230         return inStream;
231     }
232
233 }
234
Popular Tags