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.

PDFNumsArray.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import java.util.Map;
  22. import java.util.SortedMap;
  23. import org.apache.commons.io.output.CountingOutputStream;
  24. /**
  25. * Class representing an "Nums" array object (for Number Trees).
  26. */
  27. public class PDFNumsArray extends PDFObject {
  28. /** Sorted Map holding the values of this array. */
  29. protected SortedMap<Integer, Object> map = new java.util.TreeMap<Integer, Object>();
  30. /**
  31. * Create a new, empty array object.
  32. * @param parent the object's parent if any
  33. */
  34. public PDFNumsArray(PDFObject parent) {
  35. super(parent);
  36. }
  37. /**
  38. * Returns the length of the array
  39. * @return the length of the array
  40. */
  41. public int length() {
  42. return this.map.size();
  43. }
  44. /**
  45. * Determines whether a value object should be converted to an indirect reference for inclusion in a Number Tree
  46. * array according to the PDF spec.
  47. * PDF1.0 - 1.2 - Spec is silent on this subject (as Number Trees don't exist).
  48. * PDF1.3 & 1.4 - Values must be indirect object refs.
  49. * PDF1.5 - Recommended: stream, dictionary, array, and string values be indirect object refs.
  50. * PDF1.6 - 2.0 - Stream values must be indirect object refs.
  51. * Recommended: dictionary, array, and string values be indirect object refs.
  52. * Method signals for values that must be, and those recommended to be indirect object refs.
  53. * @param obj The object to be considered.
  54. * @return True iff the object should be converted.
  55. */
  56. private boolean shouldConvertToRef(PDFObject obj) {
  57. boolean retval = false;
  58. if (getDocument() != null && getDocument().getPDFVersion() != null) {
  59. switch (getDocument().getPDFVersion()) {
  60. case V1_0: // fall-through
  61. case V1_1: // fall-through
  62. case V1_2:
  63. log.error("Number Tree used in PDF version " + getDocument().getPDFVersion());
  64. break;
  65. case V1_3: // fall-through
  66. case V1_4:
  67. retval = true;
  68. break;
  69. case V1_5: // fall-through
  70. case V1_6: // fall-through
  71. case V1_7: // fall-through
  72. case V2_0:
  73. if (obj instanceof PDFStream
  74. || obj instanceof PDFDictionary
  75. || obj instanceof PDFArray
  76. || obj instanceof PDFText) {
  77. retval = true;
  78. }
  79. break;
  80. default:
  81. log.error("Unrecognised PDF version " + getDocument().getPDFVersion());
  82. break;
  83. }
  84. }
  85. return retval;
  86. }
  87. /**
  88. * This method provides conformance with the different PDF specs which require or recommend different types be used
  89. * for Number Tree array values. Method indirects objects where indicated.
  90. * @param obj The object to be considered for indirection.
  91. * @return Either the object or a reference to the object.
  92. */
  93. private Object indirectIfReq(Object obj) {
  94. PDFDocument doc = getDocument();
  95. Object retval = obj;
  96. if (obj instanceof PDFObject) {
  97. PDFObject pdfObj = (PDFObject) obj;
  98. PDFObject parent = pdfObj.getParent();
  99. if (shouldConvertToRef(pdfObj)) {
  100. if (!pdfObj.hasObjectNumber()) { // Needs registering with the doc.
  101. pdfObj.setParent(null); // Can't register if it has a parent.
  102. pdfObj = doc.registerObject(pdfObj);
  103. if (parent != null) {
  104. pdfObj.setParent(parent); // Reinstate original parent.
  105. }
  106. }
  107. retval = pdfObj.makeReference();
  108. }
  109. }
  110. return retval;
  111. }
  112. /**
  113. * Sets an entry.
  114. * @param key the key of the value to set
  115. * @param obj the new value
  116. */
  117. public void put(Integer key, Object obj) {
  118. this.map.put(key, indirectIfReq(obj));
  119. }
  120. /**
  121. * Sets an entry.
  122. * @param key the key of the value to set
  123. * @param obj the new value
  124. */
  125. public void put(int key, Object obj) {
  126. put(Integer.valueOf(key), obj);
  127. }
  128. /**
  129. * Gets an entry.
  130. * @param key the key of requested value
  131. * @return the requested value
  132. */
  133. public Object get(Integer key) {
  134. return this.map.get(key);
  135. }
  136. /**
  137. * Gets an entry.
  138. * @param key the key of requested value
  139. * @return the requested value
  140. */
  141. public Object get(int key) {
  142. return get(Integer.valueOf(key));
  143. }
  144. /** {@inheritDoc} */
  145. @Override
  146. public int output(OutputStream stream) throws IOException {
  147. CountingOutputStream cout = new CountingOutputStream(stream);
  148. StringBuilder textBuffer = new StringBuilder(64);
  149. textBuffer.append('[');
  150. boolean first = true;
  151. for (Map.Entry<Integer, Object> entry : this.map.entrySet()) {
  152. if (!first) {
  153. textBuffer.append(" ");
  154. }
  155. first = false;
  156. formatObject(entry.getKey(), cout, textBuffer);
  157. textBuffer.append(" ");
  158. formatObject(entry.getValue(), cout, textBuffer);
  159. }
  160. textBuffer.append(']');
  161. PDFDocument.flushTextBuffer(textBuffer, cout);
  162. return cout.getCount();
  163. }
  164. }