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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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.io.Writer;
  22. import java.util.Collection;
  23. import java.util.List;
  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 values = new java.util.ArrayList();
  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(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(new Integer(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(new Double(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, Collection values) {
  77. /* generic creation of PDF object */
  78. super(parent);
  79. this.values.addAll(values);
  80. }
  81. /**
  82. * Create the array object
  83. * @param parent the array's parent if any
  84. * @param values the actual array wrapped by this object
  85. */
  86. public PDFArray(PDFObject parent, Object[] values) {
  87. /* generic creation of PDF object */
  88. super(parent);
  89. for (int i = 0, c = values.length; i < c; i++) {
  90. this.values.add(values[i]);
  91. }
  92. }
  93. /**
  94. * Indicates whether the given object exists in the array.
  95. * @param obj the object to look for
  96. * @return true if obj is contained
  97. */
  98. public boolean contains(Object obj) {
  99. return this.values.contains(obj);
  100. }
  101. /**
  102. * Returns the length of the array
  103. * @return the length of the array
  104. */
  105. public int length() {
  106. return this.values.size();
  107. }
  108. /**
  109. * Sets an entry at a given location.
  110. * @param index the index of the value to set
  111. * @param obj the new value
  112. */
  113. public void set(int index, Object obj) {
  114. this.values.set(index, obj);
  115. }
  116. /**
  117. * Sets an entry at a given location.
  118. * @param index the index of the value to set
  119. * @param value the new value
  120. */
  121. public void set(int index, double value) {
  122. this.values.set(index, new Double(value));
  123. }
  124. /**
  125. * Gets an entry at a given location.
  126. * @param index the index of the value to set
  127. * @return the requested value
  128. */
  129. public Object get(int index) {
  130. return this.values.get(index);
  131. }
  132. /**
  133. * Adds a new value to the array.
  134. * @param obj the value
  135. */
  136. public void add(Object obj) {
  137. if (obj instanceof PDFObject) {
  138. PDFObject pdfObj = (PDFObject)obj;
  139. if (!pdfObj.hasObjectNumber()) {
  140. pdfObj.setParent(this);
  141. }
  142. }
  143. this.values.add(obj);
  144. }
  145. /**
  146. * Adds a new value to the array.
  147. * @param value the value
  148. */
  149. public void add(double value) {
  150. this.values.add(new Double(value));
  151. }
  152. /**
  153. * Clears the PDF array.
  154. */
  155. public void clear() {
  156. this.values.clear();
  157. }
  158. /** {@inheritDoc} */
  159. protected int output(OutputStream stream) throws IOException {
  160. CountingOutputStream cout = new CountingOutputStream(stream);
  161. Writer writer = PDFDocument.getWriterFor(cout);
  162. if (hasObjectNumber()) {
  163. writer.write(getObjectID());
  164. }
  165. writer.write('[');
  166. for (int i = 0; i < values.size(); i++) {
  167. if (i > 0) {
  168. writer.write(' ');
  169. }
  170. Object obj = this.values.get(i);
  171. formatObject(obj, cout, writer);
  172. }
  173. writer.write(']');
  174. if (hasObjectNumber()) {
  175. writer.write("\nendobj\n");
  176. }
  177. writer.flush();
  178. return cout.getCount();
  179. }
  180. }