1 /*
2 * $Id: PdfDictionary.java 3373 2008-05-12 16:21:24Z xlv $
3 *
4 * Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * (the "License"); you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the License.
13 *
14 * The Original Code is 'iText, a free JAVA-PDF library'.
15 *
16 * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
17 * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
18 * All Rights Reserved.
19 * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
20 * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
21 *
22 * Contributor(s): all the names of the contributors are added in the source code
23 * where applicable.
24 *
25 * Alternatively, the contents of this file may be used under the terms of the
26 * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
27 * provisions of LGPL are applicable instead of those above. If you wish to
28 * allow use of your version of this file only under the terms of the LGPL
29 * License and not to allow others to use your version of this file under
30 * the MPL, indicate your decision by deleting the provisions above and
31 * replace them with the notice and other provisions required by the LGPL.
32 * If you do not delete the provisions above, a recipient may use your version
33 * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
34 *
35 * This library is free software; you can redistribute it and/or modify it
36 * under the terms of the MPL as stated above or under the terms of the GNU
37 * Library General Public License as published by the Free Software Foundation;
38 * either version 2 of the License, or any later version.
39 *
40 * This library is distributed in the hope that it will be useful, but WITHOUT
41 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
42 * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
43 * details.
44 *
45 * If you didn't download this code from the following link, you should check if
46 * you aren't using an obsolete version:
47 * http://www.lowagie.com/iText/
48 */
49
50 package com.lowagie.text.pdf;
51
52 import java.io.IOException;
53 import java.io.OutputStream;
54 import java.util.HashMap;
55 import java.util.Iterator;
56 import java.util.Set;
57
58 /**
59 * <CODE>PdfDictionary</CODE> is the Pdf dictionary object.
60 * <P>
61 * A dictionary is an associative table containing pairs of objects. The first element
62 * of each pair is called the <I>key</I> and the second element is called the <I>value</I>.
63 * Unlike dictionaries in the PostScript language, a key must be a <CODE>PdfName</CODE>.
64 * A value can be any kind of <CODE>PdfObject</CODE>, including a dictionary. A dictionary is
65 * generally used to collect and tie together the attributes of a complex object, with each
66 * key-value pair specifying the name and value of an attribute.<BR>
67 * A dictionary is represented by two left angle brackets (<<), followed by a sequence of
68 * key-value pairs, followed by two right angle brackets (>>).<BR>
69 * This object is described in the 'Portable Document Format Reference Manual version 1.7'
70 * section 3.2.6 (page 59-60).
71 * <P>
72 *
73 * @see PdfObject
74 * @see PdfName
75 * @see BadPdfFormatException
76 */
77
78 public class PdfDictionary extends PdfObject {
79
80 // static membervariables (types of dictionary's)
81
82 /** This is a possible type of dictionary */
83 public static final PdfName FONT = PdfName.FONT;
84
85 /** This is a possible type of dictionary */
86 public static final PdfName OUTLINES = PdfName.OUTLINES;
87
88 /** This is a possible type of dictionary */
89 public static final PdfName PAGE = PdfName.PAGE;
90
91 /** This is a possible type of dictionary */
92 public static final PdfName PAGES = PdfName.PAGES;
93
94 /** This is a possible type of dictionary */
95 public static final PdfName CATALOG = PdfName.CATALOG;
96
97 // membervariables
98
99 /** This is the type of this dictionary */
100 private PdfName dictionaryType = null;
101
102 /** This is the hashmap that contains all the values and keys of the dictionary */
103 protected HashMap hashMap;
104
105 // constructors
106
107 /**
108 * Constructs an empty <CODE>PdfDictionary</CODE>-object.
109 */
110
111 public PdfDictionary() {
112 super(DICTIONARY);
113 hashMap = new HashMap();
114 }
115
116 /**
117 * Constructs a <CODE>PdfDictionary</CODE>-object of a certain type.
118 *
119 * @param type a <CODE>PdfName</CODE>
120 */
121
122 public PdfDictionary(PdfName type) {
123 this();
124 dictionaryType = type;
125 put(PdfName.TYPE, dictionaryType);
126 }
127
128 // methods overriding some methods in PdfObject
129
130 /**
131 * Returns the PDF representation of this <CODE>PdfDictionary</CODE>.
132 */
133
134 public void toPdf(PdfWriter writer, OutputStream os) throws IOException {
135 os.write('<');
136 os.write('<');
137
138 // loop over all the object-pairs in the HashMap
139 PdfName key;
140 PdfObject value;
141 int type = 0;
142 for (Iterator i = hashMap.keySet().iterator(); i.hasNext(); ) {
143 key = (PdfName) i.next();
144 value = (PdfObject) hashMap.get(key);
145 key.toPdf(writer, os);
146 type = value.type();
147 if (type != PdfObject.ARRAY && type != PdfObject.DICTIONARY && type != PdfObject.NAME && type != PdfObject.STRING)
148 os.write(' ');
149 value.toPdf(writer, os);
150 }
151 os.write('>');
152 os.write('>');
153 }
154
155 // methods concerning the HashMap member value
156
157 /**
158 * Adds a <CODE>PdfObject</CODE> and its key to the <CODE>PdfDictionary</CODE>.
159 * If the value is <CODE>null</CODE> or <CODE>PdfNull</CODE> the key is deleted.
160 *
161 * @param key key of the entry (a <CODE>PdfName</CODE>)
162 * @param value value of the entry (a <CODE>PdfObject</CODE>)
163 */
164
165 public void put(PdfName key, PdfObject value) {
166 if (value == null || value.isNull())
167 hashMap.remove(key);
168 else
169 hashMap.put(key, value);
170 }
171
172 /**
173 * Adds a <CODE>PdfObject</CODE> and its key to the <CODE>PdfDictionary</CODE>.
174 * If the value is null it does nothing.
175 *
176 * @param key key of the entry (a <CODE>PdfName</CODE>)
177 * @param value value of the entry (a <CODE>PdfObject</CODE>)
178 */
179 public void putEx(PdfName key, PdfObject value) {
180 if (value == null)
181 return;
182 put(key, value);
183 }
184
185 /**
186 * Removes a <CODE>PdfObject</CODE> and its key from the <CODE>PdfDictionary</CODE>.
187 *
188 * @param key key of the entry (a <CODE>PdfName</CODE>)
189 */
190
191 public void remove(PdfName key) {
192 hashMap.remove(key);
193 }
194
195 /**
196 * Gets a <CODE>PdfObject</CODE> with a certain key from the <CODE>PdfDictionary</CODE>.
197 *
198 * @param key key of the entry (a <CODE>PdfName</CODE>)
199 * @return the previous </CODE>PdfObject</CODE> corresponding with the <VAR>key</VAR>
200 */
201
202 public PdfObject get(PdfName key) {
203 return (PdfObject) hashMap.get(key);
204 }
205
206 // methods concerning the type of Dictionary
207
208 /**
209 * Checks if a <CODE>Dictionary</CODE> is of the type FONT.
210 *
211 * @return <CODE>true</CODE> if it is, <CODE>false</CODE> if it isn't.
212 */
213
214 public boolean isFont() {
215 return FONT.equals(dictionaryType);
216 }
217
218 /**
219 * Checks if a <CODE>Dictionary</CODE> is of the type PAGE.
220 *
221 * @return <CODE>true</CODE> if it is, <CODE>false</CODE> if it isn't.
222 */
223
224 public boolean isPage() {
225 return PAGE.equals(dictionaryType);
226 }
227
228 /**
229 * Checks if a <CODE>Dictionary</CODE> is of the type PAGES.
230 *
231 * @return <CODE>true</CODE> if it is, <CODE>false</CODE> if it isn't.
232 */
233
234 public boolean isPages() {
235 return PAGES.equals(dictionaryType);
236 }
237
238 /**
239 * Checks if a <CODE>Dictionary</CODE> is of the type CATALOG.
240 *
241 * @return <CODE>true</CODE> if it is, <CODE>false</CODE> if it isn't.
242 */
243
244 public boolean isCatalog() {
245 return CATALOG.equals(dictionaryType);
246 }
247
248 /**
249 * Checks if a <CODE>Dictionary</CODE> is of the type OUTLINES.
250 *
251 * @return <CODE>true</CODE> if it is, <CODE>false</CODE> if it isn't.
252 */
253
254 public boolean isOutlineTree() {
255 return OUTLINES.equals(dictionaryType);
256 }
257
258 public void merge(PdfDictionary other) {
259 hashMap.putAll(other.hashMap);
260 }
261
262 public void mergeDifferent(PdfDictionary other) {
263 for (Iterator i = other.hashMap.keySet().iterator(); i.hasNext();) {
264 Object key = i.next();
265 if (!hashMap.containsKey(key)) {
266 hashMap.put(key, other.hashMap.get(key));
267 }
268 }
269 }
270
271 public Set getKeys() {
272 return hashMap.keySet();
273 }
274
275 public void putAll(PdfDictionary dic) {
276 hashMap.putAll(dic.hashMap);
277 }
278
279 public int size() {
280 return hashMap.size();
281 }
282
283 public boolean contains(PdfName key) {
284 return hashMap.containsKey(key);
285 }
286
287 /**
288 * @see com.lowagie.text.pdf.PdfObject#toString()
289 */
290 public String toString() {
291 if (get(PdfName.TYPE) == null) return "Dictionary";
292 return "Dictionary of type: " + get(PdfName.TYPE);
293 }
294
295 /**
296 * This function behaves the same as 'get', but will never return an indirect reference,
297 * it will always look such references up and return the actual object.
298 * @param key
299 * @return null, or a non-indirect object
300 */
301 public PdfObject getDirectObject(PdfName key) {
302 return PdfReader.getPdfObject(get(key));
303 }
304
305 /**
306 * All the getAs functions will return either null, or the specified object type
307 * This function will automatically look up indirect references. There's one obvious
308 * exception, the one that will only return an indirect reference. All direct objects
309 * come back as a null.
310 * Mark A Storer (2/17/06)
311 * @param key
312 * @return the appropriate object in its final type, or null
313 */
314 public PdfDictionary getAsDict(PdfName key) {
315 PdfDictionary dict = null;
316 PdfObject orig = getDirectObject(key);
317 if (orig != null && orig.isDictionary())
318 dict = (PdfDictionary) orig;
319 return dict;
320 }
321
322 public PdfArray getAsArray(PdfName key) {
323 PdfArray array = null;
324 PdfObject orig = getDirectObject(key);
325 if (orig != null && orig.isArray())
326 array = (PdfArray) orig;
327 return array;
328 }
329
330 public PdfStream getAsStream(PdfName key) {
331 PdfStream stream = null;
332 PdfObject orig = getDirectObject(key);
333 if (orig != null && orig.isStream())
334 stream = (PdfStream) orig;
335 return stream;
336 }
337
338 public PdfString getAsString(PdfName key) {
339 PdfString string = null;
340 PdfObject orig = getDirectObject(key);
341 if (orig != null && orig.isString())
342 string = (PdfString) orig;
343 return string;
344 }
345
346 public PdfNumber getAsNumber(PdfName key) {
347 PdfNumber number = null;
348 PdfObject orig = getDirectObject(key);
349 if (orig != null && orig.isNumber())
350 number = (PdfNumber) orig;
351 return number;
352 }
353
354 public PdfName getAsName(PdfName key) {
355 PdfName name = null;
356 PdfObject orig = getDirectObject(key);
357 if (orig != null && orig.isName())
358 name = (PdfName) orig;
359 return name;
360 }
361
362 public PdfBoolean getAsBoolean(PdfName key) {
363 PdfBoolean bool = null;
364 PdfObject orig = getDirectObject(key);
365 if (orig != null && orig.isBoolean())
366 bool = (PdfBoolean)orig;
367 return bool;
368 }
369
370 public PdfIndirectReference getAsIndirectObject( PdfName key ) {
371 PdfIndirectReference ref = null;
372 PdfObject orig = get(key); // not getDirect this time.
373 if (orig != null && orig.isIndirect())
374 ref = (PdfIndirectReference) orig;
375 return ref;
376 }
377 }