001: /**
002: * Copyright (c) 2003-2006, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.cos;
031:
032: import java.util.ArrayList;
033: import java.util.Collection;
034: import java.util.Iterator;
035: import java.util.List;
036:
037: import org.pdfbox.exceptions.COSVisitorException;
038: import org.pdfbox.pdmodel.common.COSObjectable;
039:
040: /**
041: * An array of PDFBase objects as part of the PDF document.
042: *
043: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
044: * @version $Revision: 1.24 $
045: */
046: public class COSArray extends COSBase {
047: private List objects = new ArrayList();
048:
049: /**
050: * Constructor.
051: */
052: public COSArray() {
053: //default constructor
054: }
055:
056: /**
057: * This will add an object to the array.
058: *
059: * @param object The object to add to the array.
060: */
061: public void add(COSBase object) {
062: objects.add(object);
063: }
064:
065: /**
066: * This will add an object to the array.
067: *
068: * @param object The object to add to the array.
069: */
070: public void add(COSObjectable object) {
071: objects.add(object.getCOSObject());
072: }
073:
074: /**
075: * Add the specified object at the ith location and push the rest to the
076: * right.
077: *
078: * @param i The index to add at.
079: * @param object The object to add at that index.
080: */
081: public void add(int i, COSBase object) {
082: objects.add(i, object);
083: }
084:
085: /**
086: * This will remove all of the objects in the collection.
087: */
088: public void clear() {
089: objects.clear();
090: }
091:
092: /**
093: * This will remove all of the objects in the collection.
094: *
095: * @param objectsList The list of objects to remove from the collection.
096: */
097: public void removeAll(Collection objectsList) {
098: objects.removeAll(objectsList);
099: }
100:
101: /**
102: * This will retain all of the objects in the collection.
103: *
104: * @param objectsList The list of objects to retain from the collection.
105: */
106: public void retainAll(Collection objectsList) {
107: objects.retainAll(objectsList);
108: }
109:
110: /**
111: * This will add an object to the array.
112: *
113: * @param objectsList The object to add to the array.
114: */
115: public void addAll(Collection objectsList) {
116: objects.addAll(objectsList);
117: }
118:
119: /**
120: * This will add all objects to this array.
121: *
122: * @param objectList The objects to add.
123: */
124: public void addAll(COSArray objectList) {
125: if (objectList != null) {
126: objects.addAll(objectList.objects);
127: }
128: }
129:
130: /**
131: * Add the specified object at the ith location and push the rest to the
132: * right.
133: *
134: * @param i The index to add at.
135: * @param objectList The object to add at that index.
136: */
137: public void addAll(int i, Collection objectList) {
138: objects.addAll(i, objectList);
139: }
140:
141: /**
142: * This will set an object at a specific index.
143: *
144: * @param index zero based index into array.
145: * @param object The object to set.
146: */
147: public void set(int index, COSBase object) {
148: objects.set(index, object);
149: }
150:
151: /**
152: * This will set an object at a specific index.
153: *
154: * @param index zero based index into array.
155: * @param intVal The object to set.
156: */
157: public void set(int index, int intVal) {
158: objects.set(index, new COSInteger(intVal));
159: }
160:
161: /**
162: * This will set an object at a specific index.
163: *
164: * @param index zero based index into array.
165: * @param object The object to set.
166: */
167: public void set(int index, COSObjectable object) {
168: COSBase base = null;
169: if (object != null) {
170: base = object.getCOSObject();
171: }
172: objects.set(index, base);
173: }
174:
175: /**
176: * This will get an object from the array. This will dereference the object.
177: * If the object is COSNull then null will be returned.
178: *
179: * @param index The index into the array to get the object.
180: *
181: * @return The object at the requested index.
182: */
183: public COSBase getObject(int index) {
184: Object obj = objects.get(index);
185: if (obj instanceof COSObject) {
186: obj = ((COSObject) obj).getObject();
187: }
188: if (obj instanceof COSNull) {
189: obj = null;
190: }
191: return (COSBase) obj;
192: }
193:
194: /**
195: * This will get an object from the array. This will NOT derefernce
196: * the COS object.
197: *
198: * @param index The index into the array to get the object.
199: *
200: * @return The object at the requested index.
201: */
202: public COSBase get(int index) {
203: return (COSBase) objects.get(index);
204: }
205:
206: /**
207: * Get the value of the array as an integer.
208: *
209: * @param index The index into the list.
210: *
211: * @return The value at that index or -1 if it is null.
212: */
213: public int getInt(int index) {
214: return getInt(index, -1);
215: }
216:
217: /**
218: * Get the value of the array as an integer, return the default if it does
219: * not exist.
220: *
221: * @param index The value of the array.
222: * @param defaultValue The value to return if the value is null.
223: * @return The value at the index or the defaultValue.
224: */
225: public int getInt(int index, int defaultValue) {
226: int retval = defaultValue;
227: if (defaultValue < size()) {
228: COSNumber number = (COSNumber) get(index);
229: if (number != null) {
230: retval = number.intValue();
231: }
232: }
233: return retval;
234: }
235:
236: /**
237: * Set the value in the array as an integer.
238: *
239: * @param index The index into the array.
240: * @param value The value to set.
241: */
242: public void setInt(int index, int value) {
243: set(index, new COSInteger(value));
244: }
245:
246: /**
247: * Set the value in the array as a name.
248: * @param index The index into the array.
249: * @param name The name to set in the array.
250: */
251: public void setName(int index, String name) {
252: set(index, COSName.getPDFName(name));
253: }
254:
255: /**
256: * Get the value of the array as a string.
257: *
258: * @param index The index into the array.
259: * @return The name converted to a string or null if it does not exist.
260: */
261: public String getName(int index) {
262: return getName(index, null);
263: }
264:
265: /**
266: * Get an entry in the array that is expected to be a COSName.
267: * @param index The index into the array.
268: * @param defaultValue The value to return if it is null.
269: * @return The value at the index or defaultValue if none is found.
270: */
271: public String getName(int index, String defaultValue) {
272: String retval = defaultValue;
273: if (index < size()) {
274: COSName name = (COSName) get(index);
275: if (name != null) {
276: retval = name.getName();
277: }
278: }
279: return retval;
280: }
281:
282: /**
283: * Set the value in the array as a string.
284: * @param index The index into the array.
285: * @param string The string to set in the array.
286: */
287: public void setString(int index, String string) {
288: set(index, new COSString(string));
289: }
290:
291: /**
292: * Get the value of the array as a string.
293: *
294: * @param index The index into the array.
295: * @return The string or null if it does not exist.
296: */
297: public String getString(int index) {
298: return getString(index, null);
299: }
300:
301: /**
302: * Get an entry in the array that is expected to be a COSName.
303: * @param index The index into the array.
304: * @param defaultValue The value to return if it is null.
305: * @return The value at the index or defaultValue if none is found.
306: */
307: public String getString(int index, String defaultValue) {
308: String retval = defaultValue;
309: if (index < size()) {
310: COSString string = (COSString) get(index);
311: if (string != null) {
312: retval = string.getString();
313: }
314: }
315: return retval;
316: }
317:
318: /**
319: * This will get the size of this array.
320: *
321: * @return The number of elements in the array.
322: */
323: public int size() {
324: return objects.size();
325: }
326:
327: /**
328: * This will remove an element from the array.
329: *
330: * @param i The index of the object to remove.
331: *
332: * @return The object that was removed.
333: */
334: public COSBase remove(int i) {
335: return (COSBase) objects.remove(i);
336: }
337:
338: /**
339: * This will remove an element from the array.
340: *
341: * @param o The object to remove.
342: *
343: * @return The object that was removed.
344: */
345: public boolean remove(COSBase o) {
346: return objects.remove(o);
347: }
348:
349: /**
350: * {@inheritDoc}
351: */
352: public String toString() {
353: return "COSArray{" + objects + "}";
354: }
355:
356: /**
357: * Get access to the list.
358: *
359: * @return an iterator over the array elements
360: */
361: public Iterator iterator() {
362: return objects.iterator();
363: }
364:
365: /**
366: * This will return the index of the entry or -1 if it is not found.
367: *
368: * @param object The object to search for.
369: * @return The index of the object or -1.
370: */
371: public int indexOf(COSBase object) {
372: int retval = -1;
373: for (int i = 0; retval < 0 && i < size(); i++) {
374: if (get(i).equals(object)) {
375: retval = i;
376: }
377: }
378: return retval;
379: }
380:
381: /**
382: * This will add null values until the size of the array is at least
383: * as large as the parameter. If the array is already larger than the
384: * parameter then nothing is done.
385: *
386: * @param size The desired size of the array.
387: */
388: public void growToSize(int size) {
389: growToSize(size, null);
390: }
391:
392: /**
393: * This will add the object until the size of the array is at least
394: * as large as the parameter. If the array is already larger than the
395: * parameter then nothing is done.
396: *
397: * @param size The desired size of the array.
398: * @param object The object to fill the array with.
399: */
400: public void growToSize(int size, COSBase object) {
401: while (size() < size) {
402: add(object);
403: }
404: }
405:
406: /**
407: * visitor pattern double dispatch method.
408: *
409: * @param visitor The object to notify when visiting this object.
410: * @return any object, depending on the visitor implementation, or null
411: * @throws COSVisitorException If an error occurs while visiting this object.
412: */
413: public Object accept(ICOSVisitor visitor)
414: throws COSVisitorException {
415: return visitor.visitFromArray(this );
416: }
417:
418: /**
419: * This will take an COSArray of numbers and convert it to a float[].
420: *
421: * @return This COSArray as an array of float numbers.
422: */
423: public float[] toFloatArray() {
424: float[] retval = new float[size()];
425: for (int i = 0; i < size(); i++) {
426: retval[i] = ((COSNumber) getObject(i)).floatValue();
427: }
428: return retval;
429: }
430:
431: /**
432: * Clear the current contents of the COSArray and set it with the float[].
433: *
434: * @param value The new value of the float array.
435: */
436: public void setFloatArray(float[] value) {
437: this .clear();
438: for (int i = 0; i < value.length; i++) {
439: add(new COSFloat(value[i]));
440: }
441: }
442: }
|