KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > PdfDictionary


1 /*
2  * $Id: PdfDictionary.java 2739 2007-05-04 11:24:51Z blowagie $
3  * $Name$
4  *
5  * Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50
51 package com.lowagie.text.pdf;
52
53 import java.io.IOException JavaDoc;
54 import java.io.OutputStream JavaDoc;
55 import java.util.HashMap JavaDoc;
56 import java.util.Iterator JavaDoc;
57 import java.util.Set JavaDoc;
58
59 /**
60  * <CODE>PdfDictionary</CODE> is the Pdf dictionary object.
61  * <P>
62  * A dictionary is an associative table containing pairs of objects. The first element
63  * of each pair is called the <I>key</I> and the second element is called the <I>value</I>.
64  * Unlike dictionaries in the PostScript language, a key must be a <CODE>PdfName</CODE>.
65  * A value can be any kind of <CODE>PdfObject</CODE>, including a dictionary. A dictionary is
66  * generally used to collect and tie together the attributes of a complex object, with each
67  * key-value pair specifying the name and value of an attribute.<BR>
68  * A dictionary is represented by two left angle brackets (<<), followed by a sequence of
69  * key-value pairs, followed by two right angle brackets (>>).<BR>
70  * This object is described in the 'Portable Document Format Reference Manual version 1.7'
71  * section 3.2.6 (page 59-60).
72  * <P>
73  *
74  * @see PdfObject
75  * @see PdfName
76  * @see BadPdfFormatException
77  */

78
79 public class PdfDictionary extends PdfObject {
80     
81     // static membervariables (types of dictionary's)
82

83 /** This is a possible type of dictionary */
84     public static final PdfName FONT = PdfName.FONT;
85     
86 /** This is a possible type of dictionary */
87     public static final PdfName OUTLINES = PdfName.OUTLINES;
88     
89 /** This is a possible type of dictionary */
90     public static final PdfName PAGE = PdfName.PAGE;
91     
92 /** This is a possible type of dictionary */
93     public static final PdfName PAGES = PdfName.PAGES;
94     
95 /** This is a possible type of dictionary */
96     public static final PdfName CATALOG = PdfName.CATALOG;
97     
98     // membervariables
99

100 /** This is the type of this dictionary */
101     private PdfName dictionaryType = null;
102     
103 /** This is the hashmap that contains all the values and keys of the dictionary */
104     protected HashMap JavaDoc hashMap;
105     
106     // constructors
107

108 /**
109  * Constructs an empty <CODE>PdfDictionary</CODE>-object.
110  */

111     
112     public PdfDictionary() {
113         super(DICTIONARY);
114         hashMap = new HashMap JavaDoc();
115     }
116     
117 /**
118  * Constructs a <CODE>PdfDictionary</CODE>-object of a certain type.
119  *
120  * @param type a <CODE>PdfName</CODE>
121  */

122     
123     public PdfDictionary(PdfName type) {
124         this();
125         dictionaryType = type;
126         put(PdfName.TYPE, dictionaryType);
127     }
128     
129     // methods overriding some methods in PdfObject
130

131 /**
132  * Returns the PDF representation of this <CODE>PdfDictionary</CODE>.
133  */

134     
135     public void toPdf(PdfWriter writer, OutputStream JavaDoc os) throws IOException JavaDoc {
136         os.write('<');
137         os.write('<');
138
139         // loop over all the object-pairs in the HashMap
140
PdfName key;
141         PdfObject value;
142         int type = 0;
143         for (Iterator JavaDoc i = hashMap.keySet().iterator(); i.hasNext(); ) {
144             key = (PdfName) i.next();
145             value = (PdfObject) hashMap.get(key);
146             key.toPdf(writer, os);
147             type = value.type();
148             if (type != PdfObject.ARRAY && type != PdfObject.DICTIONARY && type != PdfObject.NAME && type != PdfObject.STRING)
149                 os.write(' ');
150             value.toPdf(writer, os);
151         }
152         os.write('>');
153         os.write('>');
154     }
155     
156     // methods concerning the HashMap member value
157

158 /**
159  * Adds a <CODE>PdfObject</CODE> and its key to the <CODE>PdfDictionary</CODE>.
160  * If the value is <CODE>null</CODE> or <CODE>PdfNull</CODE> the key is deleted.
161  *
162  * @param key key of the entry (a <CODE>PdfName</CODE>)
163  * @param value value of the entry (a <CODE>PdfObject</CODE>)
164  */

165     
166     public void put(PdfName key, PdfObject value) {
167         if (value == null || value.isNull())
168             hashMap.remove(key);
169         else
170             hashMap.put(key, value);
171     }
172     
173 /**
174  * Adds a <CODE>PdfObject</CODE> and its key to the <CODE>PdfDictionary</CODE>.
175  * If the value is null it does nothing.
176  *
177  * @param key key of the entry (a <CODE>PdfName</CODE>)
178  * @param value value of the entry (a <CODE>PdfObject</CODE>)
179  */

180     public void putEx(PdfName key, PdfObject value) {
181         if (value == null)
182             return;
183         put(key, value);
184     }
185     
186 /**
187  * Removes a <CODE>PdfObject</CODE> and its key from the <CODE>PdfDictionary</CODE>.
188  *
189  * @param key key of the entry (a <CODE>PdfName</CODE>)
190  */

191     
192     public void remove(PdfName key) {
193         hashMap.remove(key);
194     }
195     
196 /**
197  * Gets a <CODE>PdfObject</CODE> with a certain key from the <CODE>PdfDictionary</CODE>.
198  *
199  * @param key key of the entry (a <CODE>PdfName</CODE>)
200  * @return the previous </CODE>PdfObject</CODE> corresponding with the <VAR>key</VAR>
201  */

202     
203     public PdfObject get(PdfName key) {
204         return (PdfObject) hashMap.get(key);
205     }
206     
207     // methods concerning the type of Dictionary
208

209 /**
210  * Checks if a <CODE>PdfDictionary</CODE> is of a certain type.
211  *
212  * @param type a type of dictionary
213  * @return <CODE>true</CODE> of <CODE>false</CODE>
214  *
215  * @deprecated
216  */

217     
218     public boolean isDictionaryType(PdfName type) {
219         return type.equals(dictionaryType);
220     }
221     
222 /**
223  * Checks if a <CODE>Dictionary</CODE> is of the type FONT.
224  *
225  * @return <CODE>true</CODE> if it is, <CODE>false</CODE> if it isn't.
226  */

227     
228     public boolean isFont() {
229         return FONT.equals(dictionaryType);
230     }
231     
232 /**
233  * Checks if a <CODE>Dictionary</CODE> is of the type PAGE.
234  *
235  * @return <CODE>true</CODE> if it is, <CODE>false</CODE> if it isn't.
236  */

237     
238     public boolean isPage() {
239         return PAGE.equals(dictionaryType);
240     }
241     
242 /**
243  * Checks if a <CODE>Dictionary</CODE> is of the type PAGES.
244  *
245  * @return <CODE>true</CODE> if it is, <CODE>false</CODE> if it isn't.
246  */

247     
248     public boolean isPages() {
249         return PAGES.equals(dictionaryType);
250     }
251     
252 /**
253  * Checks if a <CODE>Dictionary</CODE> is of the type CATALOG.
254  *
255  * @return <CODE>true</CODE> if it is, <CODE>false</CODE> if it isn't.
256  */

257     
258     public boolean isCatalog() {
259         return CATALOG.equals(dictionaryType);
260     }
261     
262 /**
263  * Checks if a <CODE>Dictionary</CODE> is of the type OUTLINES.
264  *
265  * @return <CODE>true</CODE> if it is, <CODE>false</CODE> if it isn't.
266  */

267     
268     public boolean isOutlineTree() {
269         return OUTLINES.equals(dictionaryType);
270     }
271     
272     public void merge(PdfDictionary other) {
273         hashMap.putAll(other.hashMap);
274     }
275     
276     public void mergeDifferent(PdfDictionary other) {
277         for (Iterator JavaDoc i = other.hashMap.keySet().iterator(); i.hasNext();) {
278             Object JavaDoc key = i.next();
279             if (!hashMap.containsKey(key)) {
280                 hashMap.put(key, other.hashMap.get(key));
281             }
282         }
283     }
284     
285     public Set JavaDoc getKeys() {
286         return hashMap.keySet();
287     }
288
289     public void putAll(PdfDictionary dic) {
290         hashMap.putAll(dic.hashMap);
291     }
292     
293     public int size() {
294         return hashMap.size();
295     }
296     
297     public boolean contains(PdfName key) {
298         return hashMap.containsKey(key);
299     }
300     
301     public String JavaDoc toString() {
302         return "Dictionary of type: " + get(PdfName.TYPE);
303     }
304     
305     /**
306      * This function behaves the same as 'get', but will never return an indirect reference,
307      * it will always look such references up and return the actual object.
308      * @param key
309      * @return null, or a non-indirect object
310      */

311     public PdfObject getDirectObject(PdfName key) {
312         return PdfReader.getPdfObject(get(key));
313     }
314     
315     /**
316      * All the getAs functions will return either null, or the specified object type
317      * This function will automatically look up indirect references. There's one obvious
318      * exception, the one that will only return an indirect reference. All direct objects
319      * come back as a null.
320      * Mark A Storer (2/17/06)
321      * @param key
322      * @return the appropriate object in its final type, or null
323      */

324     public PdfDictionary getAsDict(PdfName key) {
325         PdfDictionary dict = null;
326         PdfObject orig = getDirectObject(key);
327         if (orig != null && orig.isDictionary())
328             dict = (PdfDictionary) orig;
329         return dict;
330     }
331     
332     public PdfArray getAsArray(PdfName key) {
333         PdfArray array = null;
334         PdfObject orig = getDirectObject(key);
335         if (orig != null && orig.isArray())
336             array = (PdfArray) orig;
337         return array;
338     }
339     
340     public PdfStream getAsStream(PdfName key) {
341         PdfStream stream = null;
342         PdfObject orig = getDirectObject(key);
343         if (orig != null && orig.isStream())
344             stream = (PdfStream) orig;
345         return stream;
346     }
347     
348     public PdfString getAsString(PdfName key) {
349         PdfString string = null;
350         PdfObject orig = getDirectObject(key);
351         if (orig != null && orig.isString())
352             string = (PdfString) orig;
353         return string;
354     }
355     
356     public PdfNumber getAsNumber(PdfName key) {
357         PdfNumber number = null;
358         PdfObject orig = getDirectObject(key);
359         if (orig != null && orig.isNumber())
360             number = (PdfNumber) orig;
361         return number;
362     }
363     
364     public PdfName getAsName(PdfName key) {
365         PdfName name = null;
366         PdfObject orig = getDirectObject(key);
367         if (orig != null && orig.isName())
368             name = (PdfName) orig;
369         return name;
370     }
371     
372     public PdfBoolean getAsBoolean(PdfName key) {
373         PdfBoolean bool = null;
374         PdfObject orig = getDirectObject(key);
375         if (orig != null && orig.isBoolean())
376             bool = (PdfBoolean)orig;
377         return bool;
378     }
379     
380     public PdfIndirectReference getAsIndirectObject( PdfName key ) {
381         PdfIndirectReference ref = null;
382         PdfObject orig = get(key); // not getDirect this time.
383
if (orig != null && orig.isIndirect())
384             ref = (PdfIndirectReference) orig;
385         return ref;
386     }
387 }
Popular Tags