001: /*
002: * $Id: PdfArray.java 2739 2007-05-04 11:24:51Z blowagie $
003: * $Name$
004: *
005: * Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */
050:
051: package com.lowagie.text.pdf;
052:
053: import java.io.IOException;
054: import java.io.OutputStream;
055: import java.util.ArrayList;
056: import java.util.Iterator;
057: import java.util.ListIterator;
058:
059: /**
060: * <CODE>PdfArray</CODE> is the PDF Array object.
061: * <P>
062: * An array is a sequence of PDF objects. An array may contain a mixture of object types.
063: * An array is written as a left square bracket ([), followed by a sequence of objects,
064: * followed by a right square bracket (]).<BR>
065: * This object is described in the 'Portable Document Format Reference Manual version 1.7'
066: * section 3.2.5 (page 58).
067: *
068: * @see PdfObject
069: */
070:
071: public class PdfArray extends PdfObject {
072:
073: // membervariables
074:
075: /** this is the actual array of PdfObjects */
076: protected ArrayList arrayList;
077:
078: // constructors
079:
080: /**
081: * Constructs an empty <CODE>PdfArray</CODE>-object.
082: */
083:
084: public PdfArray() {
085: super (ARRAY);
086: arrayList = new ArrayList();
087: }
088:
089: /**
090: * Constructs an <CODE>PdfArray</CODE>-object, containing 1 <CODE>PdfObject</CODE>.
091: *
092: * @param object a <CODE>PdfObject</CODE> that has to be added to the array
093: */
094:
095: public PdfArray(PdfObject object) {
096: super (ARRAY);
097: arrayList = new ArrayList();
098: arrayList.add(object);
099: }
100:
101: public PdfArray(float values[]) {
102: super (ARRAY);
103: arrayList = new ArrayList();
104: add(values);
105: }
106:
107: public PdfArray(int values[]) {
108: super (ARRAY);
109: arrayList = new ArrayList();
110: add(values);
111: }
112:
113: /**
114: * Constructs an <CODE>PdfArray</CODE>-object, containing all the <CODE>PdfObject</CODE>s in a given <CODE>PdfArray</CODE>.
115: *
116: * @param array a <CODE>PdfArray</CODE> that has to be added to the array
117: */
118:
119: public PdfArray(PdfArray array) {
120: super (ARRAY);
121: arrayList = new ArrayList(array.getArrayList());
122: }
123:
124: // methods overriding some methods in PdfObject
125:
126: /**
127: * Returns the PDF representation of this <CODE>PdfArray</CODE>.
128: */
129:
130: public void toPdf(PdfWriter writer, OutputStream os)
131: throws IOException {
132: os.write('[');
133:
134: Iterator i = arrayList.iterator();
135: PdfObject object;
136: int type = 0;
137: if (i.hasNext()) {
138: object = (PdfObject) i.next();
139: if (object == null)
140: object = PdfNull.PDFNULL;
141: object.toPdf(writer, os);
142: }
143: while (i.hasNext()) {
144: object = (PdfObject) i.next();
145: if (object == null)
146: object = PdfNull.PDFNULL;
147: type = object.type();
148: if (type != PdfObject.ARRAY && type != PdfObject.DICTIONARY
149: && type != PdfObject.NAME
150: && type != PdfObject.STRING)
151: os.write(' ');
152: object.toPdf(writer, os);
153: }
154: os.write(']');
155: }
156:
157: // methods concerning the ArrayList-membervalue
158:
159: /**
160: * Returns an ArrayList containing <CODE>PdfObject</CODE>s.
161: *
162: * @return an ArrayList
163: */
164:
165: public ArrayList getArrayList() {
166: return arrayList;
167: }
168:
169: /**
170: * Returns the number of entries in the array.
171: *
172: * @return the size of the ArrayList
173: */
174:
175: public int size() {
176: return arrayList.size();
177: }
178:
179: /**
180: * Adds a <CODE>PdfObject</CODE> to the <CODE>PdfArray</CODE>.
181: *
182: * @param object <CODE>PdfObject</CODE> to add
183: * @return <CODE>true</CODE>
184: */
185:
186: public boolean add(PdfObject object) {
187: return arrayList.add(object);
188: }
189:
190: public boolean add(float values[]) {
191: for (int k = 0; k < values.length; ++k)
192: arrayList.add(new PdfNumber(values[k]));
193: return true;
194: }
195:
196: public boolean add(int values[]) {
197: for (int k = 0; k < values.length; ++k)
198: arrayList.add(new PdfNumber(values[k]));
199: return true;
200: }
201:
202: /**
203: * Adds a <CODE>PdfObject</CODE> to the <CODE>PdfArray</CODE>.
204: * <P>
205: * The newly added object will be the first element in the <CODE>ArrayList</CODE>.
206: *
207: * @param object <CODE>PdfObject</CODE> to add
208: */
209:
210: public void addFirst(PdfObject object) {
211: arrayList.add(0, object);
212: }
213:
214: /**
215: * Checks if the <CODE>PdfArray</CODE> already contains a certain <CODE>PdfObject</CODE>.
216: *
217: * @param object <CODE>PdfObject</CODE> to check
218: * @return <CODE>true</CODE>
219: */
220:
221: public boolean contains(PdfObject object) {
222: return arrayList.contains(object);
223: }
224:
225: public ListIterator listIterator() {
226: return arrayList.listIterator();
227: }
228:
229: public String toString() {
230: return arrayList.toString();
231: }
232:
233: public PdfObject getPdfObject(int idx) {
234: return (PdfObject) arrayList.get(idx);
235: }
236:
237: public PdfObject getDirectObject(int idx) {
238: return PdfReader.getPdfObject(getPdfObject(idx));
239: }
240:
241: // more of the same like PdfDictionary. (MAS 2/17/06)
242: public PdfDictionary getAsDict(int idx) {
243: PdfDictionary dict = null;
244: PdfObject orig = getDirectObject(idx);
245: if (orig != null && orig.isDictionary())
246: dict = (PdfDictionary) orig;
247: return dict;
248: }
249:
250: public PdfArray getAsArray(int idx) {
251: PdfArray array = null;
252: PdfObject orig = getDirectObject(idx);
253: if (orig != null && orig.isArray())
254: array = (PdfArray) orig;
255: return array;
256: }
257:
258: public PdfStream getAsStream(int idx) {
259: PdfStream stream = null;
260: PdfObject orig = getDirectObject(idx);
261: if (orig != null && orig.isStream())
262: stream = (PdfStream) orig;
263: return stream;
264: }
265:
266: public PdfString getAsString(int idx) {
267: PdfString string = null;
268: PdfObject orig = getDirectObject(idx);
269: if (orig != null && orig.isString())
270: string = (PdfString) orig;
271: return string;
272: }
273:
274: public PdfNumber getAsNumber(int idx) {
275: PdfNumber number = null;
276: PdfObject orig = getDirectObject(idx);
277: if (orig != null && orig.isNumber())
278: number = (PdfNumber) orig;
279: return number;
280: }
281:
282: public PdfName getAsName(int idx) {
283: PdfName name = null;
284: PdfObject orig = getDirectObject(idx);
285: if (orig != null && orig.isName())
286: name = (PdfName) orig;
287: return name;
288: }
289:
290: public PdfBoolean getAsBoolean(int idx) {
291: PdfBoolean bool = null;
292: PdfObject orig = getDirectObject(idx);
293: if (orig != null && orig.isBoolean())
294: bool = (PdfBoolean) orig;
295: return bool;
296: }
297:
298: public PdfIndirectReference getAsIndirectObject(int idx) {
299: PdfIndirectReference ref = null;
300: PdfObject orig = getPdfObject(idx); // not getDirect this time.
301: if (orig != null && orig.isIndirect())
302: ref = (PdfIndirectReference) orig;
303: return ref;
304: }
305: }
|