1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package javax.print;
19
20 import java.io.IOException;
21 import java.io.ObjectInputStream;
22 import java.io.ObjectOutputStream;
23 import java.io.Serializable;
24 import java.security.AccessController;
25 import java.security.PrivilegedAction;
26 import org.apache.harmony.x.print.MimeType;
27
28 /**
29 * Encapsulates an object that specifies the format to a DocPrintJob print
30 * data.
31 * @author Irina A. Arkhipets
32 */
33 public class DocFlavor implements Serializable, Cloneable {
34 private static final long serialVersionUID = -4512080796965449721L;
35
36 public static final String hostEncoding;
37 static {
38 /*
39 * Host encoding. Need to change it to "hostEncoding =
40 * Charset.defaultCharset().toString"
41 */
42 hostEncoding = AccessController.doPrivileged(new PrivilegedAction<String>() {
43 public String run() {
44 return System.getProperty("file.encoding");
45 }
46 });
47 }
48
49 // Document media type (as defined in RFC 2045,2046, 822)
50 private transient MimeType aType;
51
52 private final String aClass; // Representation class name
53
54 public DocFlavor(String mimeType, String className) {
55 if ((mimeType == null) || (className == null)) {
56 throw new NullPointerException();
57 }
58 aType = new MimeType(mimeType);
59 aClass = className;
60 }
61
62 @Override
63 public boolean equals(Object obj) {
64 return (obj != null) && (obj instanceof DocFlavor)
65 && (getCanonicalForm().equals(((DocFlavor) obj).getCanonicalForm()));
66 }
67
68 public String getMediaSubtype() {
69 return aType.getSubtype();
70 }
71
72 public String getMediaType() {
73 return aType.getType();
74 }
75
76 public String getMimeType() {
77 return aType.toString();
78 }
79
80 public String getParameter(String paramName) {
81 if (paramName == null) {
82 throw new NullPointerException();
83 }
84 return aType.getParameter(paramName);
85 }
86
87 public String getRepresentationClassName() {
88 return aClass;
89 }
90
91 @Override
92 public int hashCode() {
93 return getCanonicalForm().hashCode();
94 }
95
96 @Override
97 public String toString() {
98 return getCanonicalForm();
99 }
100
101 // --------------------------------------------------------------
102 private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
103 s.defaultReadObject();
104 aType = new MimeType((String) (s.readObject()));
105 }
106
107 private void writeObject(ObjectOutputStream s) throws IOException {
108 s.defaultWriteObject();
109 s.writeObject(aType.getCanonicalForm());
110 }
111
112 private String getCanonicalForm() {
113 return aType.toString() + "; class=\"" + aClass + "\"";
114 }
115
116 // --------------------------------------------------------------
117 public static class BYTE_ARRAY extends DocFlavor {
118 private static final long serialVersionUID = -9065578006593857475L;
119
120 public BYTE_ARRAY(String mimeType) {
121 super(mimeType, "[B");
122 }
123
124 public static final BYTE_ARRAY AUTOSENSE = new BYTE_ARRAY("application/octet-stream");
125
126 public static final BYTE_ARRAY GIF = new BYTE_ARRAY("image/gif");
127
128 public static final BYTE_ARRAY JPEG = new BYTE_ARRAY("image/jpeg");
129
130 public static final BYTE_ARRAY PCL = new BYTE_ARRAY("application/vnd.hp-pcl");
131
132 public static final BYTE_ARRAY PDF = new BYTE_ARRAY("application/pdf");
133
134 public static final BYTE_ARRAY PNG = new BYTE_ARRAY("image/png");
135
136 public static final BYTE_ARRAY POSTSCRIPT = new BYTE_ARRAY("application/postscript");
137
138 public static final BYTE_ARRAY TEXT_HTML_HOST = new BYTE_ARRAY("text/html;charset="
139 + hostEncoding);
140
141 public static final BYTE_ARRAY TEXT_HTML_US_ASCII = new BYTE_ARRAY(
142 "text/html;charset=us-ascii");
143
144 public static final BYTE_ARRAY TEXT_HTML_UTF_16 = new BYTE_ARRAY(
145 "text/html;charset=utf-16");
146
147 public static final BYTE_ARRAY TEXT_HTML_UTF_16BE = new BYTE_ARRAY(
148 "text/html;charset=utf-16be");
149
150 public static final BYTE_ARRAY TEXT_HTML_UTF_16LE = new BYTE_ARRAY(
151 "text/html;charset=utf-16le");
152
153 public static final BYTE_ARRAY TEXT_HTML_UTF_8 = new BYTE_ARRAY(
154 "text/html;charset=utf-8");
155
156 public static final BYTE_ARRAY TEXT_PLAIN_HOST = new BYTE_ARRAY("text/plain;charset="
157 + hostEncoding);
158
159 public static final BYTE_ARRAY TEXT_PLAIN_US_ASCII = new BYTE_ARRAY(
160 "text/plain;charset=us-ascii");
161
162 public static final BYTE_ARRAY TEXT_PLAIN_UTF_16 = new BYTE_ARRAY(
163 "text/plain;charset=utf-16");
164
165 public static final BYTE_ARRAY TEXT_PLAIN_UTF_16BE = new BYTE_ARRAY(
166 "text/plain;charset=utf-16be");
167
168 public static final BYTE_ARRAY TEXT_PLAIN_UTF_16LE = new BYTE_ARRAY(
169 "text/plain;charset=utf-16le");
170
171 public static final BYTE_ARRAY TEXT_PLAIN_UTF_8 = new BYTE_ARRAY(
172 "text/plain;charset=utf-8");
173 }
174
175 public static class CHAR_ARRAY extends DocFlavor {
176 private static final long serialVersionUID = -8720590903724405128L;
177
178 public CHAR_ARRAY(String mimeType) {
179 super(mimeType, "[C");
180 }
181
182 public static final CHAR_ARRAY TEXT_HTML = new CHAR_ARRAY("text/html;charset=utf-16");
183
184 public static final CHAR_ARRAY TEXT_PLAIN = new CHAR_ARRAY("text/plain;charset=utf-16");
185 }
186
187 public static class INPUT_STREAM extends DocFlavor {
188 private static final long serialVersionUID = -7045842700749194127L;
189
190 public INPUT_STREAM(String mimeType) {
191 super(mimeType, "java.io.InputStream");
192 }
193
194 public static final INPUT_STREAM AUTOSENSE = new INPUT_STREAM(
195 "application/octet-stream");
196
197 public static final INPUT_STREAM GIF = new INPUT_STREAM("image/gif");
198
199 public static final INPUT_STREAM JPEG = new INPUT_STREAM("image/jpeg");
200
201 public static final INPUT_STREAM PCL = new INPUT_STREAM("application/vnd.hp-pcl");
202
203 public static final INPUT_STREAM PDF = new INPUT_STREAM("application/pdf");
204
205 public static final INPUT_STREAM PNG = new INPUT_STREAM("image/png");
206
207 public static final INPUT_STREAM POSTSCRIPT = new INPUT_STREAM("application/postscript");
208
209 public static final INPUT_STREAM TEXT_HTML_HOST = new INPUT_STREAM("text/html;charset="
210 + hostEncoding);
211
212 public static final INPUT_STREAM TEXT_HTML_US_ASCII = new INPUT_STREAM(
213 "text/html;charset=us-ascii");
214
215 public static final INPUT_STREAM TEXT_HTML_UTF_16 = new INPUT_STREAM(
216 "text/html;charset=utf-16");
217
218 public static final INPUT_STREAM TEXT_HTML_UTF_16BE = new INPUT_STREAM(
219 "text/html;charset=utf-16be");
220
221 public static final INPUT_STREAM TEXT_HTML_UTF_16LE = new INPUT_STREAM(
222 "text/html;charset=utf-16le");
223
224 public static final INPUT_STREAM TEXT_HTML_UTF_8 = new INPUT_STREAM(
225 "text/html;charset=utf-8");
226
227 public static final INPUT_STREAM TEXT_PLAIN_HOST = new INPUT_STREAM(
228 "text/plain;charset=" + hostEncoding);
229
230 public static final INPUT_STREAM TEXT_PLAIN_US_ASCII = new INPUT_STREAM(
231 "text/plain;charset=us-ascii");
232
233 public static final INPUT_STREAM TEXT_PLAIN_UTF_16 = new INPUT_STREAM(
234 "text/plain;charset=utf-16");
235
236 public static final INPUT_STREAM TEXT_PLAIN_UTF_16BE = new INPUT_STREAM(
237 "text/plain;charset=utf-16be");
238
239 public static final INPUT_STREAM TEXT_PLAIN_UTF_16LE = new INPUT_STREAM(
240 "text/plain;charset=utf-16le");
241
242 public static final INPUT_STREAM TEXT_PLAIN_UTF_8 = new INPUT_STREAM(
243 "text/plain;charset=utf-8");
244 }
245
246 public static class READER extends DocFlavor {
247 private static final long serialVersionUID = 7100295812579351567L;
248
249 public READER(String mimeType) {
250 super(mimeType, "java.io.Reader");
251 }
252
253 public static final READER TEXT_HTML = new READER("text/html;charset=utf-16");
254
255 public static final READER TEXT_PLAIN = new READER("text/plain;charset=utf-16");
256 }
257
258 public static class SERVICE_FORMATTED extends DocFlavor {
259 private static final long serialVersionUID = 6181337766266637256L;
260
261 public SERVICE_FORMATTED(String className) {
262 super("application/x-java-jvm-local-objectref", className);
263 }
264
265 public static final SERVICE_FORMATTED PAGEABLE = new SERVICE_FORMATTED(
266 "java.awt.print.Pageable");
267
268 public static final SERVICE_FORMATTED PRINTABLE = new SERVICE_FORMATTED(
269 "java.awt.print.Printable");
270
271 public static final SERVICE_FORMATTED RENDERABLE_IMAGE = new SERVICE_FORMATTED(
272 "java.awt.image.renderable.RenderableImage");
273 }
274
275 public static class STRING extends DocFlavor {
276 private static final long serialVersionUID = 4414407504887034035L;
277
278 public STRING(String mimeType) {
279 super(mimeType, "java.lang.String");
280 }
281
282 public static final STRING TEXT_HTML = new STRING("text/html;charset=utf-16");
283
284 public static final STRING TEXT_PLAIN = new STRING("text/plain;charset=utf-16");
285 }
286
287 public static class URL extends DocFlavor {
288 private static final long serialVersionUID = 2936725788144902062L;
289
290 public URL(String mimeType) {
291 super(mimeType, "java.net.URL");
292 }
293
294 public static final URL AUTOSENSE = new URL("application/octet-stream");
295
296 public static final URL GIF = new URL("image/gif");
297
298 public static final URL JPEG = new URL("image/jpeg");
299
300 public static final URL PCL = new URL("application/vnd.hp-pcl");
301
302 public static final URL PDF = new URL("application/pdf");
303
304 public static final URL PNG = new URL("image/png");
305
306 public static final URL POSTSCRIPT = new URL("application/postscript");
307
308 public static final URL TEXT_HTML_HOST = new URL("text/html;charset=" + hostEncoding);
309
310 public static final URL TEXT_HTML_US_ASCII = new URL("text/html;charset=us-ascii");
311
312 public static final URL TEXT_HTML_UTF_16 = new URL("text/html;charset=utf-16");
313
314 public static final URL TEXT_HTML_UTF_16BE = new URL("text/html;charset=utf-16be");
315
316 public static final URL TEXT_HTML_UTF_16LE = new URL("text/html;charset=utf-16le");
317
318 public static final URL TEXT_HTML_UTF_8 = new URL("text/html;charset=utf-8");
319
320 public static final URL TEXT_PLAIN_HOST = new URL("text/plain;charset=" + hostEncoding);
321
322 public static final URL TEXT_PLAIN_US_ASCII = new URL("text/plain;charset=us-ascii");
323
324 public static final URL TEXT_PLAIN_UTF_16 = new URL("text/plain;charset=utf-16");
325
326 public static final URL TEXT_PLAIN_UTF_16BE = new URL("text/plain;charset=utf-16be");
327
328 public static final URL TEXT_PLAIN_UTF_16LE = new URL("text/plain;charset=utf-16le");
329
330 public static final URL TEXT_PLAIN_UTF_8 = new URL("text/plain;charset=utf-8");
331 }
332 } /* End of DocFlavor class */