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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.util.Collection;
  20. import java.util.List;
  21. /**
  22. * Class representing an array object.
  23. */
  24. public class PDFArray extends PDFObject {
  25. /**
  26. * List holding the values of this array
  27. */
  28. protected List values = new java.util.ArrayList();
  29. /**
  30. * Create a new, empty array object
  31. */
  32. public PDFArray() {
  33. /* generic creation of PDF object */
  34. super();
  35. }
  36. /**
  37. * Create the array object
  38. *
  39. * @param values the actual array wrapped by this object
  40. */
  41. public PDFArray(int[] values) {
  42. /* generic creation of PDF object */
  43. super();
  44. for (int i = 0, c = values.length; i < c; i++) {
  45. this.values.add(new Integer(values[i]));
  46. }
  47. }
  48. /**
  49. * Create the array object
  50. *
  51. * @param values the actual values wrapped by this object
  52. */
  53. public PDFArray(Collection values) {
  54. /* generic creation of PDF object */
  55. super();
  56. this.values.addAll(values);
  57. }
  58. /**
  59. * Create the array object
  60. *
  61. * @param values the actual array wrapped by this object
  62. */
  63. public PDFArray(Object[] values) {
  64. /* generic creation of PDF object */
  65. super();
  66. for (int i = 0, c = values.length; i < c; i++) {
  67. this.values.add(values[i]);
  68. }
  69. }
  70. /**
  71. * Returns the length of the array
  72. * @return the length of the array
  73. */
  74. public int length() {
  75. return this.values.size();
  76. }
  77. /**
  78. * Sets an entry at a given location.
  79. * @param index the index of the value to set
  80. * @param obj the new value
  81. */
  82. public void set(int index, Object obj) {
  83. this.values.set(index, obj);
  84. }
  85. /**
  86. * Sets an entry at a given location.
  87. * @param index the index of the value to set
  88. * @param value the new value
  89. */
  90. public void set(int index, double value) {
  91. this.values.set(index, new Double(value));
  92. }
  93. /**
  94. * Gets an entry at a given location.
  95. * @param index the index of the value to set
  96. * @return the requested value
  97. */
  98. public Object get(int index) {
  99. return this.values.get(index);
  100. }
  101. /**
  102. * Adds a new value to the array.
  103. * @param obj the value
  104. */
  105. public void add(Object obj) {
  106. this.values.add(obj);
  107. }
  108. /**
  109. * Adds a new value to the array.
  110. * @param value the value
  111. */
  112. public void add(double value) {
  113. this.values.add(new Double(value));
  114. }
  115. /**
  116. * {@inheritDoc}
  117. */
  118. public String toPDFString() {
  119. StringBuffer p = new StringBuffer(64);
  120. if (hasObjectNumber()) {
  121. p.append(getObjectID());
  122. }
  123. p.append("[");
  124. for (int i = 0; i < values.size(); i++) {
  125. if (i > 0) {
  126. p.append(" ");
  127. }
  128. Object obj = this.values.get(i);
  129. formatObject(obj, p);
  130. }
  131. p.append("]");
  132. if (hasObjectNumber()) {
  133. p.append("\nendobj\n");
  134. }
  135. return p.toString();
  136. }
  137. }