SimpleDoc.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* SimpleDoc.java --
  2. Copyright (C) 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.ByteArrayInputStream;
  33. import java.io.CharArrayReader;
  34. import java.io.IOException;
  35. import java.io.InputStream;
  36. import java.io.Reader;
  37. import java.io.StringReader;
  38. import javax.print.attribute.AttributeSetUtilities;
  39. import javax.print.attribute.DocAttributeSet;
  40. /**
  41. * Simple implementation of the <code>Doc</code> interface capable of handling
  42. * the predefined document flavors of <code>DocFlavor</code>.
  43. * <p>
  44. * This implementation can construct a reader or stream for the service from
  45. * the print data and ensures that always the same object is returned on each
  46. * method call. It does simple checks that the supplied data matches the
  47. * specified flavor of the doc object and supports thread safe access.
  48. * </p>
  49. *
  50. * @author Wolfgang Baer (WBaer@gmx.de)
  51. */
  52. public final class SimpleDoc implements Doc
  53. {
  54. private final Object printData;
  55. private final DocFlavor flavor;
  56. private final DocAttributeSet attributes;
  57. private InputStream stream;
  58. private Reader reader;
  59. /**
  60. * Constructs a SimpleDoc with the specified print data, doc flavor and doc attribute set.
  61. * @param printData the object with the data to print.
  62. * @param flavor the document flavor of the print data.
  63. * @param attributes the attributes of the doc (may be <code>null</code>).
  64. *
  65. * @throws IllegalArgumentException if either <code>printData</code> or
  66. * <code>flavor</code> are <code>null</code>, or the print data is not
  67. * supplied in the document format specified by the given flavor object.
  68. */
  69. public SimpleDoc(Object printData, DocFlavor flavor,
  70. DocAttributeSet attributes)
  71. {
  72. if (printData == null || flavor == null)
  73. throw new IllegalArgumentException("printData/flavor may not be null");
  74. if (! (printData.getClass().getName().equals(
  75. flavor.getRepresentationClassName())
  76. || flavor.getRepresentationClassName().equals("java.io.Reader")
  77. && printData instanceof Reader
  78. || flavor.getRepresentationClassName().equals("java.io.InputStream")
  79. && printData instanceof InputStream))
  80. {
  81. throw new IllegalArgumentException("data is not of declared flavor type");
  82. }
  83. this.printData = printData;
  84. this.flavor = flavor;
  85. if (attributes != null)
  86. this.attributes = AttributeSetUtilities.unmodifiableView(attributes);
  87. else
  88. this.attributes = null;
  89. stream = null;
  90. reader = null;
  91. }
  92. /**
  93. * Returns the unmodifiable view of the attributes of this doc object.
  94. * <p>
  95. * The attributes of this doc's attributes set overrides attributes of
  96. * the same category in the print job's attribute set. If an attribute
  97. * is not available in this doc's attributes set or <code>null</code>
  98. * is returned the attributes of the same category of the print job are
  99. * used.
  100. * </p>
  101. *
  102. * @return The unmodifiable attributes set, or <code>null</code>.
  103. */
  104. public DocAttributeSet getAttributes()
  105. {
  106. return attributes;
  107. }
  108. /**
  109. * Returns the flavor of this doc objects print data.
  110. *
  111. * @return The document flavor.
  112. */
  113. public DocFlavor getDocFlavor()
  114. {
  115. return flavor;
  116. }
  117. /**
  118. * Returns the print data of this doc object.
  119. * <p>
  120. * The returned object is an instance as described by the associated
  121. * document flavor ({@link DocFlavor#getRepresentationClassName()})
  122. * and can be cast to this representation class.
  123. * </p>
  124. *
  125. * @return The print data in the representation class.
  126. * @throws IOException if representation class is a stream and I/O
  127. * exception occures.
  128. */
  129. public Object getPrintData() throws IOException
  130. {
  131. return printData;
  132. }
  133. /**
  134. * Returns a <code>Reader</code> object for extracting character print data
  135. * from this document.
  136. * <p>
  137. * This method is supported if the document flavor is of type:
  138. * <ul>
  139. * <li><code>char[]</code></li>
  140. * <li><code>java.lang.String</code></li>
  141. * <li><code>java.io.Reader</code></li>
  142. * </ul>
  143. * otherwise this method returns <code>null</code>.
  144. * </p>
  145. *
  146. * @return The <code>Reader</code> object, or <code>null</code>.
  147. *
  148. * @throws IOException if an error occurs.
  149. */
  150. public Reader getReaderForText() throws IOException
  151. {
  152. synchronized (this)
  153. {
  154. // construct the reader if applicable on request
  155. if (reader == null)
  156. {
  157. if (flavor instanceof DocFlavor.CHAR_ARRAY)
  158. reader = new CharArrayReader((char[]) printData);
  159. else if (flavor instanceof DocFlavor.STRING)
  160. reader = new StringReader((String) printData);
  161. else if (flavor instanceof DocFlavor.READER)
  162. reader = (Reader) printData;
  163. }
  164. return reader;
  165. }
  166. }
  167. /**
  168. * Returns an <code>InputStream</code> object for extracting byte print data
  169. * from this document.
  170. * <p>
  171. * This method is supported if the document flavor is of type:
  172. * <ul>
  173. * <li><code>byte[]</code></li>
  174. * <li><code>java.io.InputStream</code></li>
  175. * </ul>
  176. * otherwise this method returns <code>null</code>.
  177. * </p>
  178. *
  179. * @return The <code>InputStream</code> object, or <code>null</code>.
  180. *
  181. * @throws IOException if an error occurs.
  182. */
  183. public InputStream getStreamForBytes() throws IOException
  184. {
  185. synchronized (this)
  186. {
  187. // construct the stream if applicable on request
  188. if (stream == null)
  189. {
  190. if (flavor instanceof DocFlavor.BYTE_ARRAY)
  191. stream = new ByteArrayInputStream((byte[]) printData);
  192. else if (flavor instanceof DocFlavor.INPUT_STREAM)
  193. stream = (InputStream) printData;
  194. }
  195. return stream;
  196. }
  197. }
  198. }