KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > etymon > pj > object > PjArray


1 package com.etymon.pj.object;
2
3 import java.io.*;
4 import java.util.*;
5 import com.etymon.pj.*;
6
7 /**
8    A representation of the PDF array type.
9    @author Nassib Nassar
10 */

11 public class PjArray
12     extends PjObject {
13
14     /**
15        Creates an empty array.
16     */

17     public PjArray() {
18         _v = new Vector();
19     }
20
21     /**
22        Creates an array as a wrapper around a Vector.
23        @param v the Vector to use for this array.
24     */

25     public PjArray(Vector v) {
26         _v = v;
27     }
28
29     /**
30        Returns the Vector used to represent this array.
31        @return the Vector used to represent this array.
32     */

33     public Vector getVector() {
34         return _v;
35     }
36
37     /**
38        Writes this array to a stream in PDF format.
39        @param os the stream to write to.
40        @return the number of bytes written.
41        @exception IOException if an I/O error occurs.
42      */

43     public long writePdf(OutputStream os) throws IOException {
44         long z = writeln(os, "[");
45         int size = _v.size();
46         for (int x = 0; x < size; x++) {
47             z = z + ((PjObject)(_v.elementAt(x))).writePdf(os);
48             z = z + writeln(os, "");
49         }
50         z = z + write(os, "]");
51         return z;
52     }
53
54     /**
55        Returns a deep copy of this object.
56        @return a deep copy of this object.
57        @exception CloneNotSupportedException if the instance can not be cloned.
58     */

59     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
60         return new PjArray(cloneVector());
61     }
62
63     protected Vector cloneVector() throws CloneNotSupportedException JavaDoc {
64         Vector v = new Vector(_v.size());
65         Enumeration m = _v.elements();
66         while (m.hasMoreElements()) {
67             Object JavaDoc value = m.nextElement();
68             if (value instanceof PjObject) {
69                 v.addElement(((PjObject)value).clone());
70             } else {
71                 throw new CloneNotSupportedException JavaDoc("Object in array is not a PjObject.");
72             }
73         }
74         return v;
75     }
76     
77     /**
78        Renumbers object references within this object. This
79        method calls itself recursively to comprehensively renumber
80        all objects contained within this object.
81        @param map the table of object number mappings. Each
82        object number is looked up by key in the hash table, and
83        the associated value is assigned as the new object number.
84        The map hash table should consist of PjNumber keys and
85        PjReference values.
86     */

87     public void renumber(Hashtable map) {
88         PjObject obj;
89         Object JavaDoc r;
90         int size = _v.size();
91         for (int x = 0; x < size; x++) {
92             try {
93                 obj = (PjObject)(_v.elementAt(x));
94                 if (obj instanceof PjReference) {
95                     r = map.get(((PjReference)obj).getObjNumber());
96                     if (r != null) {
97                         _v.setElementAt(r, x);
98                     }
99                 } else {
100                     obj.renumber(map);
101                 }
102             }
103             catch (ClassCastException JavaDoc e) {
104                 // ignore bad objects
105
}
106         }
107     }
108     
109     protected Vector _v;
110     
111 }
112
Popular Tags