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

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