001 /* SimpleDoc.java --
002 Copyright (C) 2006 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038
039 package javax.print;
040
041 import java.io.ByteArrayInputStream;
042 import java.io.CharArrayReader;
043 import java.io.IOException;
044 import java.io.InputStream;
045 import java.io.Reader;
046 import java.io.StringReader;
047
048 import javax.print.attribute.AttributeSetUtilities;
049 import javax.print.attribute.DocAttributeSet;
050
051 /**
052 * Simple implementation of the <code>Doc</code> interface capable of handling
053 * the predefined document flavors of <code>DocFlavor</code>.
054 * <p>
055 * This implementation can construct a reader or stream for the service from
056 * the print data and ensures that always the same object is returned on each
057 * method call. It does simple checks that the supplied data matches the
058 * specified flavor of the doc object and supports thread safe access.
059 * </p>
060 *
061 * @author Wolfgang Baer (WBaer@gmx.de)
062 */
063 public final class SimpleDoc implements Doc
064 {
065 private final Object printData;
066 private final DocFlavor flavor;
067 private final DocAttributeSet attributes;
068
069 private InputStream stream;
070 private Reader reader;
071
072 /**
073 * Constructs a SimpleDoc with the specified print data, doc flavor and doc attribute set.
074 * @param printData the object with the data to print.
075 * @param flavor the document flavor of the print data.
076 * @param attributes the attributes of the doc (may be <code>null</code>).
077 *
078 * @throws IllegalArgumentException if either <code>printData</code> or
079 * <code>flavor</code> are <code>null</code>, or the print data is not
080 * supplied in the document format specified by the given flavor object.
081 */
082 public SimpleDoc(Object printData, DocFlavor flavor,
083 DocAttributeSet attributes)
084 {
085 if (printData == null || flavor == null)
086 throw new IllegalArgumentException("printData/flavor may not be null");
087
088 if (! (printData.getClass().getName().equals(
089 flavor.getRepresentationClassName())
090 || flavor.getRepresentationClassName().equals("java.io.Reader")
091 && printData instanceof Reader
092 || flavor.getRepresentationClassName().equals("java.io.InputStream")
093 && printData instanceof InputStream))
094 {
095 throw new IllegalArgumentException("data is not of declared flavor type");
096 }
097
098 this.printData = printData;
099 this.flavor = flavor;
100
101 if (attributes != null)
102 this.attributes = AttributeSetUtilities.unmodifiableView(attributes);
103 else
104 this.attributes = null;
105
106 stream = null;
107 reader = null;
108 }
109
110 /**
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>
119 *
120 * @return The unmodifiable attributes set, or <code>null</code>.
121 */
122 public DocAttributeSet getAttributes()
123 {
124 return attributes;
125 }
126
127 /**
128 * Returns the flavor of this doc objects print data.
129 *
130 * @return The document flavor.
131 */
132 public DocFlavor getDocFlavor()
133 {
134 return flavor;
135 }
136
137 /**
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>
144 *
145 * @return The print data in the representation class.
146 * @throws IOException if representation class is a stream and I/O
147 * exception occures.
148 */
149 public Object getPrintData() throws IOException
150 {
151 return printData;
152 }
153
154 /**
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>
166 *
167 * @return The <code>Reader</code> object, or <code>null</code>.
168 *
169 * @throws IOException if an error occurs.
170 */
171 public Reader getReaderForText() throws IOException
172 {
173 synchronized (this)
174 {
175 // construct the reader if applicable on request
176 if (reader == null)
177 {
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;
184 }
185
186 return reader;
187 }
188 }
189
190 /**
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>
201 *
202 * @return The <code>InputStream</code> object, or <code>null</code>.
203 *
204 * @throws IOException if an error occurs.
205 */
206 public InputStream getStreamForBytes() throws IOException
207 {
208 synchronized (this)
209 {
210 // construct the stream if applicable on request
211 if (stream == null)
212 {
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;
217 }
218
219 return stream;
220 }
221 }
222
223 }