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.

PDFDictionary.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.Collection;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Set;
  26. import org.apache.commons.io.output.CountingOutputStream;
  27. /**
  28. * Class representing a PDF dictionary object
  29. */
  30. public class PDFDictionary extends PDFObject {
  31. private boolean visited;
  32. /**
  33. * the entry map
  34. */
  35. protected Map<String, Object> entries = new java.util.HashMap<String, Object>();
  36. /**
  37. * maintains the order of the entries added to the entry map. Whenever you modify
  38. * "entries", always make sure you adjust this list accordingly.
  39. */
  40. protected List<String> order = new java.util.ArrayList<String>();
  41. /**
  42. * Create a new dictionary object.
  43. */
  44. public PDFDictionary() {
  45. super();
  46. }
  47. /**
  48. * Create a new dictionary object.
  49. * @param parent the object's parent if any
  50. */
  51. public PDFDictionary(PDFObject parent) {
  52. super(parent);
  53. }
  54. /**
  55. * Puts a new name/value pair.
  56. * @param name the name
  57. * @param value the value
  58. */
  59. public void put(String name, Object value) {
  60. if (value instanceof PDFObject) {
  61. PDFObject pdfObj = (PDFObject)value;
  62. if (!pdfObj.hasObjectNumber()) {
  63. pdfObj.setParent(this);
  64. }
  65. }
  66. if (!entries.containsKey(name)) {
  67. this.order.add(name);
  68. }
  69. this.entries.put(name, value);
  70. }
  71. /**
  72. * Puts a new name/value pair.
  73. * @param name the name
  74. * @param value the value
  75. */
  76. public void put(String name, int value) {
  77. if (!entries.containsKey(name)) {
  78. this.order.add(name);
  79. }
  80. this.entries.put(name, value);
  81. }
  82. /**
  83. * Returns the value given a name.
  84. * @param name the name of the value
  85. * @return the value or null, if there's no value with the given name.
  86. */
  87. public Object get(String name) {
  88. return this.entries.get(name);
  89. }
  90. /** {@inheritDoc} */
  91. @Override
  92. public int output(OutputStream stream) throws IOException {
  93. CountingOutputStream cout = new CountingOutputStream(stream);
  94. StringBuilder textBuffer = new StringBuilder(64);
  95. writeDictionary(cout, textBuffer);
  96. PDFDocument.flushTextBuffer(textBuffer, cout);
  97. return cout.getCount();
  98. }
  99. /**
  100. * Writes the contents of the dictionary to a StringBuffer.
  101. * @param out the OutputStream (for binary content)
  102. * @param textBuffer the text buffer for text output
  103. * @throws IOException if an I/O error occurs
  104. */
  105. protected void writeDictionary(OutputStream out, StringBuilder textBuffer) throws IOException {
  106. textBuffer.append("<<");
  107. boolean compact = (this.order.size() <= 2);
  108. for (String key : this.order) {
  109. if (compact) {
  110. textBuffer.append(' ');
  111. } else {
  112. textBuffer.append("\n ");
  113. }
  114. textBuffer.append(PDFName.escapeName(key));
  115. textBuffer.append(' ');
  116. Object obj = this.entries.get(key);
  117. formatObject(obj, out, textBuffer);
  118. }
  119. if (compact) {
  120. textBuffer.append(' ');
  121. } else {
  122. textBuffer.append('\n');
  123. }
  124. textBuffer.append(">>");
  125. }
  126. @Override
  127. public void getChildren(Set<PDFObject> children) {
  128. if (!visited) {
  129. visited = true;
  130. Map<String, Object> childrenMap = new HashMap<String, Object>(entries);
  131. childrenMap.remove("Parent");
  132. getChildren(childrenMap.values(), children);
  133. visited = false;
  134. }
  135. }
  136. public static void getChildren(Collection<Object> values, Set<PDFObject> children) {
  137. for (Object x : values) {
  138. if (x instanceof PDFReference) {
  139. x = ((PDFReference) x).getObject();
  140. }
  141. if (x instanceof PDFObject) {
  142. if (((PDFObject) x).hasObjectNumber()) {
  143. children.add((PDFObject) x);
  144. }
  145. ((PDFObject) x).getChildren(children);
  146. }
  147. }
  148. }
  149. public Set<String> keySet() {
  150. return entries.keySet();
  151. }
  152. /**
  153. * @see java.util.Map#containsKey(Object)
  154. *
  155. * @param name The key to find in the map.
  156. * @return true if the map contains this key.
  157. */
  158. public boolean containsKey(String name) {
  159. return this.entries.containsKey(name);
  160. }
  161. /**
  162. * Removes the mapping for the specified key
  163. * @param name key whose mapping is to be removed
  164. */
  165. public void remove(String name) {
  166. entries.remove(name);
  167. }
  168. }