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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.Iterator;
  23. import java.util.List;
  24. import java.util.Map;
  25. import org.apache.commons.io.output.CountingOutputStream;
  26. /**
  27. * Class representing a PDF dictionary object
  28. */
  29. public class PDFDictionary extends PDFObject {
  30. /**
  31. * the entry map
  32. */
  33. protected Map entries = new java.util.HashMap();
  34. /**
  35. * maintains the order of the entries added to the entry map. Whenever you modify
  36. * "entries", always make sure you adjust this list accordingly.
  37. */
  38. protected List order = new java.util.ArrayList();
  39. /**
  40. * Create a new dictionary object.
  41. */
  42. public PDFDictionary() {
  43. super();
  44. }
  45. /**
  46. * Create a new dictionary object.
  47. * @param parent the object's parent if any
  48. */
  49. public PDFDictionary(PDFObject parent) {
  50. super(parent);
  51. }
  52. /**
  53. * Puts a new name/value pair.
  54. * @param name the name
  55. * @param value the value
  56. */
  57. public void put(String name, Object value) {
  58. if (value instanceof PDFObject) {
  59. PDFObject pdfObj = (PDFObject)value;
  60. if (!pdfObj.hasObjectNumber()) {
  61. pdfObj.setParent(this);
  62. }
  63. }
  64. if (!entries.containsKey(name)) {
  65. this.order.add(name);
  66. }
  67. this.entries.put(name, value);
  68. }
  69. /**
  70. * Puts a new name/value pair.
  71. * @param name the name
  72. * @param value the value
  73. */
  74. public void put(String name, int value) {
  75. if (!entries.containsKey(name)) {
  76. this.order.add(name);
  77. }
  78. this.entries.put(name, new Integer(value));
  79. }
  80. /**
  81. * Returns the value given a name.
  82. * @param name the name of the value
  83. * @return the value or null, if there's no value with the given name.
  84. */
  85. public Object get(String name) {
  86. return this.entries.get(name);
  87. }
  88. /** {@inheritDoc} */
  89. protected int output(OutputStream stream) throws IOException {
  90. CountingOutputStream cout = new CountingOutputStream(stream);
  91. Writer writer = PDFDocument.getWriterFor(cout);
  92. if (hasObjectNumber()) {
  93. writer.write(getObjectID());
  94. }
  95. writeDictionary(cout, writer);
  96. if (hasObjectNumber()) {
  97. writer.write("\nendobj\n");
  98. }
  99. writer.flush();
  100. return cout.getCount();
  101. }
  102. /**
  103. * Writes the contents of the dictionary to a StringBuffer.
  104. * @param out the OutputStream (for binary content)
  105. * @param writer the Writer (for text content, wraps the above OutputStream)
  106. * @throws IOException if an I/O error occurs
  107. */
  108. protected void writeDictionary(OutputStream out, Writer writer) throws IOException {
  109. writer.write("<<");
  110. boolean compact = (this.order.size() <= 2);
  111. Iterator iter = this.order.iterator();
  112. while (iter.hasNext()) {
  113. String key = (String)iter.next();
  114. if (compact) {
  115. writer.write(' ');
  116. } else {
  117. writer.write("\n ");
  118. }
  119. writer.write('/');
  120. writer.write(key);
  121. writer.write(' ');
  122. Object obj = this.entries.get(key);
  123. formatObject(obj, out, writer);
  124. }
  125. if (compact) {
  126. writer.write(' ');
  127. } else {
  128. writer.write('\n');
  129. }
  130. writer.write(">>\n");
  131. }
  132. }