1 package com.quadcap.text.sax; 2 3 40 41 import java.io.BufferedReader ; 42 import java.io.IOException ; 43 import java.io.Reader ; 44 45 import java.util.Hashtable ; 46 47 import org.xml.sax.AttributeList ; 48 import org.xml.sax.DocumentHandler ; 49 import org.xml.sax.ErrorHandler ; 50 import org.xml.sax.InputSource ; 51 import org.xml.sax.Parser ; 52 import org.xml.sax.Locator ; 53 import org.xml.sax.SAXException ; 54 import org.xml.sax.SAXParseException ; 55 56 import org.xml.sax.helpers.ParserFactory ; 57 58 import com.quadcap.util.Debug; 59 import com.quadcap.util.ConfigString; 60 61 66 public class Handler implements DocumentHandler , ErrorHandler { 67 protected Object context = null; 68 protected Hashtable env = new Hashtable (); 69 protected StringBuffer data = new StringBuffer (""); 70 71 Parser parser; 72 Locator locator; 73 74 77 public Handler() throws SAXException { 78 try { 79 ConfigString ps = ConfigString.find("xml.sax.parser", 80 "com.quadcap.text.sax.Parser"); 81 ClassLoader cl = ClassLoader.getSystemClassLoader(); 82 this.parser = 83 (Parser)(Class.forName(ps.toString(), true, cl).newInstance()); 84 parser.setDocumentHandler(this); 85 parser.setErrorHandler(this); 86 } catch (Exception e) { 87 Debug.print(e); 88 throw new SAXException (e); 89 } 90 } 91 92 99 public void parse(Reader r, Object context) 100 throws SAXException 101 { 102 this.context = context; 103 try { 104 parser.parse(new InputSource (new BufferedReader (r))); 105 } catch (SAXException e) { 106 throw e; 107 } catch (Exception e) { 108 Debug.print(e); 109 throw new SAXException (e); 110 } 111 } 112 113 120 public void error(SAXParseException exception) { 121 System.err.println("error"); 122 exception.printStackTrace(System.err); 123 } 124 125 132 public void fatalError(SAXParseException exception) { 133 System.err.println("fatal error"); 134 exception.printStackTrace(System.err); 135 } 136 137 144 public void warning(SAXParseException exception) { 145 System.err.println("warning"); 146 exception.printStackTrace(System.err); 147 } 148 149 162 public void characters(char[] ch, int start, int length) 163 throws SAXException 164 { 165 data.append(ch, start, length); 166 } 167 168 172 public void endDocument() { 173 } 174 175 181 public void endElement(String name) throws SAXException { 182 } 183 184 185 protected final String consumeAny(String name) throws SAXException { 186 String ret = (String )env.get(name); 187 if (ret != null) { 188 env.remove(name); 189 } 190 return ret; 191 } 192 193 protected final String consume(String name) throws SAXException { 194 String ret = consumeAny(name); 195 if (ret == null) { 196 throw new SAXException ("No value for <" + name + ">"); 197 } 198 return ret; 199 } 200 201 protected final String consumeData() { 202 String ret = data.toString().trim(); 203 data.setLength(0); 204 return ret; 205 } 206 207 214 public void ignorableWhitespace(char[] ch, int start, int length) { 215 } 216 217 224 public void processingInstruction(String target, String data) { 225 } 226 227 232 public void setDocumentLocator(Locator locator) { 233 this.locator = locator; 234 } 235 236 240 public void startDocument() { 241 } 242 243 251 public void startElement(String name, AttributeList attrs) 252 throws SAXException 253 { 254 data.setLength(0); 255 } 256 257 258 259 } 260 | Popular Tags |