1 48 49 package com.lowagie.text.pdf; 50 51 import java.io.ByteArrayInputStream ; 52 import java.io.FileInputStream ; 53 import java.io.IOException ; 54 import java.util.HashMap ; 55 import java.util.Stack ; 56 57 import com.lowagie.text.xml.simpleparser.SimpleXMLDocHandler; 58 import com.lowagie.text.xml.simpleparser.SimpleXMLParser; 59 60 64 public class XfdfReader implements SimpleXMLDocHandler { 65 private boolean foundRoot = false; 67 private Stack fieldNames = new Stack (); 68 private Stack fieldValues = new Stack (); 69 70 HashMap fields; 72 73 String fileSpec; 75 76 80 public XfdfReader(String filename) throws IOException { 81 FileInputStream fin = null; 82 try { 83 fin = new FileInputStream (filename); 84 SimpleXMLParser.parse(this, fin); 85 } 86 finally { 87 try{if (fin != null) {fin.close();}}catch(Exception e){} 88 } 89 } 90 91 95 public XfdfReader(byte xfdfIn[]) throws IOException { 96 SimpleXMLParser.parse( this, new ByteArrayInputStream (xfdfIn)); 97 } 98 99 104 public HashMap getFields() { 105 return fields; 106 } 107 108 112 public String getField(String name) { 113 return (String )fields.get(name); 114 } 115 116 121 public String getFieldValue(String name) { 122 String field = (String )fields.get(name); 123 if (field == null) 124 return null; 125 else 126 return field; 127 } 128 129 132 public String getFileSpec() { 133 return fileSpec; 134 } 135 136 141 public void startElement(String tag, HashMap h) 142 { 143 if ( !foundRoot ) { 144 if (!tag.equals("xfdf")) 145 throw new RuntimeException ("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 )h.get( "href" ); 154 } else if ( tag.equals("fields") ) { 155 fields = new HashMap (); } else if ( tag.equals("field") ) { 157 String fName = (String ) h.get( "name" ); 158 fieldNames.push( fName ); 159 } else if ( tag.equals("value") ) { 160 fieldValues.push( "" ); 161 } 162 } 163 167 public void endElement(String tag) { 168 if ( tag.equals("value") ) { 169 String fName = ""; 170 for (int k = 0; k < fieldNames.size(); ++k) { 171 fName += "." + (String )fieldNames.elementAt(k); 172 } 173 if (fName.startsWith(".")) 174 fName = fName.substring(1); 175 String fVal = (String ) 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 187 public void startDocument() 188 { 189 fileSpec = ""; 190 } 191 194 public void endDocument() 195 { 196 197 } 198 202 public void text(String str) 203 { 204 if (fieldNames.isEmpty() || fieldValues.isEmpty()) 205 return; 206 207 String val = (String )fieldValues.pop(); 208 val += str; 209 fieldValues.push(val); 210 } 211 } | Popular Tags |