001: /**
002: * Copyright (c) 2004, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. 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: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.pdmodel.fdf;
031:
032: import java.io.IOException;
033: import java.io.Writer;
034: import java.util.ArrayList;
035: import java.util.List;
036:
037: import org.pdfbox.cos.COSArray;
038: import org.pdfbox.cos.COSBase;
039: import org.pdfbox.cos.COSDictionary;
040: import org.pdfbox.cos.COSStream;
041: import org.pdfbox.cos.COSString;
042:
043: import org.pdfbox.pdmodel.common.COSObjectable;
044: import org.pdfbox.pdmodel.common.COSArrayList;
045: import org.pdfbox.pdmodel.common.filespecification.PDFileSpecification;
046: import org.pdfbox.pdmodel.common.filespecification.PDSimpleFileSpecification;
047: import org.w3c.dom.Element;
048: import org.w3c.dom.Node;
049: import org.w3c.dom.NodeList;
050:
051: /**
052: * This represents an FDF dictionary that is part of the FDF document.
053: *
054: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
055: * @version $Revision: 1.8 $
056: */
057: public class FDFDictionary implements COSObjectable {
058: private COSDictionary fdf;
059:
060: /**
061: * Default constructor.
062: */
063: public FDFDictionary() {
064: fdf = new COSDictionary();
065: }
066:
067: /**
068: * Constructor.
069: *
070: * @param fdfDictionary The FDF documents catalog.
071: */
072: public FDFDictionary(COSDictionary fdfDictionary) {
073: fdf = fdfDictionary;
074: }
075:
076: /**
077: * This will create an FDF dictionary from an XFDF XML document.
078: *
079: * @param fdfXML The XML document that contains the XFDF data.
080: * @throws IOException If there is an error reading from the dom.
081: */
082: public FDFDictionary(Element fdfXML) throws IOException {
083: this ();
084: NodeList nodeList = fdfXML.getChildNodes();
085: for (int i = 0; i < nodeList.getLength(); i++) {
086: Node node = nodeList.item(i);
087: if (node instanceof Element) {
088: Element child = (Element) node;
089: if (child.getTagName().equals("f")) {
090: PDSimpleFileSpecification fs = new PDSimpleFileSpecification();
091: fs.setFile(child.getAttribute("href"));
092:
093: } else if (child.getTagName().equals("ids")) {
094: COSArray ids = new COSArray();
095: String original = child.getAttribute("original");
096: String modified = child.getAttribute("modified");
097: ids.add(COSString.createFromHexString(original));
098: ids.add(COSString.createFromHexString(modified));
099: setID(ids);
100: } else if (child.getTagName().equals("fields")) {
101: NodeList fields = child
102: .getElementsByTagName("field");
103: List fieldList = new ArrayList();
104: for (int f = 0; f < fields.getLength(); f++) {
105: fieldList.add(new FDFField((Element) fields
106: .item(f)));
107: }
108: setFields(fieldList);
109: }
110: }
111: }
112: }
113:
114: /**
115: * This will write this element as an XML document.
116: *
117: * @param output The stream to write the xml to.
118: *
119: * @throws IOException If there is an error writing the XML.
120: */
121: public void writeXML(Writer output) throws IOException {
122: PDFileSpecification fs = this .getFile();
123: if (fs != null) {
124: output.write("<f href=\"" + fs.getFile() + "\" />\n");
125: }
126: COSArray ids = this .getID();
127: if (ids != null) {
128: COSString original = (COSString) ids.getObject(0);
129: COSString modified = (COSString) ids.getObject(1);
130: output.write("<ids original=\"" + original.getHexString()
131: + "\" ");
132: output.write("modified=\"" + modified.getHexString()
133: + "\" />\n");
134: }
135: List fields = getFields();
136: if (fields != null && fields.size() > 0) {
137: output.write("<fields>\n");
138: for (int i = 0; i < fields.size(); i++) {
139: ((FDFField) fields.get(i)).writeXML(output);
140: }
141: output.write("</fields>\n");
142: }
143: }
144:
145: /**
146: * Convert this standard java object to a COS object.
147: *
148: * @return The cos object that matches this Java object.
149: */
150: public COSBase getCOSObject() {
151: return fdf;
152: }
153:
154: /**
155: * Convert this standard java object to a COS object.
156: *
157: * @return The cos object that matches this Java object.
158: */
159: public COSDictionary getCOSDictionary() {
160: return fdf;
161: }
162:
163: /**
164: * The source file or target file: the PDF document file that
165: * this FDF file was exported from or is intended to be imported into.
166: *
167: * @return The F entry of the FDF dictionary.
168: *
169: * @throws IOException If there is an error creating the file spec.
170: */
171: public PDFileSpecification getFile() throws IOException {
172: return PDFileSpecification.createFS(fdf
173: .getDictionaryObject("F"));
174: }
175:
176: /**
177: * This will set the file specification.
178: *
179: * @param fs The file specification.
180: */
181: public void setFile(PDFileSpecification fs) {
182: fdf.setItem("F", fs);
183: }
184:
185: /**
186: * This is the FDF id.
187: *
188: * @return The FDF ID.
189: */
190: public COSArray getID() {
191: return (COSArray) fdf.getDictionaryObject("ID");
192: }
193:
194: /**
195: * This will set the FDF id.
196: *
197: * @param id The new id for the FDF.
198: */
199: public void setID(COSArray id) {
200: fdf.setItem("ID", id);
201: }
202:
203: /**
204: * This will get the list of FDF Fields. This will return a list of FDFField
205: * objects.
206: *
207: * @return A list of FDF fields.
208: */
209: public List getFields() {
210: List retval = null;
211: COSArray fieldArray = (COSArray) fdf
212: .getDictionaryObject("Fields");
213: if (fieldArray != null) {
214: List fields = new ArrayList();
215: for (int i = 0; i < fieldArray.size(); i++) {
216: fields.add(new FDFField((COSDictionary) fieldArray
217: .getObject(i)));
218: }
219: retval = new COSArrayList(fields, fieldArray);
220: }
221: return retval;
222: }
223:
224: /**
225: * This will set the list of fields. This should be a list of FDFField objects.
226: *
227: * @param fields The list of fields.
228: */
229: public void setFields(List fields) {
230: fdf.setItem("Fields", COSArrayList.converterToCOSArray(fields));
231: }
232:
233: /**
234: * This will get the status string to be displayed as the result of an
235: * action.
236: *
237: * @return The status.
238: */
239: public String getStatus() {
240: return fdf.getString("Status");
241: }
242:
243: /**
244: * This will set the status string.
245: *
246: * @param status The new status string.
247: */
248: public void setStatus(String status) {
249: fdf.setString("Status", status);
250: }
251:
252: /**
253: * This will get the list of FDF Pages. This will return a list of FDFPage objects.
254: *
255: * @return A list of FDF pages.
256: */
257: public List getPages() {
258: List retval = null;
259: COSArray pageArray = (COSArray) fdf
260: .getDictionaryObject("Pages");
261: if (pageArray != null) {
262: List pages = new ArrayList();
263: for (int i = 0; i < pageArray.size(); i++) {
264: pages
265: .add(new FDFPage((COSDictionary) pageArray
266: .get(i)));
267: }
268: retval = new COSArrayList(pages, pageArray);
269: }
270: return retval;
271: }
272:
273: /**
274: * This will set the list of pages. This should be a list of FDFPage objects.
275: *
276: *
277: * @param pages The list of pages.
278: */
279: public void setPages(List pages) {
280: fdf.setItem("Pages", COSArrayList.converterToCOSArray(pages));
281: }
282:
283: /**
284: * The encoding to be used for a FDF field. The default is PDFDocEncoding
285: * and this method will never return null.
286: *
287: * @return The encoding value.
288: */
289: public String getEncoding() {
290: String encoding = fdf.getNameAsString("Encoding");
291: if (encoding == null) {
292: encoding = "PDFDocEncoding";
293: }
294: return encoding;
295:
296: }
297:
298: /**
299: * This will set the encoding.
300: *
301: * @param encoding The new encoding.
302: */
303: public void setEncoding(String encoding) {
304: fdf.setName("Encoding", encoding);
305: }
306:
307: /**
308: * This will get the list of FDF Annotations. This will return a list of FDFAnnotation objects
309: * or null if the entry is not set.
310: *
311: * @return A list of FDF annotations.
312: */
313: public List getAnnotations() {
314: List retval = null;
315: COSArray annotArray = (COSArray) fdf
316: .getDictionaryObject("Annots");
317: if (annotArray != null) {
318: List annots = new ArrayList();
319: for (int i = 0; i < annotArray.size(); i++) {
320: annots.add(new FDFAnnotation((COSDictionary) annotArray
321: .getObject(i)));
322: }
323: retval = new COSArrayList(annots, annotArray);
324: }
325: return retval;
326: }
327:
328: /**
329: * This will set the list of annotations. This should be a list of FDFAnnotation objects.
330: *
331: *
332: * @param annots The list of annotations.
333: */
334: public void setAnnotations(List annots) {
335: fdf.setItem("Annots", COSArrayList.converterToCOSArray(annots));
336: }
337:
338: /**
339: * This will get the incremental updates since the PDF was last opened.
340: *
341: * @return The differences entry of the FDF dictionary.
342: */
343: public COSStream getDifferences() {
344: return (COSStream) fdf.getDictionaryObject("Differences");
345: }
346:
347: /**
348: * This will set the differences stream.
349: *
350: * @param diff The new differences stream.
351: */
352: public void setDifferences(COSStream diff) {
353: fdf.setItem("Differences", diff);
354: }
355:
356: /**
357: * This will get the target frame in the browser to open this document.
358: *
359: * @return The target frame.
360: */
361: public String getTarget() {
362: return fdf.getString("Target");
363: }
364:
365: /**
366: * This will set the target frame in the browser to open this document.
367: *
368: * @param target The new target frame.
369: */
370: public void setTarget(String target) {
371: fdf.setString("Target", target);
372: }
373:
374: /**
375: * This will get the list of embedded FDF entries, or null if the entry is null.
376: * This will return a list of PDFileSpecification objects.
377: *
378: * @return A list of embedded FDF files.
379: *
380: * @throws IOException If there is an error creating the file spec.
381: */
382: public List getEmbeddedFDFs() throws IOException {
383: List retval = null;
384: COSArray embeddedArray = (COSArray) fdf
385: .getDictionaryObject("EmbeddedFDFs");
386: if (embeddedArray != null) {
387: List embedded = new ArrayList();
388: for (int i = 0; i < embeddedArray.size(); i++) {
389: embedded.add(PDFileSpecification.createFS(embeddedArray
390: .get(i)));
391: }
392: retval = new COSArrayList(embedded, embeddedArray);
393: }
394: return retval;
395: }
396:
397: /**
398: * This will set the list of embedded FDFs. This should be a list of
399: * PDFileSpecification objects.
400: *
401: *
402: * @param embedded The list of embedded FDFs.
403: */
404: public void setEmbeddedFDFs(List embedded) {
405: fdf.setItem("EmbeddedFDFs", COSArrayList
406: .converterToCOSArray(embedded));
407: }
408:
409: /**
410: * This will get the java script entry.
411: *
412: * @return The java script entry describing javascript commands.
413: */
414: public FDFJavaScript getJavaScript() {
415: FDFJavaScript fs = null;
416: COSDictionary dic = (COSDictionary) fdf
417: .getDictionaryObject("JavaScript");
418: if (dic != null) {
419: fs = new FDFJavaScript(dic);
420: }
421: return fs;
422: }
423:
424: /**
425: * This will set the JavaScript entry.
426: *
427: * @param js The javascript entries.
428: */
429: public void setJavaScript(FDFJavaScript js) {
430: fdf.setItem("JavaScript", js);
431: }
432:
433: }
|