001: package com.etymon.pj.object;
002:
003: import java.io.*;
004: import java.util.*;
005: import com.etymon.pj.*;
006:
007: /**
008: A representation of the PDF array type.
009: @author Nassib Nassar
010: */
011: public class PjArray extends PjObject {
012:
013: /**
014: Creates an empty array.
015: */
016: public PjArray() {
017: _v = new Vector();
018: }
019:
020: /**
021: Creates an array as a wrapper around a Vector.
022: @param v the Vector to use for this array.
023: */
024: public PjArray(Vector v) {
025: _v = v;
026: }
027:
028: /**
029: Returns the Vector used to represent this array.
030: @return the Vector used to represent this array.
031: */
032: public Vector getVector() {
033: return _v;
034: }
035:
036: /**
037: Writes this array to a stream in PDF format.
038: @param os the stream to write to.
039: @return the number of bytes written.
040: @exception IOException if an I/O error occurs.
041: */
042: public long writePdf(OutputStream os) throws IOException {
043: long z = writeln(os, "[");
044: int size = _v.size();
045: for (int x = 0; x < size; x++) {
046: z = z + ((PjObject) (_v.elementAt(x))).writePdf(os);
047: z = z + writeln(os, "");
048: }
049: z = z + write(os, "]");
050: return z;
051: }
052:
053: /**
054: Returns a deep copy of this object.
055: @return a deep copy of this object.
056: @exception CloneNotSupportedException if the instance can not be cloned.
057: */
058: public Object clone() throws CloneNotSupportedException {
059: return new PjArray(cloneVector());
060: }
061:
062: protected Vector cloneVector() throws CloneNotSupportedException {
063: Vector v = new Vector(_v.size());
064: Enumeration m = _v.elements();
065: while (m.hasMoreElements()) {
066: Object value = m.nextElement();
067: if (value instanceof PjObject) {
068: v.addElement(((PjObject) value).clone());
069: } else {
070: throw new CloneNotSupportedException(
071: "Object in array is not a PjObject.");
072: }
073: }
074: return v;
075: }
076:
077: /**
078: Renumbers object references within this object. This
079: method calls itself recursively to comprehensively renumber
080: all objects contained within this object.
081: @param map the table of object number mappings. Each
082: object number is looked up by key in the hash table, and
083: the associated value is assigned as the new object number.
084: The map hash table should consist of PjNumber keys and
085: PjReference values.
086: */
087: public void renumber(Hashtable map) {
088: PjObject obj;
089: Object r;
090: int size = _v.size();
091: for (int x = 0; x < size; x++) {
092: try {
093: obj = (PjObject) (_v.elementAt(x));
094: if (obj instanceof PjReference) {
095: r = map.get(((PjReference) obj).getObjNumber());
096: if (r != null) {
097: _v.setElementAt(r, x);
098: }
099: } else {
100: obj.renumber(map);
101: }
102: } catch (ClassCastException e) {
103: // ignore bad objects
104: }
105: }
106: }
107:
108: protected Vector _v;
109:
110: }
|