com.aowagie.text.pdf.PdfDictionary Maven / Gradle / Ivy
Show all versions of afirma-lib-itext-android Show documentation
/*
* $Id: PdfDictionary.java 3762 2009-03-06 16:53:44Z blowagie $
*
* Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
package com.aowagie.text.pdf;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Set;
/**
* PdfDictionary
is the Pdf dictionary object.
*
* A dictionary is an associative table containing pairs of objects.
* The first element of each pair is called the key and the second
* element is called the value.
* Unlike dictionaries in the PostScript language, a key must be a
* PdfName
.
* A value can be any kind of PdfObject
, including a dictionary.
* A dictionary is generally used to collect and tie together the attributes
* of a complex object, with each key-value pair specifying the name and value
* of an attribute.
* A dictionary is represented by two left angle brackets (<<), followed by a
* sequence of key-value pairs, followed by two right angle brackets (>>).
* This object is described in the 'Portable Document Format Reference Manual
* version 1.7' section 3.2.6 (page 59-60).
*
* @see PdfObject
* @see PdfName
* @see BadPdfFormatException
*/
public class PdfDictionary extends PdfObject {
// CONSTANTS
/** This is a possible type of dictionary */
private static final PdfName FONT = PdfName.FONT;
/** This is a possible type of dictionary */
static final PdfName OUTLINES = PdfName.OUTLINES;
/** This is a possible type of dictionary */
static final PdfName PAGE = PdfName.PAGE;
/** This is a possible type of dictionary */
private static final PdfName PAGES = PdfName.PAGES;
/** This is a possible type of dictionary */
static final PdfName CATALOG = PdfName.CATALOG;
// CLASS VARIABLES
/** This is the type of this dictionary */
private PdfName dictionaryType = null;
/** This is the hashmap that contains all the values and keys of the dictionary */
protected HashMap hashMap;
// CONSTRUCTORS
/**
* Constructs an empty PdfDictionary
-object.
*/
public PdfDictionary() {
super(DICTIONARY);
this.hashMap = new LinkedHashMap();
}
/**
* Constructs a PdfDictionary
-object of a certain type.
*
* @param type a PdfName
*/
public PdfDictionary(final PdfName type) {
this();
this.dictionaryType = type;
put(PdfName.TYPE, this.dictionaryType);
}
// METHODS OVERRIDING SOME PDFOBJECT METHODS
/**
* Writes the PDF representation of this PdfDictionary
as an
* array of byte
to the given OutputStream
.
*
* @param writer for backwards compatibility
* @param os the OutputStream
to write the bytes to.
* @throws IOException
*/
@Override
public void toPdf(final PdfWriter writer, final OutputStream os) throws IOException {
os.write('<');
os.write('<');
// loop over all the object-pairs in the HashMap
PdfName key;
PdfObject value;
int type = 0;
for (final Object element : this.hashMap.keySet()) {
key = (PdfName) element;
value = this.hashMap.get(key);
key.toPdf(writer, os);
type = value.type();
if (type != PdfObject.ARRAY && type != PdfObject.DICTIONARY && type != PdfObject.NAME && type != PdfObject.STRING) {
os.write(' ');
}
value.toPdf(writer, os);
}
os.write('>');
os.write('>');
}
/**
* Returns a string representation of this PdfDictionary
.
*
* The string doesn't contain any of the content of this dictionary.
* Rather the string "dictionary" is returned, possibly followed by the
* type of this PdfDictionary
, if set.
*
* @return the string representation of this PdfDictionary
* @see com.aowagie.text.pdf.PdfObject#toString()
*/
@Override
public String toString() {
if (get(PdfName.TYPE) == null) {
return "Dictionary";
}
return "Dictionary of type: " + get(PdfName.TYPE);
}
// DICTIONARY CONTENT METHODS
/**
* Associates the specified PdfObject
as value with
* the specified PdfName
as key in this map.
*
* If the map previously contained a mapping for this key, the
* old value is replaced. If the value is
* null
or PdfNull
the key is deleted.
*
* @param key a PdfName
* @param object the PdfObject
to be associated with the
* key
*/
public void put(final PdfName key, final PdfObject object) {
if (object == null || object.isNull()) {
this.hashMap.remove(key);
} else {
this.hashMap.put(key, object);
}
}
/**
* Copies all of the mappings from the specified PdfDictionary
* to this PdfDictionary
.
*
* These mappings will replace any mappings previously contained in this
* PdfDictionary
.
*
* @param dic The PdfDictionary
with the mappings to be
* copied over
*/
void putAll(final PdfDictionary dic) {
this.hashMap.putAll(dic.hashMap);
}
/**
* Removes a PdfObject
and its key from the
* PdfDictionary
.
*
* @param key a PdfName
*/
public void remove(final PdfName key) {
this.hashMap.remove(key);
}
/**
* Returns the PdfObject
associated to the specified
* key.
*
* @param key a PdfName
* @return the PdfObject
previously associated to the
* key
*/
public PdfObject get(final PdfName key) {
return this.hashMap.get(key);
}
/**
* Returns the PdfObject
associated to the specified
* key, resolving a possible indirect reference to a direct
* object.
*
* This method will never return a PdfIndirectReference
* object.
*
* @param key A key for the PdfObject
to be returned
* @return A direct PdfObject
or null
*/
public PdfObject getDirectObject(final PdfName key) {
return PdfReader.getPdfObject(get(key));
}
/**
* Get all keys that are set.
*
* @return true
if it is, otherwise false
.
*/
public Set getKeys() {
return this.hashMap.keySet();
}
/**
* Returns the number of key-value mappings in this
* PdfDictionary
.
*
* @return the number of key-value mappings in this
* PdfDictionary
.
*/
public int size() {
return this.hashMap.size();
}
/**
* Returns true
if this PdfDictionary
contains a
* mapping for the specified key.
*
* @return true
if the key is set, otherwise false
.
*/
boolean contains(final PdfName key) {
return this.hashMap.containsKey(key);
}
// DICTIONARY TYPE METHODS
/**
* Checks if a Dictionary
is of the type FONT.
*
* @return true
if it is, otherwise false
.
*/
public boolean isFont() {
return FONT.equals(this.dictionaryType);
}
/**
* Checks if a Dictionary
is of the type PAGE.
*
* @return true
if it is, otherwise false
.
*/
public boolean isPage() {
return PAGE.equals(this.dictionaryType);
}
/**
* Checks if a Dictionary
is of the type PAGES.
*
* @return true
if it is, otherwise false
.
*/
public boolean isPages() {
return PAGES.equals(this.dictionaryType);
}
/**
* Checks if a Dictionary
is of the type CATALOG.
*
* @return true
if it is, otherwise false
.
*/
public boolean isCatalog() {
return CATALOG.equals(this.dictionaryType);
}
/**
* Checks if a Dictionary
is of the type OUTLINES.
*
* @return true
if it is, otherwise false
.
*/
public boolean isOutlineTree() {
return OUTLINES.equals(this.dictionaryType);
}
// OTHER METHODS
void merge(final PdfDictionary other) {
this.hashMap.putAll(other.hashMap);
}
void mergeDifferent(final PdfDictionary other) {
for (final PdfName key : other.hashMap.keySet()) {
if (!this.hashMap.containsKey(key)) {
this.hashMap.put(key, other.hashMap.get(key));
}
}
}
// DOWNCASTING GETTERS
// @author Mark A Storer (2/17/06)
/**
* Returns a PdfObject
as a PdfDictionary
,
* resolving indirect references.
*
* The object associated with the PdfName
given is retrieved
* and resolved to a direct object.
* If it is a PdfDictionary
, it is cast down and returned as
* such. Otherwise null
is returned.
*
* @param key A PdfName
* @return the associated PdfDictionary
object,
* or null
*/
public PdfDictionary getAsDict(final PdfName key) {
PdfDictionary dict = null;
final PdfObject orig = getDirectObject(key);
if (orig != null && orig.isDictionary()) {
dict = (PdfDictionary) orig;
}
return dict;
}
/**
* Returns a PdfObject
as a PdfArray
,
* resolving indirect references.
*
* The object associated with the PdfName
given is retrieved
* and resolved to a direct object.
* If it is a PdfArray
, it is cast down and returned as such.
* Otherwise null
is returned.
*
* @param key A PdfName
* @return the associated PdfArray
object,
* or null
*/
public PdfArray getAsArray(final PdfName key) {
PdfArray array = null;
final PdfObject orig = getDirectObject(key);
if (orig != null && orig.isArray()) {
array = (PdfArray) orig;
}
return array;
}
/**
* Returns a PdfObject
as a PdfString
,
* resolving indirect references.
*
* The object associated with the PdfName
given is retrieved
* and resolved to a direct object.
* If it is a PdfString
, it is cast down and returned as such.
* Otherwise null
is returned.
*
* @param key A PdfName
* @return the associated PdfString
object,
* or null
*/
public PdfString getAsString(final PdfName key) {
PdfString string = null;
final PdfObject orig = getDirectObject(key);
if (orig != null && orig.isString()) {
string = (PdfString) orig;
}
return string;
}
/**
* Returns a PdfObject
as a PdfNumber
,
* resolving indirect references.
*
* The object associated with the PdfName
given is retrieved
* and resolved to a direct object.
* If it is a PdfNumber
, it is cast down and returned as such.
* Otherwise null
is returned.
*
* @param key A PdfName
* @return the associated PdfNumber
object,
* or null
*/
public PdfNumber getAsNumber(final PdfName key) {
PdfNumber number = null;
final PdfObject orig = getDirectObject(key);
if (orig != null && orig.isNumber()) {
number = (PdfNumber) orig;
}
return number;
}
/**
* Returns a PdfObject
as a PdfName
,
* resolving indirect references.
*
* The object associated with the PdfName
given is retrieved
* and resolved to a direct object.
* If it is a PdfName
, it is cast down and returned as such.
* Otherwise null
is returned.
*
* @param key A PdfName
* @return the associated PdfName
object,
* or null
*/
public PdfName getAsName(final PdfName key) {
PdfName name = null;
final PdfObject orig = getDirectObject(key);
if (orig != null && orig.isName()) {
name = (PdfName) orig;
}
return name;
}
/**
* Returns a PdfObject
as a PdfBoolean
,
* resolving indirect references.
*
* The object associated with the PdfName
given is retrieved
* and resolved to a direct object.
* If it is a PdfBoolean
, it is cast down and returned as such.
* Otherwise null
is returned.
*
* @param key A PdfName
* @return the associated PdfBoolean
object,
* or null
*/
PdfBoolean getAsBoolean(final PdfName key) {
PdfBoolean bool = null;
final PdfObject orig = getDirectObject(key);
if (orig != null && orig.isBoolean()) {
bool = (PdfBoolean)orig;
}
return bool;
}
/**
* Returns a PdfObject
as a PdfIndirectReference
.
*
* The object associated with the PdfName
given is retrieved
* If it is a PdfIndirectReference
, it is cast down and returned
* as such. Otherwise null
is returned.
*
* @param key A PdfName
* @return the associated PdfIndirectReference
object,
* or null
*/
PdfIndirectReference getAsIndirectObject(final PdfName key) {
PdfIndirectReference ref = null;
final PdfObject orig = get(key); // not getDirect this time.
if (orig != null && orig.isIndirect()) {
ref = (PdfIndirectReference) orig;
}
return ref;
}
}