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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 an array object.
  43. * @param parent the array's parent if any
  44. * @param values the actual array wrapped by this object
  45. */
  46. public PDFArray(PDFObject parent, int[] values) {
  47. /* generic creation of PDF object */
  48. super(parent);
  49. for (int i = 0, c = values.length; i < c; i++) {
  50. this.values.add(new Integer(values[i]));
  51. }
  52. }
  53. /**
  54. * Create an array object.
  55. * @param parent the array's parent if any
  56. * @param values the actual values wrapped by this object
  57. */
  58. public PDFArray(PDFObject parent, Collection values) {
  59. /* generic creation of PDF object */
  60. super(parent);
  61. this.values.addAll(values);
  62. }
  63. /**
  64. * Create the array object
  65. * @param parent the array's parent if any
  66. * @param values the actual array wrapped by this object
  67. */
  68. public PDFArray(PDFObject parent, Object[] values) {
  69. /* generic creation of PDF object */
  70. super(parent);
  71. for (int i = 0, c = values.length; i < c; i++) {
  72. this.values.add(values[i]);
  73. }
  74. }
  75. /**
  76. * Returns the length of the array
  77. * @return the length of the array
  78. */
  79. public int length() {
  80. return this.values.size();
  81. }
  82. /**
  83. * Sets an entry at a given location.
  84. * @param index the index of the value to set
  85. * @param obj the new value
  86. */
  87. public void set(int index, Object obj) {
  88. this.values.set(index, obj);
  89. }
  90. /**
  91. * Sets an entry at a given location.
  92. * @param index the index of the value to set
  93. * @param value the new value
  94. */
  95. public void set(int index, double value) {
  96. this.values.set(index, new Double(value));
  97. }
  98. /**
  99. * Gets an entry at a given location.
  100. * @param index the index of the value to set
  101. * @return the requested value
  102. */
  103. public Object get(int index) {
  104. return this.values.get(index);
  105. }
  106. /**
  107. * Adds a new value to the array.
  108. * @param obj the value
  109. */
  110. public void add(PDFObject obj) {
  111. if (obj != null) {
  112. obj.setParent(this);
  113. }
  114. this.values.add(obj);
  115. }
  116. /**
  117. * Adds a new value to the array.
  118. * @param obj the value
  119. */
  120. public void add(Object obj) {
  121. this.values.add(obj);
  122. }
  123. /**
  124. * Adds a new value to the array.
  125. * @param value the value
  126. */
  127. public void add(double value) {
  128. this.values.add(new Double(value));
  129. }
  130. /** {@inheritDoc} */
  131. protected int output(OutputStream stream) throws IOException {
  132. CountingOutputStream cout = new CountingOutputStream(stream);
  133. Writer writer = PDFDocument.getWriterFor(cout);
  134. if (hasObjectNumber()) {
  135. writer.write(getObjectID());
  136. }
  137. writer.write('[');
  138. for (int i = 0; i < values.size(); i++) {
  139. if (i > 0) {
  140. writer.write(' ');
  141. }
  142. Object obj = this.values.get(i);
  143. formatObject(obj, cout, writer);
  144. }
  145. writer.write(']');
  146. if (hasObjectNumber()) {
  147. writer.write("\nendobj\n");
  148. }
  149. writer.flush();
  150. return cout.getCount();
  151. }
  152. }