KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > stefanochizzolini > clown > objects > PdfDictionary


1 /*
2   Copyright © 2006 Stefano Chizzolini. http://clown.stefanochizzolini.it
3
4   Contributors:
5     * Stefano Chizzolini (original code developer, info@stefanochizzolini.it):
6       contributed code is Copyright © 2006 by Stefano Chizzolini.
7
8   This file should be part of the source code distribution of "PDF Clown library"
9   (the Program): see the accompanying README files for more info.
10
11   This Program is free software; you can redistribute it and/or modify it under
12   the terms of the GNU General Public License as published by the Free Software
13   Foundation; either version 2 of the License, or (at your option) any later version.
14
15   This Program is distributed in the hope that it will be useful, but WITHOUT ANY
16   WARRANTY, either expressed or implied; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more details.
18
19   You should have received a copy of the GNU General Public License along with this
20   Program (see README files); if not, go to the GNU website (http://www.gnu.org/).
21
22   Redistribution and use, with or without modification, are permitted provided that such
23   redistributions retain the above copyright notice, license and disclaimer, along with
24   this list of conditions.
25 */

26
27 package it.stefanochizzolini.clown.objects;
28
29 import it.stefanochizzolini.clown.bytes.IOutputStream;
30 import it.stefanochizzolini.clown.files.File;
31 import it.stefanochizzolini.clown.util.NotImplementedException;
32
33 import java.util.Collection JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.Set JavaDoc;
37
38 /**
39   PDF dictionary object.
40 */

41 public class PdfDictionary
42   extends PdfDirectObject
43   implements Map JavaDoc<PdfName,PdfDirectObject>
44 {
45   // <class>
46
// <dynamic>
47
// <fields>
48
private HashMap JavaDoc<PdfName,PdfDirectObject> entries;
49   // </fields>
50

51   // <constructors>
52
public PdfDictionary(
53     )
54   {entries = new HashMap JavaDoc<PdfName,PdfDirectObject>();}
55
56   public PdfDictionary(
57     int capacity
58     )
59   {entries = new HashMap JavaDoc<PdfName,PdfDirectObject>(capacity);}
60
61   public PdfDictionary(
62     PdfName[] keys,
63     PdfDirectObject[] values
64     )
65   {
66     this(values.length);
67
68     for(
69       int index = 0;
70       index < values.length;
71       index++
72       )
73     {
74       put(
75         keys[index],
76         values[index]
77         );
78     }
79   }
80   // </constructors>
81

82   // <interface>
83
// <public>
84
@Override JavaDoc
85   public Object JavaDoc clone(
86     File context
87     )
88   {
89     //TODO:IMPL redefine to support real cloning (current implementation is prone to object slicing hazard)!!!
90
PdfDictionary clone = new PdfDictionary(entries.size());
91
92     for(
93       Map.Entry JavaDoc<PdfName,PdfDirectObject> entry : entries.entrySet()
94       )
95     {
96       clone.put(
97         entry.getKey(),
98         (PdfDirectObject)entry.getValue().clone(context)
99         );
100     }
101
102     return clone;
103   }
104
105   /**
106     Gets the key associated to a given value.
107   */

108   public PdfName getKey(
109     PdfDirectObject value
110     )
111   {
112     /*
113       NOTE: Current PdfDictionary implementation doesn't support bidirectional
114       maps, to say that the only currently-available way to retrieve a key from a
115       value is to iterate the whole map (really poor performance!).
116       NOTE: Complex high-level matches are not verified (too expensive!), to say that
117       if the searched high-level object (font, xobject, colorspace etc.) has a
118       PdfReference base-object while some high-level objects in the
119       collection have other direct type (PdfName, for example) base-objects, they
120       won't match in any case (even if they represent the SAME high-level object --
121       but that should be a rare case...).
122     */

123     for(
124       Map.Entry JavaDoc<PdfName,PdfDirectObject> entry : entries.entrySet()
125       )
126     {
127       if(entry.getValue().equals(value))
128         return entry.getKey(); // Found.
129
}
130
131     return null; // Not found.
132
}
133
134   @Override JavaDoc
135   public String JavaDoc toPdf(
136     )
137   {throw new NotImplementedException();}
138
139   @Override JavaDoc
140   public String JavaDoc toString(
141     )
142   {
143     StringBuilder JavaDoc buffer = new StringBuilder JavaDoc();
144
145     // Begin.
146
buffer.append("<< ");
147
148     // Entries.
149
for(
150       Map.Entry JavaDoc<PdfName,PdfDirectObject> entry : entries.entrySet()
151       )
152     {
153       // Entry...
154
// ...key.
155
buffer.append(entry.getKey().toString() + " ");
156
157       // ...value.
158
buffer.append(entry.getValue().toString() + " ");
159     }
160
161     // End.
162
buffer.append(">>");
163
164     return buffer.toString();
165   }
166
167   // <Map>
168
public void clear(
169     )
170   {entries.clear();}
171
172   public boolean containsKey(
173     Object JavaDoc key
174     )
175   {return entries.containsKey(key);}
176
177   public boolean containsValue(
178     Object JavaDoc value
179     )
180   {return entries.containsValue(value);}
181
182   public Set JavaDoc<Map.Entry JavaDoc<PdfName,PdfDirectObject>> entrySet(
183     )
184   {return entries.entrySet();}
185
186   public boolean equals(
187     Object JavaDoc object
188     )
189   {throw new NotImplementedException();}
190
191   public PdfDirectObject get(
192     Object JavaDoc key
193     )
194   {return entries.get(key);}
195
196   public int hashCode(
197     )
198   {return entries.hashCode();}
199
200   public boolean isEmpty(
201     )
202   {return entries.isEmpty();}
203
204   public Set JavaDoc<PdfName> keySet(
205     )
206   {return entries.keySet();}
207
208   public PdfDirectObject put(
209     PdfName key,
210     PdfDirectObject value
211     )
212   {return entries.put(key,value);}
213
214   public void putAll(
215     Map JavaDoc<? extends PdfName,? extends PdfDirectObject> entries
216     )
217   {this.entries.putAll(entries);}
218
219   public PdfDirectObject remove(
220     Object JavaDoc key
221     )
222   {return entries.remove(key);}
223
224   public int size(
225     )
226   {return entries.size();}
227
228   public Collection JavaDoc<PdfDirectObject> values(
229     )
230   {return entries.values();}
231   // </Map>
232
// </public>
233

234   // <internal>
235
/**
236     <h3>Remarks</h3>
237     <p>For internal use only.</p>
238   */

239   @Override JavaDoc
240   public int writeTo(
241     IOutputStream stream
242     )
243   {
244     // Begin.
245
int size = stream.write("<< ");
246
247     // Entries.
248
for(
249       Map.Entry JavaDoc<PdfName,PdfDirectObject> entry : entries.entrySet()
250       )
251     {
252       // Entry...
253
// ...key.
254
size += entry.getKey().writeTo(stream);
255       size += stream.write(" ");
256
257       // ...value.
258
size += entry.getValue().writeTo(stream);
259       size += stream.write(" ");
260     }
261
262     // End.
263
size += stream.write(">>");
264
265     return size;
266   }
267   // </internal>
268
// </interface>
269
// </dynamic>
270
// </class>
271
}
Popular Tags