001: /*
002: Copyright (C) Etymon Systems, Inc. <http://www.etymon.com/>
003: */
004:
005: package com.etymon.pjx;
006:
007: import java.io.*;
008: import java.util.*;
009:
010: /**
011: Represents the PDF array object.
012: @author Nassib Nassar
013: */
014: public class PdfArray extends PdfObject {
015:
016: /**
017: The contents of the array.
018: */
019: protected List _a;
020:
021: /**
022: A protected constructor intended to be called only from
023: {@link #wrap(List) wrap(List)}.
024: */
025: protected PdfArray() { // called only from wrap()
026: }
027:
028: /**
029: Constructs an array object from a list of
030: <code>PdfObject</code> instances.
031: @param a the list containing the <code>PdfObject</code>
032: instances.
033: */
034: public PdfArray(List a) {
035: _a = Collections.unmodifiableList(new ArrayList(a));
036: }
037:
038: protected PdfObject filterContents(PdfObjectFilter f)
039: throws PdfFormatException {
040: List newList = new ArrayList(_a.size());
041: for (Iterator t = _a.iterator(); t.hasNext();) {
042: Object obj = t.next();
043: if (!(obj instanceof PdfObject)) {
044: throw new PdfFormatException(
045: "Array element is not a PDF object.");
046: }
047:
048: PdfObject objF = ((PdfObject) obj).filter(f);
049:
050: if (objF != null) {
051: newList.add(objF);
052: }
053: }
054: return f.postFilter(new PdfArray(newList));
055: }
056:
057: public boolean equals(Object obj) {
058:
059: if ((obj == null) || (!(obj instanceof PdfArray))) {
060: return false;
061: }
062:
063: return _a.equals(((PdfArray) obj)._a);
064: }
065:
066: /**
067: Returns the list of elements contained in this array.
068: @return the list of elements. The returned
069: <code>List</code> object is unmodifiable.
070: */
071: public List getList() {
072: return _a;
073: }
074:
075: public int hashCode() {
076: return _a.hashCode();
077: }
078:
079: /**
080: A factory for fast construction of this class. The
081: constructed object will be a wrapper around the specified
082: <code>List</code>. The calling method must ensure that the
083: <code>List</code> is never externally modified, in order to
084: meet the immutability requirement of {@link PdfObject
085: PdfObject}.
086: @param a the <code>List</code> to be used to back this
087: array.
088: @return the constructed object.
089: */
090: protected static PdfArray wrap(List a) {
091: PdfArray pa = new PdfArray();
092: pa._a = Collections.unmodifiableList(a);
093: return pa;
094: }
095:
096: protected int writePdf(PdfWriter w, boolean spacing)
097: throws IOException {
098:
099: DataOutputStream dos = w.getDataOutputStream();
100:
101: int count = 2; // for [ and ]
102:
103: dos.write('[');
104:
105: boolean first = true;
106: for (Iterator p = _a.iterator(); p.hasNext();) {
107: PdfObject obj = (PdfObject) p.next();
108: if (first) {
109: count += obj.writePdf(w, false);
110: first = false;
111: } else {
112: count += obj.writePdf(w, true);
113: }
114: }
115:
116: dos.write(']');
117:
118: return count;
119: }
120:
121: }
|