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 13KB

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