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.COSString;
037:
038: /**
039: * Repesents the PDF's FileSpecification-type
040: *
041: */
042: public class PDFileSpecification extends PDObject {
043: // todo 2 review file system attribute
044:
045: /**
046: * The meta class implementation
047: */
048: static public class MetaClass extends PDObject.MetaClass {
049: protected MetaClass(Class instanceClass) {
050: super (instanceClass);
051: }
052:
053: public Class getRootClass() {
054: return PDFileSpecification.class;
055: }
056:
057: /*
058: * (non-Javadoc)
059: *
060: * @see de.intarsys.pdf.cos.COSBasedObject.MetaClass#doDetermineClass(de.intarsys.pdf.cos.COSObject)
061: */
062: protected COSBasedObject.MetaClass doDetermineClass(
063: COSObject object) {
064: // Type entry is optional!
065: // COSName type = ((COSDictionary) object).get(DK_Type).asName();
066: // if (!CN_Type_Filespec.equals(type)) {
067: // throw new IllegalArgumentException(
068: // "not a File Specification Dictionary");
069: // }
070: COSName fileSystem = ((COSDictionary) object).get(DK_FS)
071: .asName();
072: if (CN_FS_URL.equals(fileSystem)) {
073: return PDFileSpecificationURL.META;
074: }
075: return PDFileSpecification.META;
076: }
077: }
078:
079: static public final COSName CN_Type_Filespec = COSName
080: .constant("Filespec");
081:
082: static public final COSName CN_Type_Alt_Filespec = COSName
083: .constant("F");
084:
085: /** The meta class instance */
086: static public final MetaClass META = new MetaClass(MetaClass.class
087: .getDeclaringClass());
088:
089: /** Name of the file system. */
090: static public final COSName DK_FS = COSName.constant("FS");
091:
092: /** File */
093: static public final COSName DK_F = COSName.constant("F");
094:
095: /** File specification string for DOS files */
096: static public final COSName CN_F_DOS = COSName.constant("DOS");
097:
098: /** File specification string for Macintosh files */
099: static public final COSName CN_F_Mac = COSName.constant("Mac");
100:
101: /** File specification string for UNIX files */
102: static public final COSName CN_F_Unix = COSName.constant("Unix");
103:
104: /** File URL */
105: static public final COSName CN_FS_URL = COSName.constant("URL");
106:
107: /** Embedded file dict */
108: static public final COSName DK_EF = COSName.constant("EF");
109:
110: /** Embedded file array dict. */
111: static public final COSName DK_RF = COSName.constant("RF");
112:
113: private COSName fileSystem = null;
114:
115: protected PDFileSpecification(COSObject object) {
116: super (object);
117: }
118:
119: public void setFileSystem(COSName bs) {
120: fileSystem = bs;
121: }
122:
123: public COSName getFileSystem() {
124: return fileSystem;
125: }
126:
127: /*
128: * (non-Javadoc)
129: *
130: * @see de.intarsys.pdf.pd.PDObject#cosGetExpectedType()
131: */
132: protected COSName cosGetExpectedType() {
133: return CN_Type_Filespec;
134: }
135:
136: /**
137: * provide some hook for initialization after creation based on a cos object
138: */
139: protected void initializeFromCos() {
140: super .initializeFromCos();
141: }
142:
143: /**
144: * provide some hook for initialization after creation from scratch
145: */
146: protected void initializeFromScratch() {
147: super .initializeFromScratch();
148: cosSetField(DK_FS, getFileSystem());
149: }
150:
151: public String getFile() {
152: COSString cosFile = cosGetField(DK_F).asString();
153: if (cosFile == null) {
154: return null;
155: }
156: return cosFile.stringValue();
157: }
158:
159: /**
160: * The embedded file of a specific flavor (one of F, DOS, Mac, Unix),
161: *
162: * @param flavor
163: * One of F, DOC, Mac, Unix
164: * @return the embedded file of this flavor, if any, or null.
165: */
166: public PDEmbeddedFile getEmbeddedFile(COSName flavor) {
167: COSDictionary embeddedFileDict = getEmbeddedFiles();
168: if (embeddedFileDict == null) {
169: return null;
170: }
171: return (PDEmbeddedFile) PDEmbeddedFile.META
172: .createFromCos(embeddedFileDict.get(flavor));
173: }
174:
175: public void setEmbeddedFile(COSName flavor,
176: PDEmbeddedFile embeddedFile) {
177: COSDictionary embeddedFileDict = getEmbeddedFiles();
178: if (embeddedFileDict == null) {
179: embeddedFileDict = COSDictionary.create();
180: cosSetField(DK_EF, embeddedFileDict);
181: }
182: embeddedFileDict.put(flavor, embeddedFile.cosGetObject());
183: }
184:
185: public String getFileSpecificationString(COSName flavor) {
186: return cosGetField(flavor).stringValue();
187: }
188:
189: public void setFileSpecificationString(COSName flavor, String spec) {
190: setFieldString(flavor, spec);
191: }
192:
193: /**
194: * The dictionary mapping the F/DOC/Mac/Unix entries of the file spec to
195: * embedded file dictionaries.
196: */
197: public COSDictionary getEmbeddedFiles() {
198: return cosGetField(DK_EF).asDictionary();
199: }
200:
201: public void setFile(String file) {
202: setFieldString(DK_F, file);
203: }
204: }
|