KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > cos > COSArray


1 /**
2  * Copyright (c) 2003-2006, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.cos;
32
33 import java.util.ArrayList JavaDoc;
34 import java.util.Collection JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37
38
39
40 import org.pdfbox.exceptions.COSVisitorException;
41 import org.pdfbox.pdmodel.common.COSObjectable;
42
43 /**
44  * An array of PDFBase objects as part of the PDF document.
45  *
46  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
47  * @version $Revision: 1.24 $
48  */

49 public class COSArray extends COSBase
50 {
51     private List JavaDoc objects = new ArrayList JavaDoc();
52
53     /**
54      * Constructor.
55      */

56     public COSArray()
57     {
58         //default constructor
59
}
60
61     /**
62      * This will add an object to the array.
63      *
64      * @param object The object to add to the array.
65      */

66     public void add( COSBase object )
67     {
68         objects.add( object );
69     }
70     
71     /**
72      * This will add an object to the array.
73      *
74      * @param object The object to add to the array.
75      */

76     public void add( COSObjectable object )
77     {
78         objects.add( object.getCOSObject() );
79     }
80
81     /**
82      * Add the specified object at the ith location and push the rest to the
83      * right.
84      *
85      * @param i The index to add at.
86      * @param object The object to add at that index.
87      */

88     public void add( int i, COSBase object)
89     {
90         objects.add( i, object );
91     }
92
93     /**
94      * This will remove all of the objects in the collection.
95      */

96     public void clear()
97     {
98         objects.clear();
99     }
100
101     /**
102      * This will remove all of the objects in the collection.
103      *
104      * @param objectsList The list of objects to remove from the collection.
105      */

106     public void removeAll( Collection JavaDoc objectsList )
107     {
108         objects.removeAll( objectsList );
109     }
110
111     /**
112      * This will retain all of the objects in the collection.
113      *
114      * @param objectsList The list of objects to retain from the collection.
115      */

116     public void retainAll( Collection JavaDoc objectsList )
117     {
118         objects.retainAll( objectsList );
119     }
120
121     /**
122      * This will add an object to the array.
123      *
124      * @param objectsList The object to add to the array.
125      */

126     public void addAll( Collection JavaDoc objectsList )
127     {
128         objects.addAll( objectsList );
129     }
130
131     /**
132      * This will add all objects to this array.
133      *
134      * @param objectList The objects to add.
135      */

136     public void addAll( COSArray objectList )
137     {
138         if( objectList != null )
139         {
140             objects.addAll( objectList.objects );
141         }
142     }
143
144     /**
145      * Add the specified object at the ith location and push the rest to the
146      * right.
147      *
148      * @param i The index to add at.
149      * @param objectList The object to add at that index.
150      */

151     public void addAll( int i, Collection JavaDoc objectList )
152     {
153         objects.addAll( i, objectList );
154     }
155
156     /**
157      * This will set an object at a specific index.
158      *
159      * @param index zero based index into array.
160      * @param object The object to set.
161      */

162     public void set( int index, COSBase object )
163     {
164         objects.set( index, object );
165     }
166     
167     /**
168      * This will set an object at a specific index.
169      *
170      * @param index zero based index into array.
171      * @param intVal The object to set.
172      */

173     public void set( int index, int intVal )
174     {
175         objects.set( index, new COSInteger( intVal ) );
176     }
177     
178     /**
179      * This will set an object at a specific index.
180      *
181      * @param index zero based index into array.
182      * @param object The object to set.
183      */

184     public void set( int index, COSObjectable object )
185     {
186         COSBase base = null;
187         if( object != null )
188         {
189             base = object.getCOSObject();
190         }
191         objects.set( index, base );
192     }
193
194     /**
195      * This will get an object from the array. This will dereference the object.
196      * If the object is COSNull then null will be returned.
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 getObject( int index )
203     {
204         Object JavaDoc obj = objects.get( index );
205         if( obj instanceof COSObject )
206         {
207             obj = ((COSObject)obj).getObject();
208         }
209         if( obj instanceof COSNull )
210         {
211             obj = null;
212         }
213         return (COSBase)obj;
214     }
215
216     /**
217      * This will get an object from the array. This will NOT derefernce
218      * the COS object.
219      *
220      * @param index The index into the array to get the object.
221      *
222      * @return The object at the requested index.
223      */

224     public COSBase get( int index )
225     {
226         return (COSBase)objects.get( index );
227     }
228     
229     /**
230      * Get the value of the array as an integer.
231      *
232      * @param index The index into the list.
233      *
234      * @return The value at that index or -1 if it is null.
235      */

236     public int getInt( int index )
237     {
238         return getInt( index, -1 );
239     }
240     
241     /**
242      * Get the value of the array as an integer, return the default if it does
243      * not exist.
244      *
245      * @param index The value of the array.
246      * @param defaultValue The value to return if the value is null.
247      * @return The value at the index or the defaultValue.
248      */

249     public int getInt( int index, int defaultValue )
250     {
251         int retval = defaultValue;
252         if( defaultValue < size() )
253         {
254             COSNumber number = (COSNumber)get( index );
255             if( number != null )
256             {
257                 retval = number.intValue();
258             }
259         }
260         return retval;
261     }
262     
263     /**
264      * Set the value in the array as an integer.
265      *
266      * @param index The index into the array.
267      * @param value The value to set.
268      */

269     public void setInt( int index, int value )
270     {
271         set( index, new COSInteger( value ) );
272     }
273     
274     /**
275      * Set the value in the array as a name.
276      * @param index The index into the array.
277      * @param name The name to set in the array.
278      */

279     public void setName( int index, String JavaDoc name )
280     {
281         set( index, COSName.getPDFName( name ) );
282     }
283     
284     /**
285      * Get the value of the array as a string.
286      *
287      * @param index The index into the array.
288      * @return The name converted to a string or null if it does not exist.
289      */

290     public String JavaDoc getName( int index )
291     {
292         return getName( index, null );
293     }
294     
295     /**
296      * Get an entry in the array that is expected to be a COSName.
297      * @param index The index into the array.
298      * @param defaultValue The value to return if it is null.
299      * @return The value at the index or defaultValue if none is found.
300      */

301     public String JavaDoc getName( int index, String JavaDoc defaultValue )
302     {
303         String JavaDoc retval = defaultValue;
304         if( index < size() )
305         {
306             COSName name = (COSName)get( index );
307             if( name != null )
308             {
309                 retval = name.getName();
310             }
311         }
312         return retval;
313     }
314     
315     /**
316      * Set the value in the array as a string.
317      * @param index The index into the array.
318      * @param string The string to set in the array.
319      */

320     public void setString( int index, String JavaDoc string )
321     {
322         set( index, new COSString( string ) );
323     }
324     
325     /**
326      * Get the value of the array as a string.
327      *
328      * @param index The index into the array.
329      * @return The string or null if it does not exist.
330      */

331     public String JavaDoc getString( int index )
332     {
333         return getString( index, null );
334     }
335     
336     /**
337      * Get an entry in the array that is expected to be a COSName.
338      * @param index The index into the array.
339      * @param defaultValue The value to return if it is null.
340      * @return The value at the index or defaultValue if none is found.
341      */

342     public String JavaDoc getString( int index, String JavaDoc defaultValue )
343     {
344         String JavaDoc retval = defaultValue;
345         if( index < size() )
346         {
347             COSString string = (COSString)get( index );
348             if( string != null )
349             {
350                 retval = string.getString();
351             }
352         }
353         return retval;
354     }
355
356     /**
357      * This will get the size of this array.
358      *
359      * @return The number of elements in the array.
360      */

361     public int size()
362     {
363         return objects.size();
364     }
365
366     /**
367      * This will remove an element from the array.
368      *
369      * @param i The index of the object to remove.
370      *
371      * @return The object that was removed.
372      */

373     public COSBase remove( int i )
374     {
375         return (COSBase)objects.remove( i );
376     }
377
378     /**
379      * This will remove an element from the array.
380      *
381      * @param o The object to remove.
382      *
383      * @return The object that was removed.
384      */

385     public boolean remove( COSBase o )
386     {
387         return objects.remove( o );
388     }
389
390     /**
391      * {@inheritDoc}
392      */

393     public String JavaDoc toString()
394     {
395         return "COSArray{" + objects + "}";
396     }
397
398     /**
399      * Get access to the list.
400      *
401      * @return an iterator over the array elements
402      */

403     public Iterator JavaDoc iterator()
404     {
405         return objects.iterator();
406     }
407
408     /**
409      * This will return the index of the entry or -1 if it is not found.
410      *
411      * @param object The object to search for.
412      * @return The index of the object or -1.
413      */

414     public int indexOf( COSBase object )
415     {
416         int retval = -1;
417         for( int i=0; retval < 0 && i<size(); i++ )
418         {
419             if( get( i ).equals( object ) )
420             {
421                 retval = i;
422             }
423         }
424         return retval;
425     }
426     
427     /**
428      * This will add null values until the size of the array is at least
429      * as large as the parameter. If the array is already larger than the
430      * parameter then nothing is done.
431      *
432      * @param size The desired size of the array.
433      */

434     public void growToSize( int size )
435     {
436         growToSize( size, null );
437     }
438     
439     /**
440      * This will add the object until the size of the array is at least
441      * as large as the parameter. If the array is already larger than the
442      * parameter then nothing is done.
443      *
444      * @param size The desired size of the array.
445      * @param object The object to fill the array with.
446      */

447     public void growToSize( int size, COSBase object )
448     {
449         while( size() < size )
450         {
451             add( object );
452         }
453     }
454
455     /**
456      * visitor pattern double dispatch method.
457      *
458      * @param visitor The object to notify when visiting this object.
459      * @return any object, depending on the visitor implementation, or null
460      * @throws COSVisitorException If an error occurs while visiting this object.
461      */

462     public Object JavaDoc accept(ICOSVisitor visitor) throws COSVisitorException
463     {
464         return visitor.visitFromArray(this);
465     }
466     
467     /**
468      * This will take an COSArray of numbers and convert it to a float[].
469      *
470      * @return This COSArray as an array of float numbers.
471      */

472     public float[] toFloatArray()
473     {
474         float[] retval = new float[size()];
475         for( int i=0; i<size(); i++ )
476         {
477             retval[i] = ((COSNumber)getObject( i )).floatValue();
478         }
479         return retval;
480     }
481     
482     /**
483      * Clear the current contents of the COSArray and set it with the float[].
484      *
485      * @param value The new value of the float array.
486      */

487     public void setFloatArray( float[] value )
488     {
489         this.clear();
490         for( int i=0; i<value.length; i++ )
491         {
492             add( new COSFloat( value[i] ) );
493         }
494     }
495 }
Popular Tags