001: package com.etymon.pj.object;
002:
003: import java.io.*;
004: import java.util.*;
005: import com.etymon.pj.*;
006: import com.etymon.pj.exception.*;
007:
008: /**
009: A representation of the PDF dictionary type.
010: @author Nassib Nassar
011: */
012: public class PjDictionary extends PjObject {
013:
014: /**
015: Creates an empty dictionary.
016: */
017: public PjDictionary() {
018: _h = new Hashtable();
019: }
020:
021: /**
022: Creates a dictionary as a wrapper around a Hashtable.
023: @param h the Hashtable to use for this dictionary.
024: */
025: public PjDictionary(Hashtable h) {
026: _h = h;
027: }
028:
029: /**
030: Returns the Hashtable used to represent this dictionary.
031: @return the Hashtable used to represent this dictionary.
032: */
033: public Hashtable getHashtable() {
034: return _h;
035: }
036:
037: /**
038: Writes this dictionary to a stream in PDF format.
039: @param os the stream to write to.
040: @return the number of bytes written.
041: @exception IOException if an I/O error occurs.
042: */
043: public long writePdf(OutputStream os) throws IOException {
044: long z = writeln(os, "<<");
045: PjName name;
046: for (Enumeration enum = _h.keys();
047: enum.hasMoreElements();) {
048: name = ((PjName)(enum.nextElement()));
049: z = z + name.writePdf(os);
050: z = z + write(os, " ");
051: z = z + ((PjObject)(_h.get(name))).writePdf(os);
052: z = z + writeln(os, "");
053: }
054: z = z + write(os, ">>");
055: return z;
056: }
057:
058: /**
059: Returns a string representation of this dictionary in PDF format.
060: @return the string representation.
061: public String toString() {
062: StringBuffer sb = new StringBuffer("<<");
063: sb.append(PjConst.PDF_EOL);
064: PjName name;
065: for (Enumeration enum = _h.keys();
066: enum.hasMoreElements();) {
067: name = ((PjName)(enum.nextElement()));
068: sb.append(name.toString());
069: sb.append(" ");
070: sb.append(((PjObject)(_h.get(name))).toString());
071: sb.append(PjConst.PDF_EOL);
072: }
073: sb.append(">>");
074: return sb.toString();
075: }
076: */
077:
078: /**
079: Returns a deep copy of this object.
080: @return a deep copy of this object.
081: @exception CloneNotSupportedException if the instance can not be cloned.
082: */
083: public Object clone() throws CloneNotSupportedException {
084: return new PjDictionary(cloneHt());
085: }
086:
087: protected Hashtable cloneHt() throws CloneNotSupportedException {
088: Hashtable ht = new Hashtable(Math.max(_h.size(), 1));
089: Object key;
090: Object value;
091: for (Enumeration m = _h.keys(); m.hasMoreElements();) {
092: key = m.nextElement();
093: value = _h.get(key);
094: if (value instanceof PjObject) {
095: ht.put(key, ((PjObject) value).clone());
096: } else {
097: throw new CloneNotSupportedException(
098: "Object in dictionary is not a PjObject.");
099: }
100: }
101: return ht;
102: }
103:
104: /**
105: Renumbers object references within this object. This
106: method calls itself recursively to comprehensively renumber
107: all objects contained within this object.
108: @param map the table of object number mappings. Each
109: object number is looked up by key in the hash table, and
110: the associated value is assigned as the new object number.
111: The map hash table should consist of PjNumber keys and
112: PjReference values.
113: */
114: public void renumber(Hashtable map) {
115: Object key;
116: PjObject obj;
117: Object r;
118: for (Enumeration m = _h.keys(); m.hasMoreElements();) {
119: key = m.nextElement();
120: try {
121: obj = (PjObject) (_h.get(key));
122: if (obj instanceof PjReference) {
123: r = map.get(((PjReference) obj).getObjNumber());
124: if (r != null) {
125: _h.put(key, r);
126: }
127: } else {
128: obj.renumber(map);
129: }
130: } catch (ClassCastException e) {
131: // ignore bad objects
132: }
133: }
134: }
135:
136: protected PjObject hget(PjName name)
137: throws InvalidPdfObjectException {
138: Object obj = _h.get(name);
139: if (obj != null) {
140: if (obj instanceof PjObject) {
141: return (PjObject) obj;
142: } else {
143: throw new InvalidPdfObjectException(name.getString()
144: + " is not a PDF object.");
145: }
146: } else {
147: return null;
148: }
149: }
150:
151: protected PjReference hgetReference(PjName name)
152: throws InvalidPdfObjectException {
153: Object obj = _h.get(name);
154: if (obj != null) {
155: if (obj instanceof PjReference) {
156: return (PjReference) obj;
157: } else {
158: throw new InvalidPdfObjectException(name.getString()
159: + " is not a reference.");
160: }
161: } else {
162: return null;
163: }
164: }
165:
166: protected Hashtable _h;
167:
168: }
|