KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > etymon > pjx > PdfArray


1 /*
2   Copyright (C) Etymon Systems, Inc. <http://www.etymon.com/>
3 */

4
5 package com.etymon.pjx;
6
7 import java.io.*;
8 import java.util.*;
9
10 /**
11    Represents the PDF array object.
12    @author Nassib Nassar
13 */

14 public class PdfArray
15     extends PdfObject {
16
17     /**
18        The contents of the array.
19      */

20     protected List _a;
21     
22     /**
23        A protected constructor intended to be called only from
24        {@link #wrap(List) wrap(List)}.
25      */

26     protected PdfArray() { // called only from wrap()
27
}
28
29     /**
30        Constructs an array object from a list of
31        <code>PdfObject</code> instances.
32        @param a the list containing the <code>PdfObject</code>
33        instances.
34      */

35     public PdfArray(List a) {
36         _a = Collections.unmodifiableList(new ArrayList(a));
37     }
38
39     protected PdfObject filterContents(PdfObjectFilter f) throws PdfFormatException {
40         List newList = new ArrayList(_a.size());
41         for (Iterator t = _a.iterator(); t.hasNext(); ) {
42             Object JavaDoc obj = t.next();
43             if ( !(obj instanceof PdfObject) ) {
44                 throw new PdfFormatException("Array element is not a PDF object.");
45             }
46             
47             PdfObject objF = ((PdfObject)obj).filter(f);
48             
49             if (objF != null) {
50                 newList.add(objF);
51             }
52         }
53         return f.postFilter(new PdfArray(newList));
54     }
55     
56     public boolean equals(Object JavaDoc obj) {
57
58         if ( (obj == null) || ( !(obj instanceof PdfArray) ) ) {
59             return false;
60         }
61
62         return _a.equals( ((PdfArray)obj)._a );
63     }
64
65     /**
66        Returns the list of elements contained in this array.
67        @return the list of elements. The returned
68        <code>List</code> object is unmodifiable.
69      */

70     public List getList() {
71         return _a;
72     }
73
74     public int hashCode() {
75         return _a.hashCode();
76     }
77
78     /**
79        A factory for fast construction of this class. The
80        constructed object will be a wrapper around the specified
81        <code>List</code>. The calling method must ensure that the
82        <code>List</code> is never externally modified, in order to
83        meet the immutability requirement of {@link PdfObject
84        PdfObject}.
85        @param a the <code>List</code> to be used to back this
86        array.
87        @return the constructed object.
88      */

89     protected static PdfArray wrap(List a) {
90         PdfArray pa = new PdfArray();
91         pa._a = Collections.unmodifiableList(a);
92         return pa;
93     }
94     
95     protected int writePdf(PdfWriter w, boolean spacing) throws IOException {
96
97         DataOutputStream dos = w.getDataOutputStream();
98
99         int count = 2; // for [ and ]
100

101         dos.write('[');
102         
103         boolean first = true;
104         for (Iterator p = _a.iterator(); p.hasNext(); ) {
105             PdfObject obj = (PdfObject)p.next();
106             if (first) {
107                 count += obj.writePdf(w, false);
108                 first = false;
109             } else {
110                 count += obj.writePdf(w, true);
111             }
112         }
113         
114         dos.write(']');
115
116         return count;
117     }
118
119 }
120
Popular Tags