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.pd;
031:
032: import de.intarsys.pdf.cos.COSBasedObject;
033: import de.intarsys.pdf.cos.COSDictionary;
034: import de.intarsys.pdf.cos.COSName;
035: import de.intarsys.pdf.cos.COSObject;
036: import de.intarsys.pdf.cos.COSStream;
037:
038: /**
039: * The representation of a XObject. A XObject defines a strokable object. The
040: * content is defined in the underlying content stream.
041: */
042: abstract public class PDXObject extends PDObject {
043: /**
044: * The meta class implementation
045: */
046: static public class MetaClass extends PDObject.MetaClass {
047: protected MetaClass(Class instanceClass) {
048: super (instanceClass);
049: }
050:
051: protected COSObject doCreateCOSObject() {
052: return COSStream.create(null);
053: }
054:
055: protected COSBasedObject.MetaClass doDetermineClass(
056: COSObject object) {
057: COSDictionary dict;
058: if (object instanceof COSStream) {
059: dict = ((COSStream) object).getDict();
060: } else {
061: dict = (COSDictionary) object;
062: }
063: COSName subtype = (COSName) dict.get(DK_Subtype).asName();
064: if (PDForm.CN_Subtype_Form.equals(subtype)) {
065: return PDForm.META;
066: } else if (PDImage.CN_Subtype_Image.equals(subtype)) {
067: return PDImage.META;
068: } else if (PDPostScript.CN_Subtype_PS.equals(subtype)) {
069: return PDPostScript.META;
070: } else {
071: return super .doDetermineClass(object);
072: }
073: }
074:
075: public Class getRootClass() {
076: return PDXObject.class;
077: }
078: }
079:
080: /** The meta class instance */
081: static public final MetaClass META = new MetaClass(MetaClass.class
082: .getDeclaringClass());
083:
084: //
085: static public final COSName DK_Resources = COSName
086: .constant("Resources");
087:
088: static public final COSName CN_Type_XObject = COSName
089: .constant("XObject");
090:
091: /**
092: * Create the receiver class from an already defined {@link COSStream}.
093: * NEVER use the constructor directly.
094: *
095: * @param object
096: * the PDDocument containing the new object
097: */
098: protected PDXObject(COSObject object) {
099: super (object);
100: }
101:
102: /*
103: * (non-Javadoc)
104: *
105: * @see de.intarsys.pdf.cos.COSBasedObject#cosGetDict()
106: */
107: public COSDictionary cosGetDict() {
108: return cosGetStream().getDict();
109: }
110:
111: /*
112: * (non-Javadoc)
113: *
114: * @see de.intarsys.pdf.pd.PDObject#cosGetExpectedType()
115: */
116: protected COSName cosGetExpectedType() {
117: return CN_Type_XObject;
118: }
119:
120: /**
121: * The data representing the XObject
122: *
123: * @return The data representing the XObject
124: */
125: public byte[] getBytes() {
126: return cosGetStream().getDecodedBytes();
127: }
128:
129: /**
130: * <code>true</code> if this is a form.
131: *
132: * @return <code>true</code> if this is a form.
133: */
134: public boolean isForm() {
135: return false;
136: }
137:
138: /**
139: * <code>true</code> if this is an image.
140: *
141: * @return <code>true</code> if this is an image.
142: */
143: public boolean isImage() {
144: return false;
145: }
146:
147: /**
148: * <code>true</code> if this is a postscript object.
149: *
150: * @return <code>true</code> if this is a postscript object.
151: */
152: public boolean isPostscript() {
153: return false;
154: }
155:
156: /**
157: * Set the data representing the XObject
158: *
159: * @param bytes
160: * The data representing the XObject
161: */
162: public void setBytes(byte[] bytes) {
163: cosGetStream().setDecodedBytes(bytes);
164: }
165: }
|