KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > XfdfReader


1 /*
2  *
3  * Copyright 2004 by Leonard Rosenthol.
4  *
5  * The contents of this file are subject to the Mozilla Public License Version 1.1
6  * (the "License"); you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the License.
12  *
13  * The Original Code is 'iText, a free JAVA-PDF library'.
14  *
15  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
16  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
17  * All Rights Reserved.
18  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
19  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
20  *
21  * Contributor(s): all the names of the contributors are added in the source code
22  * where applicable.
23  *
24  * Alternatively, the contents of this file may be used under the terms of the
25  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
26  * provisions of LGPL are applicable instead of those above. If you wish to
27  * allow use of your version of this file only under the terms of the LGPL
28  * License and not to allow others to use your version of this file under
29  * the MPL, indicate your decision by deleting the provisions above and
30  * replace them with the notice and other provisions required by the LGPL.
31  * If you do not delete the provisions above, a recipient may use your version
32  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
33  *
34  * This library is free software; you can redistribute it and/or modify it
35  * under the terms of the MPL as stated above or under the terms of the GNU
36  * Library General Public License as published by the Free Software Foundation;
37  * either version 2 of the License, or any later version.
38  *
39  * This library is distributed in the hope that it will be useful, but WITHOUT
40  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
41  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
42  * details.
43  *
44  * If you didn't download this code from the following link, you should check if
45  * you aren't using an obsolete version:
46  * http://www.lowagie.com/iText/
47  */

48
49 package com.lowagie.text.pdf;
50
51 import java.io.ByteArrayInputStream JavaDoc;
52 import java.io.FileInputStream JavaDoc;
53 import java.io.IOException JavaDoc;
54 import java.util.HashMap JavaDoc;
55 import java.util.Stack JavaDoc;
56
57 import com.lowagie.text.xml.simpleparser.SimpleXMLDocHandler;
58 import com.lowagie.text.xml.simpleparser.SimpleXMLParser;
59
60 /**
61  * Reads a XFDF.
62  * @author Leonard Rosenthol (leonardr@pdfsages.com)
63  */

64 public class XfdfReader implements SimpleXMLDocHandler {
65     // stuff used during parsing to handle state
66
private boolean foundRoot = false;
67     private Stack JavaDoc fieldNames = new Stack JavaDoc();
68     private Stack JavaDoc fieldValues = new Stack JavaDoc();
69
70     // storage for the field list and their values
71
HashMap JavaDoc fields;
72     
73     // storage for the path to referenced PDF, if any
74
String JavaDoc fileSpec;
75     
76    /** Reads an XFDF form.
77      * @param filename the file name of the form
78      * @throws IOException on error
79      */

80     public XfdfReader(String JavaDoc filename) throws IOException JavaDoc {
81         FileInputStream JavaDoc fin = null;
82         try {
83             fin = new FileInputStream JavaDoc(filename);
84             SimpleXMLParser.parse(this, fin);
85         }
86         finally {
87             try{if (fin != null) {fin.close();}}catch(Exception JavaDoc e){}
88         }
89     }
90     
91     /** Reads an XFDF form.
92      * @param xfdfIn the byte array with the form
93      * @throws IOException on error
94      */

95     public XfdfReader(byte xfdfIn[]) throws IOException JavaDoc {
96         SimpleXMLParser.parse( this, new ByteArrayInputStream JavaDoc(xfdfIn));
97    }
98     
99     /** Gets all the fields. The map is keyed by the fully qualified
100      * field name and the value is a merged <CODE>PdfDictionary</CODE>
101      * with the field content.
102      * @return all the fields
103      */

104     public HashMap JavaDoc getFields() {
105         return fields;
106     }
107     
108     /** Gets the field value.
109      * @param name the fully qualified field name
110      * @return the field's value
111      */

112     public String JavaDoc getField(String JavaDoc name) {
113         return (String JavaDoc)fields.get(name);
114     }
115     
116     /** Gets the field value or <CODE>null</CODE> if the field does not
117      * exist or has no value defined.
118      * @param name the fully qualified field name
119      * @return the field value or <CODE>null</CODE>
120      */

121     public String JavaDoc getFieldValue(String JavaDoc name) {
122         String JavaDoc field = (String JavaDoc)fields.get(name);
123         if (field == null)
124             return null;
125         else
126             return field;
127     }
128     
129     /** Gets the PDF file specification contained in the FDF.
130      * @return the PDF file specification contained in the FDF
131      */

132     public String JavaDoc getFileSpec() {
133         return fileSpec;
134     }
135
136     /**
137      * Called when a start tag is found.
138      * @param tag the tag name
139      * @param h the tag's attributes
140      */

141     public void startElement(String JavaDoc tag, HashMap JavaDoc h)
142     {
143         if ( !foundRoot ) {
144             if (!tag.equals("xfdf"))
145                 throw new RuntimeException JavaDoc("Root element is not Bookmark.");
146             else
147                 foundRoot = true;
148         }
149
150         if ( tag.equals("xfdf") ){
151             
152         } else if ( tag.equals("f") ) {
153             fileSpec = (String JavaDoc)h.get( "href" );
154         } else if ( tag.equals("fields") ) {
155             fields = new HashMap JavaDoc(); // init it!
156
} else if ( tag.equals("field") ) {
157             String JavaDoc fName = (String JavaDoc) h.get( "name" );
158             fieldNames.push( fName );
159         } else if ( tag.equals("value") ) {
160             fieldValues.push( "" );
161         }
162     }
163     /**
164      * Called when an end tag is found.
165      * @param tag the tag name
166      */

167     public void endElement(String JavaDoc tag) {
168         if ( tag.equals("value") ) {
169             String JavaDoc fName = "";
170             for (int k = 0; k < fieldNames.size(); ++k) {
171                 fName += "." + (String JavaDoc)fieldNames.elementAt(k);
172             }
173             if (fName.startsWith("."))
174                 fName = fName.substring(1);
175             String JavaDoc fVal = (String JavaDoc) fieldValues.pop();
176             fields.put( fName, fVal );
177         }
178         else if (tag.equals("field") ) {
179             if (!fieldNames.isEmpty())
180                 fieldNames.pop();
181         }
182     }
183     
184     /**
185      * Called when the document starts to be parsed.
186      */

187     public void startDocument()
188     {
189         fileSpec = "";
190     }
191     /**
192      * Called after the document is parsed.
193      */

194     public void endDocument()
195     {
196         
197     }
198     /**
199      * Called when a text element is found.
200      * @param str the text element, probably a fragment.
201      */

202     public void text(String JavaDoc str)
203     {
204         if (fieldNames.isEmpty() || fieldValues.isEmpty())
205             return;
206         
207         String JavaDoc val = (String JavaDoc)fieldValues.pop();
208         val += str;
209         fieldValues.push(val);
210     }
211 }
Popular Tags