DocFlavor.java 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. /* DocFlavor.java --
  2. Copyright (C) 2004, 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.print;
  32. import java.io.IOException;
  33. import java.io.ObjectInputStream;
  34. import java.io.Serializable;
  35. import java.io.StreamTokenizer;
  36. import java.io.StringReader;
  37. import java.nio.charset.Charset;
  38. import java.util.Iterator;
  39. import java.util.Map;
  40. import java.util.TreeMap;
  41. /**
  42. * <code>DocFlavor</code> provides a description of the format in which the
  43. * print data will be supplied in a print job to the print service.
  44. * <p>
  45. * A doc flavor consists of two parts:
  46. * <ul>
  47. * <li>
  48. * The MIME type (Multipurpose Internet Mail Extensions types as described
  49. * in RFC 2045/2046) specifying the media format of the print data.
  50. * </li><li>
  51. * The representation class name which is the fully qualified name of the
  52. * class providing the print data to the print job. For example if the print
  53. * data is supplied as a byte array the representation class name will be
  54. * <code>"[B"</code> or for an input stream <code>"java.io.InputStream"</code>.
  55. * </li>
  56. * </ul>
  57. * The <code>DocFlavor</code> class is therefore used in several places in the
  58. * Java Print Service API. A print service provides its supported document
  59. * flavors as an array of DocFlavor objects and a print job gets the flavor of
  60. * its data to print from the <code>Doc</code> object provided as a DocFlavor
  61. * instance.
  62. * </p>
  63. * <p>
  64. * It has to be differentiated between <b>client formatted</b> and <b>service
  65. * formatted</b> print data. Client formatted print data is already provided
  66. * formatted by the client e.g. in an image format or as postscript. For
  67. * service formatted print data, the Java Print Service instance produces
  68. * the formatted print data. Here the doc flavor's representation class name
  69. * does specify an interface instead of the actual print data source. The
  70. * print service will call the methods of the given implementation of this
  71. * interface with a special Graphics object capable of producing formatted
  72. * print data from the graphics routines inside the interface methods.
  73. * </p>
  74. * <p>
  75. * <h3>Client formatted print data document flavors</h3>
  76. * The print service uses the representation class of the doc flavor to know
  77. * how to retrieve the print data. If the representation class is a
  78. * <code>URL</code> it will open the URL to read the print data from it. If it is
  79. * a <code>byte[]</code> it will directly use the array and send it to the
  80. * printer. There are predefined doc flavor as inner class for the most common
  81. * representation class types:
  82. * <ul>
  83. * <li>Character arrays (<code>char[]</code>): The characters of the array
  84. * represent the print data.</li>
  85. * <li>Character streams (<code>java.io.Reader</code>): The whole characters
  86. * read from the stream represent the print data.</li>
  87. * <li>String (<code>java.lang.String</code>): The characters of the String
  88. * represent the print data.</li>
  89. * <li>Byte arrays (<code>byte[]</code>): The bytes of the array represent the
  90. * print data. Encoding if text content is given in the mime type.</li>
  91. * <li>Byte streams (<code>java.io.InputStream</code>): The whole bytes read
  92. * from the stream represent the print data. If text content the encoding is
  93. * specified in the mime type.</li>
  94. * <li>Uniform Resource Locator (<code>java.net.URL</code>): The bytes read
  95. * from the stream through opening of the URL represent the print data.
  96. * If text content the encoding is specified in the mime type.</li></li>
  97. * </ul>
  98. * </p>
  99. * <p>
  100. * <h3>Service formatted print data document flavors</h3>
  101. * The print service uses the provided object implementing the interface
  102. * specified by the representation class to produce the formatted print data.
  103. * The mime type of service formatted data is always
  104. * <code>"application/x-java-jvm-local-objectref"</code> to signal the local
  105. * reference to the print data object implementing the interface. Predefined
  106. * doc flavor classes exist as an inner class for the three available interface
  107. * to produce print data:
  108. * <ul>
  109. * <li>Pageable object (<code>java.awt.print.Pageable</code>): A pageable object
  110. * is supplied to the print service. The print service will call the methods of
  111. * the interface with a Grahics object to produce the formatted print data.</li>
  112. * <li>Printable object (<code>java.awt.print.Printable</code>): A printable object
  113. * is supplied to the print service. The print service will call the methods of
  114. * the interface with a Grahics object to produce the formatted print data.</li>
  115. * <li>Renderable Image object
  116. * (<code>java.awt.image.renderable.RenderableImage</code>): A renderable image
  117. * object is supplied to the print service. The print service calls methods of
  118. * this interface to obtain the image to be printed.</li>
  119. * </ul>
  120. * </p>
  121. *
  122. * @author Michael Koch (konqueror@gmx.de)
  123. * @author Wolfgang Baer (WBaer@gmx.de)
  124. */
  125. public class DocFlavor implements Cloneable, Serializable
  126. {
  127. /**
  128. * Predefined static <code>DocFlavor</code> objects for document
  129. * types which use a byte array for the print data representation.
  130. * <p>All the defined doc flavors have a print data representation
  131. * classname of "[B" (byte array).</p>
  132. *
  133. * @author Michael Koch (konqueror@gmx.de)
  134. */
  135. public static class BYTE_ARRAY
  136. extends DocFlavor
  137. {
  138. private static final long serialVersionUID = -9065578006593857475L;
  139. /**
  140. * Byte array doc flavor with a MIME Type of "application/octet-stream".
  141. */
  142. public static final BYTE_ARRAY AUTOSENSE = new BYTE_ARRAY("application/octet-stream");
  143. /**
  144. * Byte array doc flavor with a MIME Type of "image/gif".
  145. */
  146. public static final BYTE_ARRAY GIF = new BYTE_ARRAY("image/gif");
  147. /**
  148. * Byte array doc flavor with a MIME Type of "image/jpeg".
  149. */
  150. public static final BYTE_ARRAY JPEG = new BYTE_ARRAY("image/jpeg");
  151. /**
  152. * Byte array doc flavor with a MIME Type of "application/vnd.hp-PCL".
  153. */
  154. public static final BYTE_ARRAY PCL = new BYTE_ARRAY("application/vnd.hp-PCL");
  155. /**
  156. * Byte array doc flavor with a MIME Type of "application/pdf".
  157. */
  158. public static final BYTE_ARRAY PDF = new BYTE_ARRAY("application/pdf");
  159. /**
  160. * Byte array doc flavor with a MIME Type of "image/png".
  161. */
  162. public static final BYTE_ARRAY PNG = new BYTE_ARRAY("image/png");
  163. /**
  164. * Byte array doc flavor with a MIME Type of "application/postscript".
  165. */
  166. public static final BYTE_ARRAY POSTSCRIPT = new BYTE_ARRAY("application/postscript");
  167. /**
  168. * Byte array doc flavor with a MIME Type of "text/html" in the host encoding.
  169. */
  170. public static final BYTE_ARRAY TEXT_HTML_HOST = new BYTE_ARRAY("text/html; charset=" + hostEncoding);
  171. /**
  172. * Byte array doc flavor with a MIME Type of "text/html; charset=us-ascii".
  173. */
  174. public static final BYTE_ARRAY TEXT_HTML_US_ASCII = new BYTE_ARRAY("text/html; charset=us-ascii");
  175. /**
  176. * Byte array doc flavor with a MIME Type of "text/html; charset=utf-16".
  177. */
  178. public static final BYTE_ARRAY TEXT_HTML_UTF_16 = new BYTE_ARRAY("text/html; charset=utf-16");
  179. /**
  180. * Byte array doc flavor with a MIME Type of "text/html; charset=utf-16be".
  181. */
  182. public static final BYTE_ARRAY TEXT_HTML_UTF_16BE = new BYTE_ARRAY("text/html; charset=utf-16be");
  183. /**
  184. * Byte array doc flavor with a MIME Type of "text/html; charset=utf-16le".
  185. */
  186. public static final BYTE_ARRAY TEXT_HTML_UTF_16LE = new BYTE_ARRAY("text/html; charset=utf-16le");
  187. /**
  188. * Byte array doc flavor with a MIME Type of "text/html; charset=utf-8".
  189. */
  190. public static final BYTE_ARRAY TEXT_HTML_UTF_8 = new BYTE_ARRAY("text/html; charset=utf-8");
  191. /**
  192. * Byte array doc flavor with a MIME Type of "text/plain" in the host encoding.
  193. */
  194. public static final BYTE_ARRAY TEXT_PLAIN_HOST = new BYTE_ARRAY("text/plain; charset=" + hostEncoding);
  195. /**
  196. * Byte array doc flavor with a MIME Type of "text/plain; charset=us-ascii".
  197. */
  198. public static final BYTE_ARRAY TEXT_PLAIN_US_ASCII = new BYTE_ARRAY("text/plain; charset=us-ascii");
  199. /**
  200. * Byte array doc flavor with a MIME Type of "text/plain; charset=utf-16".
  201. */
  202. public static final BYTE_ARRAY TEXT_PLAIN_UTF_16 = new BYTE_ARRAY("text/plain; charset=utf-16");
  203. /**
  204. * Byte array doc flavor with a MIME Type of "text/plain; charset=utf-16be".
  205. */
  206. public static final BYTE_ARRAY TEXT_PLAIN_UTF_16BE = new BYTE_ARRAY("text/plain; charset=utf-16be");
  207. /**
  208. * Byte array doc flavor with a MIME Type of "text/plain; charset=utf-16le".
  209. */
  210. public static final BYTE_ARRAY TEXT_PLAIN_UTF_16LE = new BYTE_ARRAY("text/plain; charset=utf-16le");
  211. /**
  212. * Byte array doc flavor with a MIME Type of "text/plain; charset=utf-8".
  213. */
  214. public static final BYTE_ARRAY TEXT_PLAIN_UTF_8 = new BYTE_ARRAY("text/plain; charset=utf-8");
  215. /**
  216. * Constructor for doc flavor objects with the given MIME type
  217. * and a print data representation class name of "[B".
  218. *
  219. * @param mimeType the mime type string
  220. *
  221. * @throws NullPointerException if mimeType is <code>null</code>.
  222. * @throws IllegalArgumentException if mimeType has the wrong syntax.
  223. */
  224. public BYTE_ARRAY(String mimeType)
  225. {
  226. super(mimeType, "[B");
  227. }
  228. }
  229. /**
  230. * Predefined static <code>DocFlavor</code> objects for document
  231. * types which use a char array for the print data representation.
  232. * <p>All the defined doc flavors have a print data representation
  233. * classname of "[C" (char array).</p>
  234. *
  235. * @author Michael Koch (konqueror@gmx.de)
  236. */
  237. public static class CHAR_ARRAY
  238. extends DocFlavor
  239. {
  240. private static final long serialVersionUID = -8720590903724405128L;
  241. /**
  242. * Char array doc flavor with a MIME Type of "text/html; charset=utf-16".
  243. */
  244. public static final DocFlavor.CHAR_ARRAY TEXT_HTML = new CHAR_ARRAY("text/html; charset=utf-16");
  245. /**
  246. * Char array doc flavor with a MIME Type of "text/plain; charset=utf-16".
  247. */
  248. public static final DocFlavor.CHAR_ARRAY TEXT_PLAIN = new CHAR_ARRAY("text/plain; charset=utf-16");
  249. /**
  250. * Constructor for doc flavor objects with the given MIME type
  251. * and a print data representation class name of "[C".
  252. *
  253. * @param mimeType the mime type string
  254. *
  255. * @throws NullPointerException if mimeType is <code>null</code>.
  256. * @throws IllegalArgumentException if mimeType has the wrong syntax.
  257. */
  258. public CHAR_ARRAY(String mimeType)
  259. {
  260. super(mimeType, "[C");
  261. }
  262. }
  263. /**
  264. * Predefined static <code>DocFlavor</code> objects for document
  265. * types which use an InputStream to retrieve the print data.
  266. * <p>All the defined doc flavors have a print data representation
  267. * classname of "java.io.InputStream".</p>
  268. *
  269. * @author Michael Koch (konqueror@gmx.de)
  270. */
  271. public static class INPUT_STREAM
  272. extends DocFlavor
  273. {
  274. private static final long serialVersionUID = -7045842700749194127L;
  275. /**
  276. * InputStream doc flavor with a MIME Type of "application/octet-stream".
  277. */
  278. public static final INPUT_STREAM AUTOSENSE = new INPUT_STREAM("application/octet-stream");
  279. /**
  280. * InputStream doc flavor with a MIME Type of "image/gif".
  281. */
  282. public static final INPUT_STREAM GIF = new INPUT_STREAM("image/gif");
  283. /**
  284. * InputStream doc flavor with a MIME Type of "image/jpeg".
  285. */
  286. public static final INPUT_STREAM JPEG = new INPUT_STREAM("image/jpeg");
  287. /**
  288. * InputStream doc flavor with a MIME Type of "application/vnd.hp-PCL".
  289. */
  290. public static final INPUT_STREAM PCL = new INPUT_STREAM("application/vnd.hp-PCL");
  291. /**
  292. * InputStream doc flavor with a MIME Type of "application/pdf".
  293. */
  294. public static final INPUT_STREAM PDF = new INPUT_STREAM("application/pdf");
  295. /**
  296. * InputStream doc flavor with a MIME Type of "image/png".
  297. */
  298. public static final INPUT_STREAM PNG = new INPUT_STREAM("image/png");
  299. /**
  300. * InputStream doc flavor with a MIME Type of "application/postscript".
  301. */
  302. public static final INPUT_STREAM POSTSCRIPT = new INPUT_STREAM("application/postscript");
  303. /**
  304. * InputStream doc flavor with a MIME Type of "text/html" in the host encoding.
  305. */
  306. public static final INPUT_STREAM TEXT_HTML_HOST = new INPUT_STREAM("text/html; charset=" + hostEncoding);
  307. /**
  308. * InputStream doc flavor with a MIME Type of "text/html; charset=us-ascii".
  309. */
  310. public static final INPUT_STREAM TEXT_HTML_US_ASCII = new INPUT_STREAM("text/html; charset=us-ascii");
  311. /**
  312. * InputStream doc flavor with a MIME Type of "text/html; charset=utf-16".
  313. */
  314. public static final INPUT_STREAM TEXT_HTML_UTF_16 = new INPUT_STREAM("text/html; charset=utf-16");
  315. /**
  316. * InputStream doc flavor with a MIME Type of "text/html; charset=utf-16be".
  317. */
  318. public static final INPUT_STREAM TEXT_HTML_UTF_16BE = new INPUT_STREAM("text/html; charset=utf-16be");
  319. /**
  320. * InputStream doc flavor with a MIME Type of "text/html; charset=utf-16le".
  321. */
  322. public static final INPUT_STREAM TEXT_HTML_UTF_16LE = new INPUT_STREAM("text/html; charset=utf-16le");
  323. /**
  324. * InputStream doc flavor with a MIME Type of "text/html; charset=utf-8".
  325. */
  326. public static final INPUT_STREAM TEXT_HTML_UTF_8 = new INPUT_STREAM("text/html; charset=utf-8");
  327. /**
  328. * InputStream doc flavor with a MIME Type of "text/plain" in the host encoding.
  329. */
  330. public static final INPUT_STREAM TEXT_PLAIN_HOST = new INPUT_STREAM("text/plain; charset=" + hostEncoding);
  331. /**
  332. * InputStream doc flavor with a MIME Type of "text/plain; charset=us-ascii".
  333. */
  334. public static final INPUT_STREAM TEXT_PLAIN_US_ASCII = new INPUT_STREAM("text/plain; charset=us-ascii");
  335. /**
  336. * InputStream doc flavor with a MIME Type of "text/plain; charset=utf-16".
  337. */
  338. public static final INPUT_STREAM TEXT_PLAIN_UTF_16 = new INPUT_STREAM("text/plain; charset=utf-16");
  339. /**
  340. * InputStream doc flavor with a MIME Type of "text/plain; charset=utf-16be".
  341. */
  342. public static final INPUT_STREAM TEXT_PLAIN_UTF_16BE = new INPUT_STREAM("text/plain; charset=utf-16be");
  343. /**
  344. * InputStream doc flavor with a MIME Type of "text/plain; charset=utf-16le".
  345. */
  346. public static final INPUT_STREAM TEXT_PLAIN_UTF_16LE = new INPUT_STREAM("text/plain; charset=utf-16le");
  347. /**
  348. * InputStream doc flavor with a MIME Type of "text/plain; charset=utf-8".
  349. */
  350. public static final INPUT_STREAM TEXT_PLAIN_UTF_8 = new INPUT_STREAM("text/plain; charset=utf-8");
  351. /**
  352. * Constructor for doc flavor objects with the given MIME type
  353. * and a print data representation class name of "java.io.InputStream".
  354. *
  355. * @param mimeType the mime type string
  356. *
  357. * @throws NullPointerException if mimeType is <code>null</code>.
  358. * @throws IllegalArgumentException if mimeType has the wrong syntax.
  359. */
  360. public INPUT_STREAM(String mimeType)
  361. {
  362. super(mimeType, "java.io.InputStream");
  363. }
  364. }
  365. /**
  366. * Predefined static <code>DocFlavor</code> objects for document
  367. * types which use an Reader to retrieve the print data.
  368. * <p>All the defined doc flavors have a print data representation
  369. * classname of "java.io.Reader".</p>
  370. *
  371. * @author Michael Koch (konqueror@gmx.de)
  372. */
  373. public static class READER
  374. extends DocFlavor
  375. {
  376. private static final long serialVersionUID = 7100295812579351567L;
  377. /**
  378. * Reader doc flavor with a MIME Type of "text/html; charset=utf-16".
  379. */
  380. public static final DocFlavor.READER TEXT_HTML = new READER("text/html; charset=utf-16");
  381. /**
  382. * Reader doc flavor with a MIME Type of "text/plain; charset=utf-16".
  383. */
  384. public static final DocFlavor.READER TEXT_PLAIN = new READER("text/plain; charset=utf-16");
  385. /**
  386. * Constructor for doc flavor objects with the given MIME type
  387. * and a print data representation class name of "java.io.Reader".
  388. *
  389. * @param mimeType the mime type string
  390. *
  391. * @throws NullPointerException if mimeType is <code>null</code>.
  392. * @throws IllegalArgumentException if mimeType has the wrong syntax.
  393. */
  394. public READER(String mimeType)
  395. {
  396. super(mimeType, "java.io.Reader");
  397. }
  398. }
  399. /**
  400. * Predefined static <code>DocFlavor</code> objects for document
  401. * types which use service formatted print data.
  402. * <p>All the defined doc flavors have a MIME type of
  403. * "application/x-java-jvm-local-objectref".</p>
  404. *
  405. * @author Michael Koch (konqueror@gmx.de)
  406. */
  407. public static class SERVICE_FORMATTED
  408. extends DocFlavor
  409. {
  410. private static final long serialVersionUID = 6181337766266637256L;
  411. /**
  412. * Service formatted doc flavor with a representation class of
  413. * "java.awt.print.Pageable".
  414. */
  415. public static final DocFlavor.SERVICE_FORMATTED PAGEABLE = new SERVICE_FORMATTED("java.awt.print.Pageable");
  416. /**
  417. * Service formatted doc flavor with a representation class of
  418. * "java.awt.print.Printable".
  419. */
  420. public static final DocFlavor.SERVICE_FORMATTED PRINTABLE = new SERVICE_FORMATTED("java.awt.print.Printable");
  421. /**
  422. * Service formatted doc flavor with a representation class of
  423. * "java.awt.image.renderable.RenderableImage".
  424. */
  425. public static final DocFlavor.SERVICE_FORMATTED RENDERABLE_IMAGE = new SERVICE_FORMATTED("java.awt.image.renderable.RenderableImage");
  426. /**
  427. * Constructor for doc flavor objects with a MIME type of
  428. * "application/x-java-jvm-local-objectref" and the given
  429. * print data representation classname.
  430. *
  431. * @param className the representation classname
  432. *
  433. * @throws NullPointerException if className is <code>null</code>.
  434. */
  435. public SERVICE_FORMATTED(String className)
  436. {
  437. super("application/x-java-jvm-local-objectref", className);
  438. }
  439. }
  440. /**
  441. * Predefined static <code>DocFlavor</code> objects for document
  442. * types which use a String for the print data representation.
  443. * <p>All the defined doc flavors have a print data representation
  444. * classname of "java.lang.String".</p>
  445. *
  446. * @author Michael Koch (konqueror@gmx.de)
  447. */
  448. public static class STRING
  449. extends DocFlavor
  450. {
  451. private static final long serialVersionUID = 4414407504887034035L;
  452. /**
  453. * String doc flavor with a MIME Type of "text/html; charset=utf-16".
  454. */
  455. public static final DocFlavor.STRING TEXT_HTML = new STRING("text/html; charset=utf-16");
  456. /**
  457. * String doc flavor with a MIME Type of "text/plain; charset=utf-16".
  458. */
  459. public static final DocFlavor.STRING TEXT_PLAIN = new STRING("text/plain; charset=utf-16");
  460. /**
  461. * Constructor for doc flavor objects with the given MIME type
  462. * and a print data representation class name of "java.lang.String".
  463. *
  464. * @param mimeType the mime type string
  465. *
  466. * @throws NullPointerException if mimeType is <code>null</code>.
  467. * @throws IllegalArgumentException if mimeType has the wrong syntax.
  468. */
  469. public STRING(String mimeType)
  470. {
  471. super(mimeType, "java.lang.String");
  472. }
  473. }
  474. /**
  475. * Predefined static <code>DocFlavor</code> objects for document
  476. * types which have an URL where to retrieve the print data.
  477. * <p>All the defined doc flavors have a print data representation
  478. * classname of "java.net.URL".</p>
  479. *
  480. * @author Michael Koch (konqueror@gmx.de)
  481. */
  482. public static class URL
  483. extends DocFlavor
  484. {
  485. private static final long serialVersionUID = 2936725788144902062L;
  486. /**
  487. * URL doc flavor with a MIME Type of "application/octet-stream".
  488. */
  489. public static final DocFlavor.URL AUTOSENSE = new URL("application/octet-stream");
  490. /**
  491. * URL doc flavor with a MIME Type of "image/gif".
  492. */
  493. public static final DocFlavor.URL GIF = new URL("image/gif");
  494. /**
  495. * URL doc flavor with a MIME Type of "image/jpeg".
  496. */
  497. public static final DocFlavor.URL JPEG = new URL("image/jpeg");
  498. /**
  499. * URL doc flavor with a MIME Type of "application/vnd.hp-PCL".
  500. */
  501. public static final DocFlavor.URL PCL = new URL("application/vnd.hp-PCL");
  502. /**
  503. * URL doc flavor with a MIME Type of "application/pdf".
  504. */
  505. public static final DocFlavor.URL PDF = new URL("application/pdf");
  506. /**
  507. * URL doc flavor with a MIME Type of "image/png".
  508. */
  509. public static final DocFlavor.URL PNG = new URL("image/png");
  510. /**
  511. * URL doc flavor with a MIME Type of "application/postscript".
  512. */
  513. public static final DocFlavor.URL POSTSCRIPT = new URL("application/postscript");
  514. /**
  515. * URL doc flavor with a MIME Type of "text/html" in the host encoding.
  516. */
  517. public static final DocFlavor.URL TEXT_HTML_HOST = new URL("text/html; charset=" + hostEncoding);
  518. /**
  519. * URL doc flavor with a MIME Type of "text/html; charset=us-ascii".
  520. */
  521. public static final DocFlavor.URL TEXT_HTML_US_ASCII = new URL("text/html; charset=us-ascii");
  522. /**
  523. * URL doc flavor with a MIME Type of "text/html; charset=utf-16".
  524. */
  525. public static final DocFlavor.URL TEXT_HTML_UTF_16 = new URL("text/html; charset=utf-16");
  526. /**
  527. * URL doc flavor with a MIME Type of "text/html; charset=utf-16be".
  528. */
  529. public static final DocFlavor.URL TEXT_HTML_UTF_16BE = new URL("text/html; charset=utf-16be");
  530. /**
  531. * URL doc flavor with a MIME Type of "text/html; charset=utf-16le".
  532. */
  533. public static final DocFlavor.URL TEXT_HTML_UTF_16LE = new URL("text/html; charset=utf-16le");
  534. /**
  535. * URL doc flavor with a MIME Type of "text/html; charset=utf-8".
  536. */
  537. public static final DocFlavor.URL TEXT_HTML_UTF_8 = new URL("text/html; charset=utf-8");
  538. /**
  539. * URL doc flavor with a MIME Type of "text/plain" in the host encoding.
  540. */
  541. public static final DocFlavor.URL TEXT_PLAIN_HOST = new URL("text/plain; charset=" + hostEncoding);
  542. /**
  543. * URL doc flavor with a MIME Type of "text/plain; charset=us-ascii".
  544. */
  545. public static final DocFlavor.URL TEXT_PLAIN_US_ASCII = new URL("text/plain; charset=us-ascii");
  546. /**
  547. * URL doc flavor with a MIME Type of "text/plain; charset=utf-16".
  548. */
  549. public static final DocFlavor.URL TEXT_PLAIN_UTF_16 = new URL("text/plain; charset=utf-16");
  550. /**
  551. * URL doc flavor with a MIME Type of "text/plain; charset=utf-16be".
  552. */
  553. public static final DocFlavor.URL TEXT_PLAIN_UTF_16BE = new URL("text/plain; charset=utf-16be");
  554. /**
  555. * URL doc flavor with a MIME Type of "text/plain; charset=utf-16le".
  556. */
  557. public static final DocFlavor.URL TEXT_PLAIN_UTF_16LE = new URL("text/plain; charset=utf-16le");
  558. /**
  559. * URL doc flavor with a MIME Type of "text/plain; charset=utf-8".
  560. */
  561. public static final DocFlavor.URL TEXT_PLAIN_UTF_8 = new URL("text/plain; charset=utf-8");
  562. /**
  563. * Constructor for doc flavor objects with the given MIME type
  564. * and a print data representation class name of "java.net.URL".
  565. *
  566. * @param mimeType the mime type string
  567. *
  568. * @throws NullPointerException if mimeType is <code>null</code>.
  569. * @throws IllegalArgumentException if mimeType has the wrong syntax.
  570. */
  571. public URL(String mimeType)
  572. {
  573. super(mimeType, "java.net.URL");
  574. }
  575. }
  576. private static final long serialVersionUID = -4512080796965449721L;
  577. /**
  578. * The string representing the host encoding. This is the encoding
  579. * used in the predefined HOST doc flavors
  580. * (e.g. {@link BYTE_ARRAY#TEXT_HTML_HOST}).
  581. */
  582. public static final String hostEncoding = Charset.defaultCharset().name();
  583. private transient String mediaSubtype;
  584. private transient String mediaType;
  585. private transient TreeMap params;
  586. // name as defined in Serialized Form JDK 1.4
  587. private String myClassName;
  588. /**
  589. * Constructs a <code>DocFlavor</code> object with the given MIME type and
  590. * representation class name.
  591. *
  592. * @param mimeType the MIME type string.
  593. * @param className the fully-qualified name of the representation class.
  594. *
  595. * @throws NullPointerException if mimeType or className are <code>null</code>.
  596. * @throws IllegalArgumentException if given mimeType has syntax errors.
  597. */
  598. public DocFlavor(String mimeType, String className)
  599. {
  600. if (mimeType == null || className == null)
  601. throw new NullPointerException();
  602. params = new TreeMap();
  603. parseMimeType(mimeType);
  604. myClassName = className;
  605. }
  606. /**
  607. * Parses the given string as MIME type.
  608. * The mediatype, mediasubtype and all parameter/value
  609. * combinations are extracted, comments are dropped.
  610. *
  611. * @param mimeType the string to parse
  612. * @throws IllegalArgumentException if not conformant.
  613. */
  614. private void parseMimeType(String mimeType)
  615. {
  616. int MEDIA = 1;
  617. int MEDIASUB = 2;
  618. int PARAM_NAME = 3;
  619. int PARAM_VALUE = 4;
  620. int COMMENT_START = 5;
  621. int state = 0;
  622. int lastState = 0; // keeps track of state before comment
  623. int tok;
  624. try
  625. {
  626. String paramName = null;
  627. StreamTokenizer in = new StreamTokenizer(new StringReader(mimeType));
  628. in.resetSyntax();
  629. // Allowed characters are anything except:
  630. // SPACE, CTLs (= Unicode characters U+0000 - U+001F and U+007F)
  631. // and tspecials ( ) < > @ , ; : \ " / [ ] ? =
  632. in.whitespaceChars(0x00, 0x20);
  633. in.whitespaceChars(0x7F, 0x7F);
  634. in.wordChars('A', 'Z');
  635. in.wordChars('a', 'z');
  636. in.wordChars('0', '9');
  637. in.wordChars(0xA0, 0xFF);
  638. in.wordChars(0x21, 0x21);
  639. in.wordChars(0x23, 0x27);
  640. in.wordChars(0x2A, 0x2B);
  641. in.wordChars(0x2D, 0x2E);
  642. in.wordChars(0x5E, 0x60);
  643. in.wordChars(0x7B, 0x7E);
  644. in.quoteChar('"');
  645. while ((tok = in.nextToken()) != StreamTokenizer.TT_EOF)
  646. {
  647. switch (tok)
  648. {
  649. case StreamTokenizer.TT_WORD:
  650. if (state == 0)
  651. {
  652. mediaType = in.sval.toLowerCase();
  653. state = MEDIA;
  654. break;
  655. }
  656. if (state == MEDIA)
  657. {
  658. mediaSubtype = in.sval.toLowerCase();
  659. state = MEDIASUB;
  660. break;
  661. }
  662. // begin of parameters is either after mediasub or a parameter value
  663. if (state == MEDIASUB || state == PARAM_VALUE)
  664. {
  665. paramName = in.sval.toLowerCase();
  666. state = PARAM_NAME;
  667. break;
  668. }
  669. // a parameter always needs to follow a value
  670. if (state == PARAM_NAME)
  671. {
  672. String paramValue = in.sval;
  673. // if a charset param the value needs to be stored lowercase
  674. if (paramName.equals("charset"))
  675. paramValue = paramValue.toLowerCase();
  676. state = PARAM_VALUE;
  677. params.put(paramName, paramValue);
  678. break;
  679. }
  680. if (state == COMMENT_START)
  681. {
  682. // ignore;
  683. break;
  684. }
  685. break;
  686. case '/':
  687. // may only occur after the mediatype
  688. if (state != MEDIA)
  689. throw new IllegalArgumentException();
  690. break;
  691. case '=':
  692. // may only occur after a parameter
  693. if (state != PARAM_NAME)
  694. throw new IllegalArgumentException();
  695. break;
  696. case ';':
  697. // differentiates mime type and parameters/value combinations
  698. if (state != MEDIASUB && state != PARAM_VALUE)
  699. throw new IllegalArgumentException();
  700. break;
  701. case '(': // begin comment
  702. lastState = state;
  703. state = COMMENT_START;
  704. break;
  705. case ')': // end comment
  706. state = lastState;
  707. break;
  708. // a parameter always needs to follow a value / or quoted value
  709. case '"':
  710. if (state == PARAM_NAME)
  711. {
  712. String paramValue = in.sval;
  713. // if a charset param the value needs to be stored lowercase
  714. if (paramName.equals("charset"))
  715. paramValue = paramValue.toLowerCase();
  716. state = PARAM_VALUE;
  717. params.put(paramName, paramValue);
  718. break;
  719. }
  720. // only values may be quoted
  721. throw new IllegalArgumentException();
  722. default:
  723. // if any other char is observed its not allowed
  724. throw new IllegalArgumentException();
  725. }
  726. }
  727. }
  728. catch (IOException e)
  729. {
  730. // should not happen as mimetype str cannot be null
  731. throw new InternalError("IOException during parsing String " + mimeType);
  732. }
  733. }
  734. /**
  735. * Checks if this doc flavor object is equal to the given object.
  736. * <p>
  737. * Two doc flavor objects are considered equal if the provided object is not
  738. * <code>null</code> and an instance of <code>DocFlavor</code>. The MIME
  739. * types has to be equal in their media type, media subtype, their
  740. * paramter/value combinations and the representation classname.
  741. * </p>
  742. *
  743. * @param obj the object to test.
  744. * @return <code>true</code> if equal, <code>false</code> otherwise.
  745. */
  746. public boolean equals(Object obj)
  747. {
  748. if (! (obj instanceof DocFlavor))
  749. return false;
  750. DocFlavor tmp = (DocFlavor) obj;
  751. return (getMimeType().equals(tmp.getMimeType())
  752. && getRepresentationClassName().equals(tmp.getRepresentationClassName()));
  753. }
  754. /**
  755. * Returns the media subtype of this flavor object.
  756. * A mimetype of "text/html; charset=us-ascii" will
  757. * return "html" as the media subtype.
  758. *
  759. * @return The media subtype.
  760. */
  761. public String getMediaSubtype()
  762. {
  763. return mediaSubtype;
  764. }
  765. /**
  766. * Returns the media type of this flavor object.
  767. * A mimetype of "text/html; charset=us-ascii" will
  768. * return "text" as the media type.
  769. *
  770. * @return The media type.
  771. */
  772. public String getMediaType()
  773. {
  774. return mediaType;
  775. }
  776. /**
  777. * Returns the mime type of this flavor object.
  778. * The mimetype will have every parameter value
  779. * enclosed in quotes.
  780. *
  781. * @return The mime type.
  782. */
  783. public String getMimeType()
  784. {
  785. String mimeType = getMediaType() + "/" + getMediaSubtype();
  786. Iterator it = params.entrySet().iterator();
  787. while (it.hasNext())
  788. {
  789. Map.Entry entry = (Map.Entry) it.next();
  790. mimeType += "; " + entry.getKey() + "=\"" + entry.getValue() + "\"";
  791. }
  792. return mimeType;
  793. }
  794. /**
  795. * Returns the value for an optional parameter of the mime type of this
  796. * flavor object.
  797. *
  798. * @param paramName the name of the parameter
  799. * @return The value for the parameter, or <code>null</code> if none bound.
  800. * @throws NullPointerException if paramName is <code>null</code>.
  801. */
  802. public String getParameter(String paramName)
  803. {
  804. if (paramName == null)
  805. throw new NullPointerException();
  806. return (String) params.get(paramName.toLowerCase());
  807. }
  808. /**
  809. * Returns the name of the representation class of this flavor object.
  810. *
  811. * @return The representation classname.
  812. */
  813. public String getRepresentationClassName()
  814. {
  815. return myClassName;
  816. }
  817. /**
  818. * Returns a hash code for this doc flavor object.
  819. *
  820. * @return The hashcode.
  821. */
  822. public int hashCode()
  823. {
  824. return ((mediaType.hashCode()
  825. * mediaSubtype.hashCode()
  826. * myClassName.hashCode()) ^ params.hashCode());
  827. }
  828. /**
  829. * Returns a string representation of this doc flavor object.
  830. * The returned string is of the form
  831. * getMimeType() + "; class=\"" + getRepresentationClassName() + "\"";
  832. *
  833. * @return The constructed string representation.
  834. */
  835. public String toString()
  836. {
  837. return getMimeType() + "; class=\"" + getRepresentationClassName() + "\"";
  838. }
  839. // needs special treatment for serialization
  840. private void readObject(ObjectInputStream stream)
  841. throws IOException, ClassNotFoundException
  842. {
  843. params = new TreeMap();
  844. myClassName = (String) stream.readObject();
  845. parseMimeType((String) stream.readObject());
  846. }
  847. private void writeObject(java.io.ObjectOutputStream stream)
  848. throws IOException
  849. {
  850. stream.writeObject(myClassName);
  851. stream.writeObject(getMimeType());
  852. }
  853. }