001: /*
002: Copyright © 2006,2007 Stefano Chizzolini. http://clown.stefanochizzolini.it
003:
004: Contributors:
005: * Stefano Chizzolini (original code developer, http://www.stefanochizzolini.it):
006: contributed code is Copyright © 2006,2007 by Stefano Chizzolini.
007:
008: This file should be part of the source code distribution of "PDF Clown library"
009: (the Program): see the accompanying README files for more info.
010:
011: This Program is free software; you can redistribute it and/or modify it under
012: the terms of the GNU General Public License as published by the Free Software
013: Foundation; either version 2 of the License, or (at your option) any later version.
014:
015: This Program is distributed in the hope that it will be useful, but WITHOUT ANY
016: WARRANTY, either expressed or implied; without even the implied warranty of
017: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more details.
018:
019: You should have received a copy of the GNU General Public License along with this
020: Program (see README files); if not, go to the GNU website (http://www.gnu.org/).
021:
022: Redistribution and use, with or without modification, are permitted provided that such
023: redistributions retain the above copyright notice, license and disclaimer, along with
024: this list of conditions.
025: */
026:
027: package it.stefanochizzolini.clown.objects;
028:
029: import it.stefanochizzolini.clown.bytes.IOutputStream;
030: import it.stefanochizzolini.clown.files.File;
031: import it.stefanochizzolini.clown.util.NotImplementedException;
032:
033: import java.util.ArrayList;
034: import java.util.Collection;
035: import java.util.Iterator;
036: import java.util.List;
037: import java.util.ListIterator;
038:
039: /**
040: PDF array object.
041: @version 0.0.5
042: @since 0.0.0
043: */
044: public class PdfArray extends PdfDirectObject implements
045: List<PdfDirectObject> {
046: // <class>
047: // <dynamic>
048: // <fields>
049: private ArrayList<PdfDirectObject> items;
050:
051: // </fields>
052:
053: // <constructors>
054: public PdfArray() {
055: items = new ArrayList<PdfDirectObject>();
056: }
057:
058: public PdfArray(int capacity) {
059: items = new ArrayList<PdfDirectObject>(capacity);
060: }
061:
062: public PdfArray(PdfDirectObject[] items) {
063: this (items.length);
064:
065: for (PdfDirectObject item : items) {
066: this .items.add(item);
067: }
068: }
069:
070: // </constructors>
071:
072: // <interface>
073: // <public>
074: @Override
075: public Object clone(File context) {
076: //TODO:IMPL redefine to support real cloning (current implementation is prone to object slicing hazard)!!!
077: PdfArray clone = new PdfArray(items.size());
078:
079: for (PdfDirectObject item : items) {
080: clone.add((PdfDirectObject) item.clone(context));
081: }
082:
083: return clone;
084: }
085:
086: /**
087: @version 0.0.5
088: */
089: @Override
090: public String toPdf() {
091: StringBuilder buffer = new StringBuilder();
092:
093: // Begin.
094: buffer.append("[ ");
095:
096: // Elements.
097: for (PdfDirectObject item : items) {
098: buffer.append(item.toPdf() + " ");
099: }
100:
101: // End.
102: buffer.append("]");
103:
104: return buffer.toString();
105: }
106:
107: @Override
108: public String toString() {
109: StringBuilder buffer = new StringBuilder();
110:
111: // Begin.
112: buffer.append("[ ");
113:
114: // Elements.
115: for (PdfDirectObject item : items) {
116: buffer.append(item.toString() + " ");
117: }
118:
119: // End.
120: buffer.append("]");
121:
122: return buffer.toString();
123: }
124:
125: // <List>
126: public void add(int index, PdfDirectObject object) {
127: items.add(index, object);
128: }
129:
130: public boolean addAll(int index,
131: Collection<? extends PdfDirectObject> objects) {
132: return items.addAll(index, objects);
133: }
134:
135: public PdfDirectObject get(int index) {
136: return items.get(index);
137: }
138:
139: public int indexOf(Object object) {
140: return items.indexOf(object);
141: }
142:
143: public int lastIndexOf(Object object) {
144: return items.lastIndexOf(object);
145: }
146:
147: public ListIterator<PdfDirectObject> listIterator() {
148: return items.listIterator();
149: }
150:
151: public ListIterator<PdfDirectObject> listIterator(int index) {
152: return items.listIterator(index);
153: }
154:
155: public PdfDirectObject remove(int index) {
156: return items.remove(index);
157: }
158:
159: public PdfDirectObject set(int index, PdfDirectObject object) {
160: return items.set(index, object);
161: }
162:
163: public List<PdfDirectObject> subList(int fromIndex, int toIndex) {
164: return items.subList(fromIndex, toIndex);
165: }
166:
167: // <Collection>
168: public boolean add(PdfDirectObject object) {
169: return items.add(object);
170: }
171:
172: public boolean addAll(Collection<? extends PdfDirectObject> objects) {
173: return items.addAll(objects);
174: }
175:
176: public void clear() {
177: items.clear();
178: }
179:
180: public boolean contains(Object object) {
181: return items.contains(object);
182: }
183:
184: public boolean containsAll(Collection<?> objects) {
185: return items.containsAll(objects);
186: }
187:
188: public boolean equals(PdfDirectObject object) {
189: throw new NotImplementedException();
190: }
191:
192: public int hashCode() {
193: return items.hashCode();
194: }
195:
196: public boolean isEmpty() {
197: return items.isEmpty();
198: }
199:
200: public boolean remove(Object object) {
201: return items.remove(object);
202: }
203:
204: public boolean removeAll(Collection<?> objects) {
205: return items.removeAll(objects);
206: }
207:
208: public boolean retainAll(Collection<?> objects) {
209: return items.retainAll(objects);
210: }
211:
212: public int size() {
213: return items.size();
214: }
215:
216: public PdfDirectObject[] toArray() {
217: return (PdfDirectObject[]) items.toArray();
218: }
219:
220: public <PdfDirectObject> PdfDirectObject[] toArray(
221: PdfDirectObject[] objects) {
222: return (PdfDirectObject[]) items.toArray(objects);
223: }
224:
225: // <Iterable>
226: public Iterator<PdfDirectObject> iterator() {
227: return items.iterator();
228: }
229:
230: // </Iterable>
231: // </Collection>
232: // </List>
233: // </public>
234:
235: // <internal>
236: int writeTo(IOutputStream stream) {
237: // Begin.
238: int size = stream.write("[ ");
239:
240: // Elements.
241: for (PdfDirectObject item : items) {
242: size += item.writeTo(stream);
243: size += stream.write(" ");
244: }
245:
246: // End.
247: size += stream.write("]");
248:
249: return size;
250: }
251: // </internal>
252: // </interface>
253: // </dynamic>
254: // </class>
255: }
|