001: /*
002: * Copyright (c) 2007, intarsys consulting GmbH
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * - Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * - 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: *
014: * - Neither the name of intarsys nor the names of its contributors may be used
015: * to endorse or promote products derived from this software without specific
016: * prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: package de.intarsys.pdf.fd;
031:
032: import java.util.ArrayList;
033: import java.util.Iterator;
034: import java.util.List;
035: import de.intarsys.pdf.cos.COSArray;
036: import de.intarsys.pdf.cos.COSBasedObject;
037: import de.intarsys.pdf.cos.COSDictionary;
038: import de.intarsys.pdf.cos.COSFixed;
039: import de.intarsys.pdf.cos.COSName;
040: import de.intarsys.pdf.cos.COSNumber;
041: import de.intarsys.pdf.cos.COSObject;
042:
043: /**
044: * This is the abstract superclass for all complex FD level objects.
045: *
046: */
047: abstract public class FDObject extends COSBasedObject {
048: /**
049: * The meta class implementation
050: */
051: static public class MetaClass extends COSBasedObject.MetaClass {
052: protected MetaClass(Class instanceClass) {
053: super (instanceClass);
054: }
055: }
056:
057: /** The meta class instance */
058: static public final MetaClass META = new MetaClass(MetaClass.class
059: .getDeclaringClass());
060:
061: /**
062: * Create the receiver class from an already defined {@link COSDictionary}.
063: * NEVER use the constructor directly.
064: *
065: * @param object
066: * the PDDocument containing the new object
067: */
068: protected FDObject(COSObject object) {
069: super (object);
070: }
071:
072: /*
073: * (non-Javadoc)
074: *
075: * @see java.lang.Object#toString()
076: */
077: public String toString() {
078: String result = "[]";
079: if (cosGetObject().isIndirect()) {
080: result = "["
081: + cosGetObject().getIndirectObject().toString()
082: + "]";
083: }
084: return result;
085: }
086:
087: protected void cosMoveField(COSName key, COSDictionary source,
088: COSDictionary dest) {
089: // review warum move
090: COSObject o = source.get(key);
091: if (!o.isNull()) {
092: source.remove(key);
093: dest.put(key, o);
094: }
095: }
096:
097: protected float[] getFloatArray(COSName key) {
098: COSArray array = cosGetField(key).asArray();
099: if (array == null) {
100: return null;
101: }
102: float[] result = new float[array.size()];
103: for (int i = 0; i < array.size(); i++) {
104: COSNumber num = (COSNumber) array.get(i);
105: result[i] = num.floatValue();
106: }
107: return result;
108: }
109:
110: protected List getFDObjects(COSName key,
111: COSBasedObject.MetaClass metaclass) {
112: COSArray array = cosGetField(key).asArray();
113: if (array == null) {
114: return null;
115: }
116: List result = new ArrayList();
117: for (Iterator i = array.iterator(); i.hasNext();) {
118: result.add(metaclass.createFromCos((COSObject) i.next()));
119: }
120: return result;
121: }
122:
123: protected void setFloatArray(COSName key, float[] array,
124: boolean emptyArrayIfListEmpty) {
125: if ((array == null) || (array.length == 0)) {
126: if (emptyArrayIfListEmpty) {
127: cosSetField(key, COSArray.create());
128: } else {
129: cosRemoveField(key);
130: }
131: return;
132: }
133:
134: // todo 3 reuse existing array?
135: COSArray a = COSArray.create();
136: cosSetField(key, a);
137:
138: for (int i = 0; i < array.length; i++) {
139: a.add(COSFixed.create(array[i]));
140: }
141: }
142:
143: protected void setFDObjects(COSName key, List list,
144: boolean emptyArrayIfListEmpty) {
145: // todo 2 ugly
146: if ((list == null) || list.isEmpty()) {
147: if (emptyArrayIfListEmpty) {
148: cosSetField(key, COSArray.create());
149: } else {
150: cosRemoveField(key);
151: }
152: return;
153: }
154:
155: // todo 2 reuse existing array ?
156: COSArray a = COSArray.create();
157: cosSetField(key, a);
158:
159: for (Iterator i = list.iterator(); i.hasNext();) {
160: a.add(((FDObject) i.next()).cosGetObject());
161: }
162: }
163: }
|