Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

PDFArray.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. * Creates an array object made of the given elements.
  89. *
  90. * @param elements the array content
  91. */
  92. public PDFArray(List<?> elements) {
  93. this(null, elements);
  94. }
  95. /**
  96. * Create the array object
  97. * @param parent the array's parent if any
  98. * @param values the actual array wrapped by this object
  99. */
  100. public PDFArray(PDFObject parent, Object[] values) {
  101. /* generic creation of PDF object */
  102. super(parent);
  103. for (int i = 0, c = values.length; i < c; i++) {
  104. this.values.add(values[i]);
  105. }
  106. }
  107. /**
  108. * Indicates whether the given object exists in the array.
  109. * @param obj the object to look for
  110. * @return true if obj is contained
  111. */
  112. public boolean contains(Object obj) {
  113. return this.values.contains(obj);
  114. }
  115. /**
  116. * Returns the length of the array
  117. * @return the length of the array
  118. */
  119. public int length() {
  120. return this.values.size();
  121. }
  122. /**
  123. * Sets an entry at a given location.
  124. * @param index the index of the value to set
  125. * @param obj the new value
  126. */
  127. public void set(int index, Object obj) {
  128. this.values.set(index, obj);
  129. }
  130. /**
  131. * Sets an entry at a given location.
  132. * @param index the index of the value to set
  133. * @param value the new value
  134. */
  135. public void set(int index, double value) {
  136. this.values.set(index, new Double(value));
  137. }
  138. /**
  139. * Gets an entry at a given location.
  140. * @param index the index of the value to set
  141. * @return the requested value
  142. */
  143. public Object get(int index) {
  144. return this.values.get(index);
  145. }
  146. /**
  147. * Adds a new value to the array.
  148. * @param obj the value
  149. */
  150. public void add(Object obj) {
  151. if (obj instanceof PDFObject) {
  152. PDFObject pdfObj = (PDFObject)obj;
  153. if (!pdfObj.hasObjectNumber()) {
  154. pdfObj.setParent(this);
  155. }
  156. }
  157. this.values.add(obj);
  158. }
  159. /**
  160. * Adds a new value to the array.
  161. * @param value the value
  162. */
  163. public void add(double value) {
  164. this.values.add(new Double(value));
  165. }
  166. /**
  167. * Clears the PDF array.
  168. */
  169. public void clear() {
  170. this.values.clear();
  171. }
  172. /** {@inheritDoc} */
  173. @Override
  174. public int output(OutputStream stream) throws IOException {
  175. CountingOutputStream cout = new CountingOutputStream(stream);
  176. StringBuilder textBuffer = new StringBuilder(64);
  177. textBuffer.append('[');
  178. for (int i = 0; i < values.size(); i++) {
  179. if (i > 0) {
  180. textBuffer.append(' ');
  181. }
  182. Object obj = this.values.get(i);
  183. formatObject(obj, cout, textBuffer);
  184. }
  185. textBuffer.append(']');
  186. PDFDocument.flushTextBuffer(textBuffer, cout);
  187. return cout.getCount();
  188. }
  189. }