KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > PdfArray


1 /*
2  * $Id: PdfArray.java 2739 2007-05-04 11:24:51Z blowagie $
3  * $Name$
4  *
5  * Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50
51 package com.lowagie.text.pdf;
52
53 import java.io.IOException JavaDoc;
54 import java.io.OutputStream JavaDoc;
55 import java.util.ArrayList JavaDoc;
56 import java.util.Iterator JavaDoc;
57 import java.util.ListIterator JavaDoc;
58
59 /**
60  * <CODE>PdfArray</CODE> is the PDF Array object.
61  * <P>
62  * An array is a sequence of PDF objects. An array may contain a mixture of object types.
63  * An array is written as a left square bracket ([), followed by a sequence of objects,
64  * followed by a right square bracket (]).<BR>
65  * This object is described in the 'Portable Document Format Reference Manual version 1.7'
66  * section 3.2.5 (page 58).
67  *
68  * @see PdfObject
69  */

70
71 public class PdfArray extends PdfObject {
72     
73     // membervariables
74

75 /** this is the actual array of PdfObjects */
76     protected ArrayList JavaDoc arrayList;
77     
78     // constructors
79

80 /**
81  * Constructs an empty <CODE>PdfArray</CODE>-object.
82  */

83     
84     public PdfArray() {
85         super(ARRAY);
86         arrayList = new ArrayList JavaDoc();
87     }
88     
89 /**
90  * Constructs an <CODE>PdfArray</CODE>-object, containing 1 <CODE>PdfObject</CODE>.
91  *
92  * @param object a <CODE>PdfObject</CODE> that has to be added to the array
93  */

94     
95     public PdfArray(PdfObject object) {
96         super(ARRAY);
97         arrayList = new ArrayList JavaDoc();
98         arrayList.add(object);
99     }
100     
101     public PdfArray(float values[]) {
102         super(ARRAY);
103         arrayList = new ArrayList JavaDoc();
104         add(values);
105     }
106     
107     public PdfArray(int values[]) {
108         super(ARRAY);
109         arrayList = new ArrayList JavaDoc();
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 JavaDoc(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 JavaDoc os) throws IOException JavaDoc {
131         os.write('[');
132
133         Iterator JavaDoc i = arrayList.iterator();
134         PdfObject object;
135         int type = 0;
136         if (i.hasNext()) {
137             object = (PdfObject) i.next();
138             if (object == null)
139                 object = PdfNull.PDFNULL;
140             object.toPdf(writer, os);
141         }
142         while (i.hasNext()) {
143             object = (PdfObject) i.next();
144             if (object == null)
145                 object = PdfNull.PDFNULL;
146             type = object.type();
147             if (type != PdfObject.ARRAY && type != PdfObject.DICTIONARY && type != PdfObject.NAME && type != PdfObject.STRING)
148                 os.write(' ');
149             object.toPdf(writer, os);
150         }
151         os.write(']');
152     }
153     
154     // methods concerning the ArrayList-membervalue
155

156 /**
157  * Returns an ArrayList containing <CODE>PdfObject</CODE>s.
158  *
159  * @return an ArrayList
160  */

161     
162     public ArrayList JavaDoc getArrayList() {
163         return arrayList;
164     }
165     
166 /**
167  * Returns the number of entries in the array.
168  *
169  * @return the size of the ArrayList
170  */

171     
172     public int size() {
173         return arrayList.size();
174     }
175     
176 /**
177  * Adds a <CODE>PdfObject</CODE> to the <CODE>PdfArray</CODE>.
178  *
179  * @param object <CODE>PdfObject</CODE> to add
180  * @return <CODE>true</CODE>
181  */

182     
183     public boolean add(PdfObject object) {
184         return arrayList.add(object);
185     }
186     
187     public boolean add(float values[]) {
188         for (int k = 0; k < values.length; ++k)
189             arrayList.add(new PdfNumber(values[k]));
190         return true;
191     }
192     
193     public boolean add(int values[]) {
194         for (int k = 0; k < values.length; ++k)
195             arrayList.add(new PdfNumber(values[k]));
196         return true;
197     }
198     
199 /**
200  * Adds a <CODE>PdfObject</CODE> to the <CODE>PdfArray</CODE>.
201  * <P>
202  * The newly added object will be the first element in the <CODE>ArrayList</CODE>.
203  *
204  * @param object <CODE>PdfObject</CODE> to add
205  */

206     
207     public void addFirst(PdfObject object) {
208         arrayList.add(0, object);
209     }
210     
211 /**
212  * Checks if the <CODE>PdfArray</CODE> already contains a certain <CODE>PdfObject</CODE>.
213  *
214  * @param object <CODE>PdfObject</CODE> to check
215  * @return <CODE>true</CODE>
216  */

217     
218     public boolean contains(PdfObject object) {
219         return arrayList.contains(object);
220     }
221     
222     public ListIterator JavaDoc listIterator() {
223         return arrayList.listIterator();
224     }
225     
226     public String JavaDoc toString() {
227         return arrayList.toString();
228     }
229     
230     public PdfObject getPdfObject( int idx ) {
231         return (PdfObject)arrayList.get(idx);
232     }
233     
234     public PdfObject getDirectObject( int idx ) {
235         return PdfReader.getPdfObject(getPdfObject(idx));
236     }
237     
238     // more of the same like PdfDictionary. (MAS 2/17/06)
239
public PdfDictionary getAsDict(int idx) {
240         PdfDictionary dict = null;
241         PdfObject orig = getDirectObject(idx);
242         if (orig != null && orig.isDictionary())
243             dict = (PdfDictionary) orig;
244         return dict;
245     }
246     
247     public PdfArray getAsArray(int idx) {
248         PdfArray array = null;
249         PdfObject orig = getDirectObject(idx);
250         if (orig != null && orig.isArray())
251             array = (PdfArray) orig;
252         return array;
253     }
254     
255     public PdfStream getAsStream(int idx) {
256         PdfStream stream = null;
257         PdfObject orig = getDirectObject(idx);
258         if (orig != null && orig.isStream())
259             stream = (PdfStream) orig;
260         return stream;
261     }
262     
263     public PdfString getAsString(int idx) {
264         PdfString string = null;
265         PdfObject orig = getDirectObject(idx);
266         if (orig != null && orig.isString())
267             string = (PdfString) orig;
268         return string;
269     }
270     
271     public PdfNumber getAsNumber(int idx) {
272         PdfNumber number = null;
273         PdfObject orig = getDirectObject(idx);
274         if (orig != null && orig.isNumber())
275             number = (PdfNumber) orig;
276         return number;
277     }
278     
279     public PdfName getAsName(int idx) {
280         PdfName name = null;
281         PdfObject orig = getDirectObject(idx);
282         if (orig != null && orig.isName())
283             name = (PdfName) orig;
284         return name;
285     }
286     
287     public PdfBoolean getAsBoolean(int idx) {
288         PdfBoolean bool = null;
289         PdfObject orig = getDirectObject(idx);
290         if (orig != null && orig.isBoolean())
291             bool = (PdfBoolean) orig;
292         return bool;
293     }
294     
295     public PdfIndirectReference getAsIndirectObject(int idx) {
296         PdfIndirectReference ref = null;
297         PdfObject orig = getPdfObject(idx); // not getDirect this time.
298
if (orig != null && orig.isIndirect())
299             ref = (PdfIndirectReference) orig;
300         return ref;
301     }
302 }
Popular Tags