KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > etymon > pjx > PdfDictionary


1 /*
2   Copyright (C) Etymon Systems, Inc. <http://www.etymon.com/>
3 */

4
5 package com.etymon.pjx;
6
7 import java.io.*;
8 import java.util.*;
9
10 /**
11    Represents the PDF dictionary object.
12    @author Nassib Nassar
13 */

14 public class PdfDictionary
15     extends PdfObject {
16
17     /**
18        The contents of the dictionary.
19     */

20     protected Map _m;
21
22     /**
23        A protected constructor intended to be called only from
24        {@link #wrap(Map) wrap(Map)}.
25      */

26     protected PdfDictionary() {
27     }
28
29     /**
30        Constructs a dictionary object from a map of
31        <code>PdfName</code> keys and <code>PdfObject</code>
32        values.
33        @param m the map containing the keys and values.
34      */

35     public PdfDictionary(Map m) {
36         _m = Collections.unmodifiableMap(new HashMap(m));
37     }
38
39     protected PdfObject filterContents(PdfObjectFilter f) throws PdfFormatException {
40         Map newMap = new HashMap(_m.size());
41         for (Iterator t = _m.keySet().iterator(); t.hasNext(); ) {
42             
43             Object JavaDoc obj = t.next();
44             if ( !(obj instanceof PdfObject) ) {
45                 throw new PdfFormatException("Dictionary key is not a PDF object.");
46             }
47             PdfObject key = (PdfObject)obj;
48             PdfObject keyF = key.filter(f);
49             
50             if (keyF != null) {
51                 obj = _m.get(key);
52                 if ( !(obj instanceof PdfObject) ) {
53                     throw new PdfFormatException("Dictionary value is not a PDF object.");
54                 }
55                 PdfObject valueF = ((PdfObject)obj).filter(f);
56                 
57                 if (valueF != null) {
58                     newMap.put(key, ((PdfObject)obj).filter(f));
59                 }
60             }
61             
62         }
63         return f.postFilter(new PdfDictionary(newMap));
64     }
65     
66     public boolean equals(Object JavaDoc obj) {
67
68         if ( (obj == null) || ( !(obj instanceof PdfDictionary) ) ) {
69             return false;
70         }
71
72         return _m.equals( ((PdfDictionary)obj)._m );
73     }
74
75     /**
76        Returns the map of keys and values contained in this
77        dictionary.
78        @return the map of keys and values. The returned
79        <code>Map</code> object is unmodifiable.
80      */

81     public Map getMap() {
82         return _m;
83     }
84
85     public int hashCode() {
86         return _m.hashCode();
87     }
88
89     /**
90        A factory for fast construction of this class. The
91        constructed object will be a wrapper around the specified
92        <code>Map</code>. The calling method must ensure that the
93        <code>Map</code> is never externally modified, in order to
94        meet the immutability requirement of {@link PdfObject
95        PdfObject}.
96        @param m the <code>Map</code> to be used to back this
97        dictionary.
98        @return the constructed object.
99      */

100     protected static PdfDictionary wrap(Map m) {
101         PdfDictionary pd = new PdfDictionary();
102         pd._m = Collections.unmodifiableMap(m);
103         return pd;
104     }
105     
106     protected int writePdf(PdfWriter w, boolean spacing) throws IOException {
107
108         DataOutputStream dos = w.getDataOutputStream();
109
110         int count = 4; // for << and >>
111

112         dos.writeBytes("<<");
113
114         boolean first = true;
115         for (Iterator p = _m.keySet().iterator(); p.hasNext(); ) {
116             PdfName key = (PdfName)p.next();
117             PdfObject value = (PdfObject)_m.get(key);
118             if (first) {
119                 count += key.writePdf(w, false);
120                 first = false;
121             } else {
122                 count += key.writePdf(w, true);
123             }
124             count += value.writePdf(w, true);
125         }
126         
127         dos.writeBytes(">>");
128
129         return count;
130         
131     }
132
133 }
134
Popular Tags