View Javadoc

1   package org.freehep.graphicsio.pdf;
2   
3   import java.io.IOException;
4   import java.util.Calendar;
5   
6   /**
7    * Implements a PDF Dictionary. All PDFObjects (including java Strings and
8    * arrays) can be entered into the dictionary.
9    * <p>
10   * 
11   * @author Mark Donszelmann
12   * @version $Id: PDFDictionary.java 8584 2006-08-10 23:06:37Z duns $
13   */
14  public class PDFDictionary implements PDFConstants {
15  
16      private String open = null;
17  
18      protected PDFByteWriter out;
19  
20      private boolean ok;
21  
22      private PDFObject object;
23  
24      protected PDF pdf;
25  
26      PDFDictionary(PDF pdf, PDFByteWriter writer) throws IOException {
27          this(pdf, writer, null);
28      }
29  
30      PDFDictionary(PDF pdf, PDFByteWriter writer, PDFObject parent)
31              throws IOException {
32          this.pdf = pdf;
33          object = parent;
34          out = writer;
35          out.println("<< ");
36          out.indent();
37          ok = true;
38      }
39  
40      void close() throws IOException {
41          if (open != null)
42              System.err
43                      .println("PDFWriter error: '" + open + "' was not closed");
44          out.outdent();
45          out.println(">>");
46          if (object != null)
47              object.close();
48          ok = false;
49      }
50  
51      public void entry(String key, String string) throws IOException {
52          if (!ok)
53              System.err.println("PDFWriter error: 'PDFDictionary' was closed");
54          out.println("/" + key + " (" + PDFUtil.escape(string) + ")");
55      }
56  
57      public void entry(String key, PDFName name) throws IOException {
58          if (!ok)
59              System.err.println("PDFWriter error: 'PDFDictionary' was closed");
60          out.println("/" + key + " " + name);
61      }
62  
63      public void entry(String key, int number) throws IOException {
64          if (!ok)
65              System.err.println("PDFWriter error: 'PDFDictionary' was closed");
66          out.println("/" + key + " " + number);
67      }
68  
69      public void entry(String key, double number) throws IOException {
70          if (!ok)
71              System.err.println("PDFWriter error: 'PDFDictionary' was closed");
72          out.println("/" + key + " " + PDFUtil.fixedPrecision(number));
73      }
74  
75      public void entry(String key, boolean bool) throws IOException {
76          if (!ok)
77              System.err.println("PDFWriter error: 'PDFDictionary' was closed");
78          out.println("/" + key + " " + (bool ? "true" : "false"));
79      }
80  
81      public void entry(String key, PDFRef ref) throws IOException {
82          if (!ok)
83              System.err.println("PDFWriter error: 'PDFDictionary' was closed");
84          out.println("/" + key + " " + ref);
85      }
86  
87      public void entry(String key, Calendar date) throws IOException {
88          if (!ok)
89              System.err.println("PDFWriter error: 'PDFDictionary' was closed");
90          out.println("/" + key + " " + PDFUtil.date(date));
91      }
92  
93      public void entry(String key, Object[] objs) throws IOException {
94          if (!ok)
95              System.err.println("PDFWriter error: 'PDFDictionary' was closed");
96          out.print("/" + key + " [");
97          for (int i = 0; i < objs.length; i++) {
98              if (i != 0)
99                  out.printPlain(" ");
100             out.printPlain(objs[i]);
101         }
102         out.printPlain("]");
103         out.println();
104     }
105 
106     public void entry(String key, int[] numbers) throws IOException {
107         if (!ok)
108             System.err.println("PDFWriter error: 'PDFDictionary' was closed");
109         out.print("/" + key + " [");
110         for (int i = 0; i < numbers.length; i++) {
111             if (i != 0)
112                 out.printPlain(" ");
113             out.printPlain(numbers[i]);
114         }
115         out.printPlain("]");
116         out.println();
117     }
118 
119     public void entry(String key, double[] numbers) throws IOException {
120         if (!ok)
121             System.err.println("PDFWriter error: 'PDFDictionary' was closed");
122         out.print("/" + key + " [");
123         for (int i = 0; i < numbers.length; i++) {
124             if (i != 0)
125                 out.printPlain(" ");
126             out.printPlain(PDFUtil.fixedPrecision(numbers[i]));
127         }
128         out.printPlain("]");
129         out.println();
130     }
131 
132     public void entry(String key, boolean[] bool) throws IOException {
133         if (!ok)
134             System.err.println("PDFWriter error: 'PDFDictionary' was closed");
135         out.print("/" + key + " [");
136         for (int i = 0; i < bool.length; i++) {
137             if (i != 0)
138                 out.printPlain(" ");
139             out.printPlain(bool[i] ? "true" : "false");
140         }
141         out.printPlain("]");
142         out.println();
143     }
144 
145     public PDFDictionary openDictionary(String name) throws IOException {
146         if (!ok)
147             System.err.println("PDFWriter error: 'PDFDictionary' was closed");
148         if (open != null)
149             System.err
150                     .println("PDFWriter error: '" + open + "' was not closed");
151         open = "PDFDictionary: " + name;
152         out.println("/" + name);
153         PDFDictionary dictionary = new PDFDictionary(pdf, out);
154         return dictionary;
155     }
156 
157     public void close(PDFDictionary dictionary) throws IOException {
158         dictionary.close();
159         open = null;
160     }
161 
162 }