001: /*
002: *
003: * Copyright 2004 by Leonard Rosenthol.
004: *
005: * The contents of this file are subject to the Mozilla Public License Version 1.1
006: * (the "License"); you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS" basis,
010: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
011: * for the specific language governing rights and limitations under the License.
012: *
013: * The Original Code is 'iText, a free JAVA-PDF library'.
014: *
015: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
016: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
017: * All Rights Reserved.
018: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
019: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
020: *
021: * Contributor(s): all the names of the contributors are added in the source code
022: * where applicable.
023: *
024: * Alternatively, the contents of this file may be used under the terms of the
025: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
026: * provisions of LGPL are applicable instead of those above. If you wish to
027: * allow use of your version of this file only under the terms of the LGPL
028: * License and not to allow others to use your version of this file under
029: * the MPL, indicate your decision by deleting the provisions above and
030: * replace them with the notice and other provisions required by the LGPL.
031: * If you do not delete the provisions above, a recipient may use your version
032: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
033: *
034: * This library is free software; you can redistribute it and/or modify it
035: * under the terms of the MPL as stated above or under the terms of the GNU
036: * Library General Public License as published by the Free Software Foundation;
037: * either version 2 of the License, or any later version.
038: *
039: * This library is distributed in the hope that it will be useful, but WITHOUT
040: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
041: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
042: * details.
043: *
044: * If you didn't download this code from the following link, you should check if
045: * you aren't using an obsolete version:
046: * http://www.lowagie.com/iText/
047: */
048:
049: package com.lowagie.text.pdf;
050:
051: import java.io.ByteArrayInputStream;
052: import java.io.FileInputStream;
053: import java.io.IOException;
054: import java.util.HashMap;
055: import java.util.Stack;
056:
057: import com.lowagie.text.xml.simpleparser.SimpleXMLDocHandler;
058: import com.lowagie.text.xml.simpleparser.SimpleXMLParser;
059:
060: /**
061: * Reads a XFDF.
062: * @author Leonard Rosenthol (leonardr@pdfsages.com)
063: */
064: public class XfdfReader implements SimpleXMLDocHandler {
065: // stuff used during parsing to handle state
066: private boolean foundRoot = false;
067: private Stack fieldNames = new Stack();
068: private Stack fieldValues = new Stack();
069:
070: // storage for the field list and their values
071: HashMap fields;
072:
073: // storage for the path to referenced PDF, if any
074: String fileSpec;
075:
076: /** Reads an XFDF form.
077: * @param filename the file name of the form
078: * @throws IOException on error
079: */
080: public XfdfReader(String filename) throws IOException {
081: FileInputStream fin = null;
082: try {
083: fin = new FileInputStream(filename);
084: SimpleXMLParser.parse(this , fin);
085: } finally {
086: try {
087: if (fin != null) {
088: fin.close();
089: }
090: } catch (Exception e) {
091: }
092: }
093: }
094:
095: /** Reads an XFDF form.
096: * @param xfdfIn the byte array with the form
097: * @throws IOException on error
098: */
099: public XfdfReader(byte xfdfIn[]) throws IOException {
100: SimpleXMLParser.parse(this , new ByteArrayInputStream(xfdfIn));
101: }
102:
103: /** Gets all the fields. The map is keyed by the fully qualified
104: * field name and the value is a merged <CODE>PdfDictionary</CODE>
105: * with the field content.
106: * @return all the fields
107: */
108: public HashMap getFields() {
109: return fields;
110: }
111:
112: /** Gets the field value.
113: * @param name the fully qualified field name
114: * @return the field's value
115: */
116: public String getField(String name) {
117: return (String) fields.get(name);
118: }
119:
120: /** Gets the field value or <CODE>null</CODE> if the field does not
121: * exist or has no value defined.
122: * @param name the fully qualified field name
123: * @return the field value or <CODE>null</CODE>
124: */
125: public String getFieldValue(String name) {
126: String field = (String) fields.get(name);
127: if (field == null)
128: return null;
129: else
130: return field;
131: }
132:
133: /** Gets the PDF file specification contained in the FDF.
134: * @return the PDF file specification contained in the FDF
135: */
136: public String getFileSpec() {
137: return fileSpec;
138: }
139:
140: /**
141: * Called when a start tag is found.
142: * @param tag the tag name
143: * @param h the tag's attributes
144: */
145: public void startElement(String tag, HashMap h) {
146: if (!foundRoot) {
147: if (!tag.equals("xfdf"))
148: throw new RuntimeException(
149: "Root element is not Bookmark.");
150: else
151: foundRoot = true;
152: }
153:
154: if (tag.equals("xfdf")) {
155:
156: } else if (tag.equals("f")) {
157: fileSpec = (String) h.get("href");
158: } else if (tag.equals("fields")) {
159: fields = new HashMap(); // init it!
160: } else if (tag.equals("field")) {
161: String fName = (String) h.get("name");
162: fieldNames.push(fName);
163: } else if (tag.equals("value")) {
164: fieldValues.push("");
165: }
166: }
167:
168: /**
169: * Called when an end tag is found.
170: * @param tag the tag name
171: */
172: public void endElement(String tag) {
173: if (tag.equals("value")) {
174: String fName = "";
175: for (int k = 0; k < fieldNames.size(); ++k) {
176: fName += "." + (String) fieldNames.elementAt(k);
177: }
178: if (fName.startsWith("."))
179: fName = fName.substring(1);
180: String fVal = (String) fieldValues.pop();
181: fields.put(fName, fVal);
182: } else if (tag.equals("field")) {
183: if (!fieldNames.isEmpty())
184: fieldNames.pop();
185: }
186: }
187:
188: /**
189: * Called when the document starts to be parsed.
190: */
191: public void startDocument() {
192: fileSpec = "";
193: }
194:
195: /**
196: * Called after the document is parsed.
197: */
198: public void endDocument() {
199:
200: }
201:
202: /**
203: * Called when a text element is found.
204: * @param str the text element, probably a fragment.
205: */
206: public void text(String str) {
207: if (fieldNames.isEmpty() || fieldValues.isEmpty())
208: return;
209:
210: String val = (String) fieldValues.pop();
211: val += str;
212: fieldValues.push(val);
213: }
214: }
|