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.

PDFArray.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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.ArrayList;
  22. import java.util.List;
  23. import java.util.Set;
  24. import org.apache.commons.io.output.CountingOutputStream;
  25. /**
  26. * Class representing an array object.
  27. */
  28. public class PDFArray extends PDFObject {
  29. /**
  30. * List holding the values of this array
  31. */
  32. protected List<Object> values = new java.util.ArrayList<Object>();
  33. /**
  34. * Create a new, empty array object
  35. * @param parent the array's parent if any
  36. */
  37. public PDFArray(PDFObject parent) {
  38. /* generic creation of PDF object */
  39. super(parent);
  40. }
  41. /**
  42. * Create a new, empty array object with no parent.
  43. */
  44. public PDFArray() {
  45. this((PDFObject) null);
  46. }
  47. /**
  48. * Create an array object.
  49. * @param parent the array's parent if any
  50. * @param values the actual array wrapped by this object
  51. */
  52. public PDFArray(PDFObject parent, int[] values) {
  53. /* generic creation of PDF object */
  54. super(parent);
  55. for (int i = 0, c = values.length; i < c; i++) {
  56. this.values.add(values[i]);
  57. }
  58. }
  59. /**
  60. * Create an array object.
  61. * @param parent the array's parent if any
  62. * @param values the actual array wrapped by this object
  63. */
  64. public PDFArray(PDFObject parent, double[] values) {
  65. /* generic creation of PDF object */
  66. super(parent);
  67. for (int i = 0, c = values.length; i < c; i++) {
  68. this.values.add(values[i]);
  69. }
  70. }
  71. /**
  72. * Create an array object.
  73. * @param parent the array's parent if any
  74. * @param values the actual values wrapped by this object
  75. */
  76. public PDFArray(PDFObject parent, List<?> values) {
  77. /* generic creation of PDF object */
  78. super(parent);
  79. this.values.addAll(values);
  80. }
  81. /**
  82. * Creates an array object made of the given elements.
  83. *
  84. * @param elements the array content
  85. */
  86. public PDFArray(Object... elements) {
  87. this(null, elements);
  88. }
  89. /**
  90. * Creates an array object made of the given elements.
  91. *
  92. * @param elements the array content
  93. */
  94. public PDFArray(List<?> elements) {
  95. this(null, elements);
  96. }
  97. /**
  98. * Create the array object
  99. * @param parent the array's parent if any
  100. * @param values the actual array wrapped by this object
  101. */
  102. public PDFArray(PDFObject parent, Object[] values) {
  103. /* generic creation of PDF object */
  104. super(parent);
  105. for (int i = 0, c = values.length; i < c; i++) {
  106. this.values.add(values[i]);
  107. }
  108. }
  109. /**
  110. * Indicates whether the given object exists in the array.
  111. * @param obj the object to look for
  112. * @return true if obj is contained
  113. */
  114. public boolean contains(Object obj) {
  115. return this.values.contains(obj);
  116. }
  117. /**
  118. * Returns the length of the array
  119. * @return the length of the array
  120. */
  121. public int length() {
  122. return this.values.size();
  123. }
  124. /**
  125. * Sets an entry at a given location.
  126. * @param index the index of the value to set
  127. * @param obj the new value
  128. */
  129. public void set(int index, Object obj) {
  130. this.values.set(index, obj);
  131. }
  132. /**
  133. * Sets an entry at a given location.
  134. * @param index the index of the value to set
  135. * @param value the new value
  136. */
  137. public void set(int index, double value) {
  138. this.values.set(index, value);
  139. }
  140. /**
  141. * Gets an entry at a given location.
  142. * @param index the index of the value to set
  143. * @return the requested value
  144. */
  145. public Object get(int index) {
  146. return this.values.get(index);
  147. }
  148. /**
  149. * Adds a new value to the array.
  150. * @param obj the value
  151. */
  152. public void add(Object obj) {
  153. if (obj instanceof PDFObject) {
  154. PDFObject pdfObj = (PDFObject)obj;
  155. if (!pdfObj.hasObjectNumber()) {
  156. pdfObj.setParent(this);
  157. }
  158. }
  159. this.values.add(obj);
  160. }
  161. /**
  162. * Adds a new value to the array.
  163. * @param value the value
  164. */
  165. public void add(double value) {
  166. this.values.add(value);
  167. }
  168. /**
  169. * Clears the PDF array.
  170. */
  171. public void clear() {
  172. this.values.clear();
  173. }
  174. /** {@inheritDoc} */
  175. @Override
  176. public int output(OutputStream stream) throws IOException {
  177. CountingOutputStream cout = new CountingOutputStream(stream);
  178. StringBuilder textBuffer = new StringBuilder(64);
  179. textBuffer.append('[');
  180. for (int i = 0; i < values.size(); i++) {
  181. if (i > 0) {
  182. textBuffer.append(' ');
  183. }
  184. Object obj = this.values.get(i);
  185. formatObject(obj, cout, textBuffer);
  186. }
  187. textBuffer.append(']');
  188. PDFDocument.flushTextBuffer(textBuffer, cout);
  189. return cout.getCount();
  190. }
  191. @Override
  192. public void getChildren(Set<PDFObject> children) {
  193. List<Object> contents = new ArrayList<Object>();
  194. for (Object c : values) {
  195. if (!(c instanceof PDFReference)) {
  196. contents.add(c);
  197. }
  198. }
  199. PDFDictionary.getChildren(contents, children);
  200. }
  201. }