1 6 7 package hotsax.html.sax; 8 9 import java.io.*; 10 import java.net.URL ; 11 import java.net.MalformedURLException ; 12 import java.util.*; 13 import org.xml.sax.*; 14 import org.xml.sax.helpers.*; 15 import org.apache.xerces.utils.*; 16 import org.apache.xerces.readers.*; 17 18 23 public class SaxParser implements org.xml.sax.XMLReader { 24 25 private EntityResolver entityResolver; 26 private DTDHandler dtdHandler; 27 private ContentHandler contentHandler; 28 private ErrorHandler errorHandler; 29 30 private org.xml.sax.helpers.AttributesImpl attrList; 32 34 35 private HashMap properties; 36 37 38 private HashMap features; 39 40 41 private HtmlLexer lexer; 42 43 44 private Reader reader; 45 46 47 public SaxParser() { 48 properties = new HashMap(); 49 features = new HashMap(); 50 53 entityResolver = new DefaultHandler(); 54 dtdHandler = new DefaultHandler(); 55 contentHandler = new DefaultHandler(); 56 errorHandler = new DefaultHandler(); 57 58 attrList = new org.xml.sax.helpers.AttributesImpl (); 59 } 60 61 62 65 protected Reader createReader(InputSource source) 66 throws IOException, MalformedURLException 67 { 68 if (source.getCharacterStream() != null) { 70 return source.getCharacterStream(); 71 } 72 73 if (source.getEncoding() != null && source.getByteStream() != null) { 75 java.io.Reader reader = new InputStreamReader(source.getByteStream(), source.getEncoding()); 76 return reader; 77 } 78 79 InputStream is = source.getByteStream(); 81 if (is == null) { 82 83 URL url = new URL (source.getSystemId()); 85 is = url.openStream(); 86 } 87 88 return new InputStreamReader(is); 89 90 } 91 92 93 public org.xml.sax.ContentHandler getContentHandler() { 94 return contentHandler; 95 } 96 97 public Object getProperty(String p1) throws org.xml.sax.SAXNotRecognizedException , org.xml.sax.SAXNotSupportedException { 98 return properties.get(p1); 99 } 100 101 public void setFeature(String p1,boolean p2) throws org.xml.sax.SAXNotRecognizedException , org.xml.sax.SAXNotSupportedException { 102 Boolean bool = new Boolean (p2); 103 features.put(p1, bool); 104 } 105 106 public void setEntityResolver(org.xml.sax.EntityResolver p1) { 107 entityResolver = p1; 108 } 109 110 111 public void setContentHandler(org.xml.sax.ContentHandler p1) { 112 contentHandler = p1; 113 } 114 115 public void setDTDHandler(org.xml.sax.DTDHandler p1) { 116 dtdHandler = p1; 117 } 118 119 public org.xml.sax.ErrorHandler getErrorHandler() { 120 return errorHandler; 121 } 122 123 public org.xml.sax.EntityResolver getEntityResolver() { 124 return entityResolver; 125 } 126 127 public void setErrorHandler(org.xml.sax.ErrorHandler p1) { 128 errorHandler = p1; 129 } 130 131 public org.xml.sax.DTDHandler getDTDHandler() { 132 return dtdHandler; 133 } 134 135 public void setProperty(String p1, Object p2) throws org.xml.sax.SAXNotRecognizedException , org.xml.sax.SAXNotSupportedException { 136 properties.put(p1, p2); 137 } 138 139 public boolean getFeature(String p1) throws org.xml.sax.SAXNotRecognizedException , org.xml.sax.SAXNotSupportedException { 140 Boolean bool = (Boolean )features.get(p1); 141 142 return bool.booleanValue(); 143 } 144 145 146 150 151 public void parse(String p1) 152 throws IOException, org.xml.sax.SAXException 153 { 154 InputSource source = entityResolver.resolveEntity(p1,p1); 155 try { 156 source = new InputSource(p1); 157 158 source.setCharacterStream(createReader(source)); 159 160 parse(source); 161 } 162 catch (Exception ex) 163 { 164 System.err.println("caught exception parsing " + ex.getClass().getName() + " " + ex.getMessage()); 165 ex.printStackTrace(); 166 } 167 finally { 168 try { 171 Reader reader = source.getCharacterStream(); 172 if (reader != null) { 173 reader.close(); 174 } 175 else { 176 InputStream is = source.getByteStream(); 177 if (is != null) { 178 is.close(); 179 } 180 } 181 } 182 catch (IOException e) { 183 } 185 } 186 187 } 188 189 190 193 194 public void parse(org.xml.sax.InputSource p1) 195 throws java.io.IOException , org.xml.sax.SAXException 196 { 197 Boolean debugWrapper = (Boolean )properties.get("debug"); 198 boolean debug = (debugWrapper == null ? false : debugWrapper.booleanValue()); 199 HtmlParser yyparser; 200 201 Reader reader = p1.getCharacterStream(); 202 203 yyparser = new HtmlParser(reader); 204 205 ParserDelegate delegate = yyparser.getDelegate(); 206 delegate.setSaxParser(this); 207 208 yyparser.yyparse(); 210 } 211 212 public void startDocument() 213 throws SAXException 214 { 215 if (contentHandler != null) 216 contentHandler.startDocument(); 217 } 218 219 public void endDocument() 220 throws SAXException 221 { 222 if (contentHandler != null) 223 contentHandler.endDocument(); 224 } 225 226 227 228 231 public void startElement(String name) 232 throws SAXException 233 { 234 if (contentHandler != null) 235 contentHandler.startElement("", name, "", (Attributes)attrList); 236 } 237 238 public void endElement(String name) 239 throws SAXException 240 { 241 if (contentHandler != null) 242 contentHandler.endElement("", name, ""); 243 } 244 245 246 } 247 | Popular Tags |