Source code: javax/print/DocFlavor.java
1 /* DocFlavor.java --
2 Copyright (C) 2004 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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.
10
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.
15
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.
20
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.
25
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. */
37
38
39 package javax.print;
40
41 import java.io.Serializable;
42 import java.util.HashMap;
43 import java.util.Iterator;
44 import java.util.Map;
45
46 /**
47 * @author Michael Koch (konqueror@gmx.de)
48 */
49 public class DocFlavor implements Cloneable, Serializable
50 {
51 /**
52 * @author Michael Koch (konqueror@gmx.de)
53 */
54 public static class BYTE_ARRAY
55 extends DocFlavor
56 {
57 public static final BYTE_ARRAY AUTOSENSE = new BYTE_ARRAY("application/octet-stream");
58 public static final BYTE_ARRAY GIF = new BYTE_ARRAY("image/gif");
59 public static final BYTE_ARRAY JPEG = new BYTE_ARRAY("image/jpeg");
60 public static final BYTE_ARRAY PCL = new BYTE_ARRAY("application/vnd.hp-PCL");
61 public static final BYTE_ARRAY PDF = new BYTE_ARRAY("application/pdf");
62 public static final BYTE_ARRAY PNG = new BYTE_ARRAY("image/png");
63 public static final BYTE_ARRAY POSTSCRIPT = new BYTE_ARRAY("application/postscript");
64 public static final BYTE_ARRAY TEXT_HTML_HOST = new BYTE_ARRAY("text/html");
65 public static final BYTE_ARRAY TEXT_HTML_US_ASCII = new BYTE_ARRAY("text/html; charset=us-ascii");
66 public static final BYTE_ARRAY TEXT_HTML_UTF_16 = new BYTE_ARRAY("text/html; charset=utf-16");
67 public static final BYTE_ARRAY TEXT_HTML_UTF_16BE = new BYTE_ARRAY("text/html; charset=utf-16be");
68 public static final BYTE_ARRAY TEXT_HTML_UTF_16LE = new BYTE_ARRAY("text/html; charset=utf-16le");
69 public static final BYTE_ARRAY TEXT_HTML_UTF_8 = new BYTE_ARRAY("text/html; charset=utf-8");
70 public static final BYTE_ARRAY TEXT_PLAIN_HOST = new BYTE_ARRAY("text/plain");
71 public static final BYTE_ARRAY TEXT_PLAIN_US_ASCII = new BYTE_ARRAY("text/plain; charset=us-ascii");
72 public static final BYTE_ARRAY TEXT_PLAIN_UTF_16 = new BYTE_ARRAY("text/plain; charset=utf-16");
73 public static final BYTE_ARRAY TEXT_PLAIN_UTF_16BE = new BYTE_ARRAY("text/plain; charset=utf-16be");
74 public static final BYTE_ARRAY TEXT_PLAIN_UTF_16LE = new BYTE_ARRAY("text/plain; charset=utf-16le");
75 public static final BYTE_ARRAY TEXT_PLAIN_UTF_8 = new BYTE_ARRAY("text/plain; charset=utf-8");
76
77 public BYTE_ARRAY(String mimeType)
78 {
79 super(mimeType, "[B");
80 }
81 }
82
83 /**
84 * @author Michael Koch (konqueror@gmx.de)
85 */
86 public static class CHAR_ARRAY
87 extends DocFlavor
88 {
89 private static final long serialVersionUID = -8720590903724405128L;
90
91 public static final DocFlavor.CHAR_ARRAY TEXT_HTML = new CHAR_ARRAY("text/html; charset=utf-16");
92 public static final DocFlavor.CHAR_ARRAY TEXT_PLAIN = new CHAR_ARRAY("text/plain; charset=utf-16");
93
94 public CHAR_ARRAY(String mimeType)
95 {
96 super(mimeType, "[C");
97 }
98 }
99
100 /**
101 * @author Michael Koch (konqueror@gmx.de)
102 */
103 public static class INPUT_STREAM
104 extends DocFlavor
105 {
106 public static final INPUT_STREAM AUTOSENSE = new INPUT_STREAM("application/octet-stream");
107 public static final INPUT_STREAM GIF = new INPUT_STREAM("image/gif");
108 public static final INPUT_STREAM JPEG = new INPUT_STREAM("image/jpeg");
109 public static final INPUT_STREAM PCL = new INPUT_STREAM("application/vnd.hp-PCL");
110 public static final INPUT_STREAM PDF = new INPUT_STREAM("application/pdf");
111 public static final INPUT_STREAM PNG = new INPUT_STREAM("image/png");
112 public static final INPUT_STREAM POSTSCRIPT = new INPUT_STREAM("application/postscript");
113 public static final INPUT_STREAM TEXT_HTML_HOST = new INPUT_STREAM("text/html");
114 public static final INPUT_STREAM TEXT_HTML_US_ASCII = new INPUT_STREAM("text/html; charset=us-ascii");
115 public static final INPUT_STREAM TEXT_HTML_UTF_16 = new INPUT_STREAM("text/html; charset=utf-16");
116 public static final INPUT_STREAM TEXT_HTML_UTF_16BE = new INPUT_STREAM("text/html; charset=utf-16be");
117 public static final INPUT_STREAM TEXT_HTML_UTF_16LE = new INPUT_STREAM("text/html; charset=utf-16le");
118 public static final INPUT_STREAM TEXT_HTML_UTF_8 = new INPUT_STREAM("text/html; charset=utf-8");
119 public static final INPUT_STREAM TEXT_PLAIN_HOST = new INPUT_STREAM("text/plain");
120 public static final INPUT_STREAM TEXT_PLAIN_US_ASCII = new INPUT_STREAM("text/plain; charset=us-ascii");
121 public static final INPUT_STREAM TEXT_PLAIN_UTF_16 = new INPUT_STREAM("text/plain; charset=utf-16");
122 public static final INPUT_STREAM TEXT_PLAIN_UTF_16BE = new INPUT_STREAM("text/plain; charset=utf-16be");
123 public static final INPUT_STREAM TEXT_PLAIN_UTF_16LE = new INPUT_STREAM("text/plain; charset=utf-16le");
124 public static final INPUT_STREAM TEXT_PLAIN_UTF_8 = new INPUT_STREAM("text/plain; charset=utf-8");
125
126 public INPUT_STREAM(String mimeType)
127 {
128 super(mimeType, "java.io.InputStream");
129 }
130 }
131
132 /**
133 * @author Michael Koch (konqueror@gmx.de)
134 */
135 public static class READER
136 extends DocFlavor
137 {
138 private static final long serialVersionUID = 7100295812579351567L;
139
140 public static final DocFlavor.READER TEXT_HTML = new READER("text/html; charset=utf-16");
141 public static final DocFlavor.READER TEXT_PLAIN = new READER("text/plain; charset=utf-16");
142
143 public READER(String mimeType)
144 {
145 super(mimeType, "java.io.Reader");
146 }
147 }
148
149 /**
150 * @author Michael Koch (konqueror@gmx.de)
151 */
152 public static class SERVICE_FORMATTED
153 extends DocFlavor
154 {
155 private static final long serialVersionUID = 6181337766266637256L;
156
157 public static final DocFlavor.SERVICE_FORMATTED PAGEABLE = new SERVICE_FORMATTED("java.awt.print.Pageable");
158 public static final DocFlavor.SERVICE_FORMATTED PRINTABLE = new SERVICE_FORMATTED("java.awt.print.Printable");
159 public static final DocFlavor.SERVICE_FORMATTED RENDERABLE_IMAGE = new SERVICE_FORMATTED("java.awt.image.renderable.RenderableImage");
160
161 public SERVICE_FORMATTED(String className)
162 {
163 super("application/x-java-jvm-local-objectref", className);
164 }
165 }
166
167 /**
168 * @author Michael Koch (konqueror@gmx.de)
169 */
170 public static class STRING
171 extends DocFlavor
172 {
173 private static final long serialVersionUID = 4414407504887034035L;
174
175 public static final DocFlavor.STRING TEXT_HTML = new STRING("text/html; charset=utf-16");
176 public static final DocFlavor.STRING TEXT_PLAIN = new STRING("text/plain; charset=utf-16");
177
178 public STRING(String mimeType)
179 {
180 super(mimeType, "java.lang.String");
181 }
182 }
183
184 /**
185 * @author Michael Koch (konqueror@gmx.de)
186 */
187 public static class URL
188 extends DocFlavor
189 {
190 private static final long serialVersionUID = 2936725788144902062L;
191
192 public static final DocFlavor.URL AUTOSENSE = new URL("application/octet-stream");
193 public static final DocFlavor.URL GIF = new URL("image/gif");
194 public static final DocFlavor.URL JPEG = new URL("image/jpeg");
195 public static final DocFlavor.URL PCL = new URL("application/vnd.hp-PCL");
196 public static final DocFlavor.URL PDF = new URL("application/pdf");
197 public static final DocFlavor.URL PNG = new URL("image/png");
198 public static final DocFlavor.URL POSTSCRIPT = new URL("application/postscript");
199 public static final DocFlavor.URL TEXT_HTML_HOST = new URL("text/html");
200 public static final DocFlavor.URL TEXT_HTML_US_ASCII = new URL("text/html; charset=us-ascii");
201 public static final DocFlavor.URL TEXT_HTML_UTF_16 = new URL("text/html; charset=utf-16");
202 public static final DocFlavor.URL TEXT_HTML_UTF_16BE = new URL("text/html; charset=utf-16be");
203 public static final DocFlavor.URL TEXT_HTML_UTF_16LE = new URL("text/html; charset=utf-16le");
204 public static final DocFlavor.URL TEXT_HTML_UTF_8 = new URL("text/html; charset=utf-8");
205 public static final DocFlavor.URL TEXT_PLAIN_HOST = new URL("text/plain");
206 public static final DocFlavor.URL TEXT_PLAIN_US_ASCII = new URL("text/plain; charset=us-ascii");
207 public static final DocFlavor.URL TEXT_PLAIN_UTF_16 = new URL("text/plain; charset=utf-16");
208 public static final DocFlavor.URL TEXT_PLAIN_UTF_16BE = new URL("text/plain; charset=utf-16be");
209 public static final DocFlavor.URL TEXT_PLAIN_UTF_16LE = new URL("text/plain; charset=utf-16le");
210 public static final DocFlavor.URL TEXT_PLAIN_UTF_8 = new URL("text/plain; charset=utf-8");
211
212 public URL(String mimeType)
213 {
214 super(mimeType, "java.net.URL");
215 }
216 }
217
218 private static final long serialVersionUID = -4512080796965449721L;
219
220 // FIXME: Get the host encoding from somewhere. Note that the new String is to make
221 // sure the field won't be a compile time constant.
222 public static final String hostEncoding = new String("US-ASCII");
223
224 private String mediaSubtype;
225 private String mediaType;
226 private String className;
227 private HashMap params = new HashMap();
228
229 public DocFlavor(String mimeType, String className)
230 {
231 if (mimeType == null || className == null)
232 throw new NullPointerException();
233
234 parseMimeType(mimeType);
235 this.className = className;
236 }
237
238 private void parseMimeType(String mimeType)
239 {
240 // FIXME: This method is know to be not completely correct, but it works for now.
241
242 int pos = mimeType.indexOf(';');
243
244 if (pos != -1)
245 {
246 String tmp = mimeType.substring(pos + 2);
247 mimeType = mimeType.substring(0, pos);
248 pos = tmp.indexOf('=');
249 params.put(tmp.substring(0, pos), tmp.substring(pos + 1));
250 }
251
252 pos = mimeType.indexOf('/');
253
254 if (pos == -1)
255 throw new IllegalArgumentException();
256
257 mediaType = mimeType.substring(0, pos);
258 mediaSubtype = mimeType.substring(pos + 1);
259 }
260
261 public boolean equals(Object obj)
262 {
263 if (! (obj instanceof DocFlavor))
264 return false;
265
266 DocFlavor tmp = (DocFlavor) obj;
267
268 return (getMimeType().equals(tmp.getMimeType())
269 && getRepresentationClassName().equals(tmp.getRepresentationClassName()));
270 }
271
272 public String getMediaSubtype()
273 {
274 return mediaSubtype;
275 }
276
277 public String getMediaType()
278 {
279 return mediaType;
280 }
281
282 public String getMimeType()
283 {
284 // FIXME: Check if this algorithm is correct.
285
286 String mimeType = getMediaType() + "/" + getMediaSubtype();
287 Iterator it = params.entrySet().iterator();
288
289 while (it.hasNext())
290 {
291 Map.Entry entry = (Map.Entry) it.next();
292 mimeType += "; " + entry.getKey() + "=\"" + entry.getValue() + "\"";
293 }
294
295 return mimeType;
296 }
297
298 public String getParameter(String paramName)
299 {
300 if (paramName == null)
301 throw new NullPointerException();
302
303 return (String) params.get(paramName);
304 }
305
306 public String getRepresentationClassName()
307 {
308 return className;
309 }
310
311 public int hashCode()
312 {
313 return ((mediaType.hashCode()
314 * mediaSubtype.hashCode()
315 * className.hashCode()) ^ params.hashCode());
316 }
317
318 public String toString()
319 {
320 return getMimeType();
321 }
322 }