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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.Map;
  22. import java.util.SortedMap;
  23. import org.apache.commons.io.output.CountingOutputStream;
  24. /**
  25. * Class representing an "Nums" array object (for Number Trees).
  26. */
  27. public class PDFNumsArray extends PDFObject {
  28. /** Sorted Map holding the values of this array. */
  29. protected SortedMap<Integer, Object> map = new java.util.TreeMap<Integer, Object>();
  30. /**
  31. * Create a new, empty array object.
  32. * @param parent the object's parent if any
  33. */
  34. public PDFNumsArray(PDFObject parent) {
  35. super(parent);
  36. }
  37. /**
  38. * Returns the length of the array
  39. * @return the length of the array
  40. */
  41. public int length() {
  42. return this.map.size();
  43. }
  44. /**
  45. * Sets an entry.
  46. * @param key the key of the value to set
  47. * @param obj the new value
  48. */
  49. public void put(Integer key, Object obj) {
  50. this.map.put(key, obj);
  51. }
  52. /**
  53. * Sets an entry.
  54. * @param key the key of the value to set
  55. * @param obj the new value
  56. */
  57. public void put(int key, Object obj) {
  58. put(Integer.valueOf(key), obj);
  59. }
  60. /**
  61. * Gets an entry.
  62. * @param key the key of requested value
  63. * @return the requested value
  64. */
  65. public Object get(Integer key) {
  66. return this.map.get(key);
  67. }
  68. /**
  69. * Gets an entry.
  70. * @param key the key of requested value
  71. * @return the requested value
  72. */
  73. public Object get(int key) {
  74. return get(Integer.valueOf(key));
  75. }
  76. /** {@inheritDoc} */
  77. @Override
  78. public int output(OutputStream stream) throws IOException {
  79. CountingOutputStream cout = new CountingOutputStream(stream);
  80. StringBuilder textBuffer = new StringBuilder(64);
  81. textBuffer.append('[');
  82. boolean first = true;
  83. for (Map.Entry<Integer, Object> entry : this.map.entrySet()) {
  84. if (!first) {
  85. textBuffer.append(" ");
  86. }
  87. first = false;
  88. formatObject(entry.getKey(), cout, textBuffer);
  89. textBuffer.append(" ");
  90. formatObject(entry.getValue(), cout, textBuffer);
  91. }
  92. textBuffer.append(']');
  93. PDFDocument.flushTextBuffer(textBuffer, cout);
  94. return cout.getCount();
  95. }
  96. }