You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PDFObject.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. /* $Id$ */
  18. package org.apache.fop.pdf;
  19. // Java
  20. import java.io.IOException;
  21. import java.io.OutputStream;
  22. import org.apache.commons.logging.Log;
  23. import org.apache.commons.logging.LogFactory;
  24. /**
  25. * generic PDF object.
  26. *
  27. * A PDF Document is essentially a collection of these objects. A PDF
  28. * Object has a number and a generation (although the generation will always
  29. * be 0 in new documents).
  30. */
  31. public abstract class PDFObject implements PDFWritable {
  32. /** logger for all PDFObjects (and descendants) */
  33. protected static final Log log = LogFactory.getLog(PDFObject.class.getName());
  34. /**
  35. * the object's number
  36. */
  37. private int objnum;
  38. /**
  39. * the object's generation (0 in new documents)
  40. */
  41. private int generation = 0;
  42. /**
  43. * the parent PDFDocument
  44. */
  45. private PDFDocument document;
  46. /** the parent PDFObject (may be null and may not always be set, needed for encryption) */
  47. private PDFObject parent;
  48. /**
  49. * Returns the object's number.
  50. * @return the PDF Object number
  51. */
  52. public int getObjectNumber() {
  53. if (this.objnum == 0) {
  54. throw new IllegalStateException("Object has no number assigned: " + this.toString());
  55. }
  56. return this.objnum;
  57. }
  58. /**
  59. * Default constructor.
  60. */
  61. public PDFObject() {
  62. //nop
  63. }
  64. /**
  65. * Constructor for direct objects.
  66. * @param parent the containing PDFObject instance
  67. */
  68. public PDFObject(PDFObject parent) {
  69. setParent(parent);
  70. }
  71. /**
  72. * Indicates whether this PDFObject has already been assigned an
  73. * object number.
  74. * @return True if it has an object number
  75. */
  76. public boolean hasObjectNumber() {
  77. return this.objnum > 0;
  78. }
  79. /**
  80. * Sets the object number
  81. * @param objnum the object number
  82. */
  83. public void setObjectNumber(int objnum) {
  84. this.objnum = objnum;
  85. PDFDocument doc = getDocument();
  86. setParent(null);
  87. setDocument(doc); //Restore reference to PDFDocument after setting parent to null
  88. if (log.isTraceEnabled()) {
  89. log.trace("Assigning " + this + " object number " + objnum);
  90. }
  91. }
  92. /**
  93. * Returns this object's generation.
  94. * @return the PDF Object generation
  95. */
  96. public int getGeneration() {
  97. return this.generation;
  98. }
  99. /**
  100. * Returns the parent PDFDocument if assigned.
  101. * @return the parent PDFDocument (May be null if the parent PDFDocument
  102. * has not been assigned)
  103. */
  104. public final PDFDocument getDocument() {
  105. if (this.document != null) {
  106. return this.document;
  107. } else if (getParent() != null) {
  108. return getParent().getDocument();
  109. } else {
  110. return null;
  111. }
  112. }
  113. /**
  114. * Returns the parent PDFDocument, but unlike <code>getDocument()</code>
  115. * it throws an informative Exception if the parent document is unavailable
  116. * instead of having a NullPointerException somewhere without a message.
  117. * @return the parent PDFDocument
  118. */
  119. public final PDFDocument getDocumentSafely() {
  120. final PDFDocument doc = getDocument();
  121. if (doc == null) {
  122. throw new IllegalStateException("Parent PDFDocument is unavailable on "
  123. + getClass().getName());
  124. }
  125. return doc;
  126. }
  127. /**
  128. * Sets the parent PDFDocument.
  129. * @param doc the PDFDocument.
  130. */
  131. public void setDocument(PDFDocument doc) {
  132. this.document = doc;
  133. }
  134. /**
  135. * Returns this objects's parent. The parent is null if it is a "direct object".
  136. * @return the parent or null if there's no parent (or it hasn't been set)
  137. */
  138. public PDFObject getParent() {
  139. return this.parent;
  140. }
  141. /**
  142. * Sets the direct parent object.
  143. * @param parent the direct parent
  144. */
  145. public void setParent(PDFObject parent) {
  146. this.parent = parent;
  147. }
  148. /**
  149. * Returns the PDF representation of the Object ID.
  150. * @return the Object ID
  151. */
  152. public String getObjectID() {
  153. return getObjectNumber() + " " + getGeneration() + " obj\n";
  154. }
  155. /**
  156. * Returns the PDF representation of a reference to this object.
  157. * @return the reference string
  158. */
  159. public String referencePDF() {
  160. if (!hasObjectNumber()) {
  161. throw new IllegalArgumentException(
  162. "Cannot reference this object. It doesn't have an object number");
  163. }
  164. String ref = getObjectNumber() + " " + getGeneration() + " R";
  165. return ref;
  166. }
  167. /**
  168. * Creates and returns a reference to this object.
  169. * @return the object reference
  170. */
  171. public PDFReference makeReference() {
  172. return new PDFReference(this);
  173. }
  174. /**
  175. * Write the PDF represention of this object
  176. *
  177. * @param stream the stream to write the PDF to
  178. * @throws IOException if there is an error writing to the stream
  179. * @return the number of bytes written
  180. */
  181. public int output(OutputStream stream) throws IOException {
  182. byte[] pdf = this.toPDF();
  183. stream.write(pdf);
  184. return pdf.length;
  185. }
  186. /** {@inheritDoc} */
  187. public void outputInline(OutputStream out, StringBuilder textBuffer) throws IOException {
  188. if (hasObjectNumber()) {
  189. textBuffer.append(referencePDF());
  190. } else {
  191. PDFDocument.flushTextBuffer(textBuffer, out);
  192. output(out);
  193. }
  194. }
  195. /**
  196. * Encodes the object as a byte array for output to a PDF file.
  197. *
  198. * @return PDF string
  199. */
  200. protected byte[] toPDF() {
  201. return encode(toPDFString());
  202. }
  203. /**
  204. * This method returns a String representation of the PDF object. The result
  205. * is normally converted/encoded to a byte array by toPDF(). Only use
  206. * this method to implement the serialization if the object can be fully
  207. * represented as text. If the PDF representation of the object contains
  208. * binary content use toPDF() or output(OutputStream) instead. This applies
  209. * to any object potentially containing a string object because string object
  210. * are encrypted and therefore need to be binary.
  211. * @return String the String representation
  212. */
  213. protected String toPDFString() {
  214. throw new UnsupportedOperationException("Not implemented. "
  215. + "Use output(OutputStream) instead.");
  216. }
  217. /**
  218. * Converts text to a byte array for writing to a PDF file.
  219. * @param text text to convert/encode
  220. * @return byte[] the resulting byte array
  221. */
  222. public static final byte[] encode(String text) {
  223. return PDFDocument.encode(text);
  224. }
  225. /**
  226. * Encodes a Text String (3.8.1 in PDF 1.4 specs)
  227. * @param text the text to encode
  228. * @return byte[] the encoded text
  229. */
  230. protected byte[] encodeText(String text) {
  231. if (getDocumentSafely().isEncryptionActive()) {
  232. final byte[] buf = PDFText.toUTF16(text);
  233. return PDFText.escapeByteArray(
  234. getDocument().getEncryption().encrypt(buf, this));
  235. } else {
  236. return encode(PDFText.escapeText(text, false));
  237. }
  238. }
  239. /**
  240. * Encodes a String (3.2.3 in PDF 1.4 specs)
  241. * @param string the string to encode
  242. * @return byte[] the encoded string
  243. */
  244. protected byte[] encodeString(String string) {
  245. return encodeText(string);
  246. }
  247. /**
  248. * Encodes binary data as hexadecimal string object.
  249. * @param data the binary data
  250. * @param out the OutputStream to write the encoded object to
  251. * @throws IOException if an I/O error occurs
  252. */
  253. protected void encodeBinaryToHexString(byte[] data, OutputStream out) throws IOException {
  254. out.write('<');
  255. if (getDocumentSafely().isEncryptionActive()) {
  256. data = getDocument().getEncryption().encrypt(data, this);
  257. }
  258. String hex = PDFText.toHex(data, false);
  259. byte[] encoded = hex.getBytes("US-ASCII");
  260. out.write(encoded);
  261. out.write('>');
  262. }
  263. /**
  264. * Formats an object for serialization to PDF.
  265. * <p>
  266. * IMPORTANT: If you need to write out binary output, call
  267. * {@link PDFDocument#flushTextBuffer(StringBuilder, OutputStream)} before writing any content
  268. * to the {@link OutputStream}!
  269. * @param obj the object
  270. * @param out the OutputStream to write to
  271. * @param textBuffer a text buffer for text output
  272. * @throws IOException If an I/O error occurs
  273. */
  274. protected void formatObject(Object obj, OutputStream out, StringBuilder textBuffer)
  275. throws IOException {
  276. if (obj == null) {
  277. textBuffer.append("null");
  278. } else if (obj instanceof PDFWritable) {
  279. ((PDFWritable)obj).outputInline(out, textBuffer);
  280. } else if (obj instanceof Number) {
  281. if (obj instanceof Double || obj instanceof Float) {
  282. textBuffer.append(PDFNumber.doubleOut(((Number)obj).doubleValue()));
  283. } else {
  284. textBuffer.append(obj.toString());
  285. }
  286. } else if (obj instanceof Boolean) {
  287. textBuffer.append(obj.toString());
  288. } else if (obj instanceof byte[]) {
  289. PDFDocument.flushTextBuffer(textBuffer, out);
  290. encodeBinaryToHexString((byte[])obj, out);
  291. } else {
  292. PDFDocument.flushTextBuffer(textBuffer, out);
  293. out.write(encodeText(obj.toString()));
  294. }
  295. }
  296. /**
  297. * Check if the other PDFObject has the same content as the current object.
  298. * <p>
  299. * Note: This function has a contract which is less binding than
  300. * {@link #equals(Object)}. Whereas equals would require all values to be
  301. * identical, this method is not required to check everything. In the case
  302. * of PDFObjects, this means that the overriding function does not have to
  303. * check for {@link #getObjectID()}.
  304. *
  305. * @param o
  306. * object to compare to.
  307. * @return true if the other object has the same content.
  308. */
  309. protected boolean contentEquals(PDFObject o) {
  310. return this.equals(o);
  311. }
  312. }