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

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