From 6025b6016517c6d898d8957d1d7e03ba71431912 Mon Sep 17 00:00:00 2001 From: tknall Date: Fri, 1 Dec 2006 12:20:24 +0000 Subject: Initial import of release 2.2. git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@4 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- src/main/java/org/pdfbox/cos/COSArray.java | 492 +++++++++++++++++++++++++++++ 1 file changed, 492 insertions(+) create mode 100644 src/main/java/org/pdfbox/cos/COSArray.java (limited to 'src/main/java/org/pdfbox/cos/COSArray.java') diff --git a/src/main/java/org/pdfbox/cos/COSArray.java b/src/main/java/org/pdfbox/cos/COSArray.java new file mode 100644 index 0000000..cb7d278 --- /dev/null +++ b/src/main/java/org/pdfbox/cos/COSArray.java @@ -0,0 +1,492 @@ +/** + * Copyright (c) 2003-2005, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.cos; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; + + + +import org.pdfbox.exceptions.COSVisitorException; +import org.pdfbox.pdmodel.common.COSObjectable; + +/** + * An array of PDFBase objects as part of the PDF document. + * + * @author Ben Litchfield (ben@benlitchfield.com) + * @version $Revision: 1.22 $ + */ +public class COSArray extends COSBase +{ + private List objects = new ArrayList(); + + /** + * Constructor. + */ + public COSArray() + { + //default constructor + } + + /** + * This will add an object to the array. + * + * @param object The object to add to the array. + */ + public void add( COSBase object ) + { + objects.add( object ); + } + + /** + * This will add an object to the array. + * + * @param object The object to add to the array. + */ + public void add( COSObjectable object ) + { + objects.add( object.getCOSObject() ); + } + + /** + * Add the specified object at the ith location and push the rest to the + * right. + * + * @param i The index to add at. + * @param object The object to add at that index. + */ + public void add( int i, COSBase object) + { + objects.add( i, object ); + } + + /** + * This will remove all of the objects in the collection. + */ + public void clear() + { + objects.clear(); + } + + /** + * This will remove all of the objects in the collection. + * + * @param objectsList The list of objects to remove from the collection. + */ + public void removeAll( Collection objectsList ) + { + objects.removeAll( objectsList ); + } + + /** + * This will retain all of the objects in the collection. + * + * @param objectsList The list of objects to retain from the collection. + */ + public void retainAll( Collection objectsList ) + { + objects.retainAll( objectsList ); + } + + /** + * This will add an object to the array. + * + * @param objectsList The object to add to the array. + */ + public void addAll( Collection objectsList ) + { + objects.addAll( objectsList ); + } + + /** + * This will add all objects to this array. + * + * @param objectList The objects to add. + */ + public void addAll( COSArray objectList ) + { + objects.addAll( objectList.objects ); + } + + /** + * Add the specified object at the ith location and push the rest to the + * right. + * + * @param i The index to add at. + * @param objectList The object to add at that index. + */ + public void addAll( int i, Collection objectList ) + { + objects.addAll( i, objectList ); + } + + /** + * This will set an object at a specific index. + * + * @param index zero based index into array. + * @param object The object to set. + */ + public void set( int index, COSBase object ) + { + objects.set( index, object ); + } + + /** + * This will set an object at a specific index. + * + * @param index zero based index into array. + * @param intVal The object to set. + */ + public void set( int index, int intVal ) + { + objects.set( index, new COSInteger( intVal ) ); + } + + /** + * This will set an object at a specific index. + * + * @param index zero based index into array. + * @param object The object to set. + */ + public void set( int index, COSObjectable object ) + { + COSBase base = null; + if( object != null ) + { + base = object.getCOSObject(); + } + objects.set( index, base ); + } + + /** + * This will get an object from the array. This will dereference the object. + * If the object is COSNull then null will be returned. + * + * @param index The index into the array to get the object. + * + * @return The object at the requested index. + */ + public COSBase getObject( int index ) + { + Object obj = objects.get( index ); + if( obj instanceof COSObject ) + { + obj = ((COSObject)obj).getObject(); + } + if( obj instanceof COSNull ) + { + obj = null; + } + return (COSBase)obj; + } + + /** + * This will get an object from the array. This will NOT derefernce + * the COS object. + * + * @param index The index into the array to get the object. + * + * @return The object at the requested index. + */ + public COSBase get( int index ) + { + return (COSBase)objects.get( index ); + } + + /** + * Get the value of the array as an integer. + * + * @param index The index into the list. + * + * @return The value at that index or -1 if it is null. + */ + public int getInt( int index ) + { + return getInt( index, -1 ); + } + + /** + * Get the value of the array as an integer, return the default if it does + * not exist. + * + * @param index The value of the array. + * @param defaultValue The value to return if the value is null. + * @return The value at the index or the defaultValue. + */ + public int getInt( int index, int defaultValue ) + { + int retval = defaultValue; + if( defaultValue < size() ) + { + COSNumber number = (COSNumber)get( index ); + if( number != null ) + { + retval = number.intValue(); + } + } + return retval; + } + + /** + * Set the value in the array as an integer. + * + * @param index The index into the array. + * @param value The value to set. + */ + public void setInt( int index, int value ) + { + set( index, new COSInteger( value ) ); + } + + /** + * Set the value in the array as a name. + * @param index The index into the array. + * @param name The name to set in the array. + */ + public void setName( int index, String name ) + { + set( index, COSName.getPDFName( name ) ); + } + + /** + * Get the value of the array as a string. + * + * @param index The index into the array. + * @return The name converted to a string or null if it does not exist. + */ + public String getName( int index ) + { + return getName( index, null ); + } + + /** + * Get an entry in the array that is expected to be a COSName. + * @param index The index into the array. + * @param defaultValue The value to return if it is null. + * @return The value at the index or defaultValue if none is found. + */ + public String getName( int index, String defaultValue ) + { + String retval = defaultValue; + if( index < size() ) + { + COSName name = (COSName)get( index ); + if( name != null ) + { + retval = name.getName(); + } + } + return retval; + } + + /** + * Set the value in the array as a string. + * @param index The index into the array. + * @param string The string to set in the array. + */ + public void setString( int index, String string ) + { + set( index, new COSString( string ) ); + } + + /** + * Get the value of the array as a string. + * + * @param index The index into the array. + * @return The string or null if it does not exist. + */ + public String getString( int index ) + { + return getString( index, null ); + } + + /** + * Get an entry in the array that is expected to be a COSName. + * @param index The index into the array. + * @param defaultValue The value to return if it is null. + * @return The value at the index or defaultValue if none is found. + */ + public String getString( int index, String defaultValue ) + { + String retval = defaultValue; + if( index < size() ) + { + COSString string = (COSString)get( index ); + if( string != null ) + { + retval = string.getString(); + } + } + return retval; + } + + /** + * This will get the size of this array. + * + * @return The number of elements in the array. + */ + public int size() + { + return objects.size(); + } + + /** + * This will remove an element from the array. + * + * @param i The index of the object to remove. + * + * @return The object that was removed. + */ + public COSBase remove( int i ) + { + return (COSBase)objects.remove( i ); + } + + /** + * This will remove an element from the array. + * + * @param o The object to remove. + * + * @return The object that was removed. + */ + public boolean remove( COSBase o ) + { + return objects.remove( o ); + } + + /** + * @see Object#toString() + */ + public String toString() + { + return "COSArray{" + objects + "}"; + } + + /** + * Get access to the list. + * + * @return an iterator over the array elements + */ + public Iterator iterator() + { + return objects.iterator(); + } + + /** + * This will return the index of the entry or -1 if it is not found. + * + * @param object The object to search for. + * @return The index of the object or -1. + */ + public int indexOf( COSBase object ) + { + int retval = -1; + for( int i=0; retval < 0 && i