1 3 package org.htmlparser.tests; 4 5 import java.io.IOException ; 6 7 import java.net.MalformedURLException ; 8 import java.net.URL ; 9 10 import org.xml.sax.Attributes ; 11 import org.xml.sax.ContentHandler ; 12 import org.xml.sax.ErrorHandler ; 13 import org.xml.sax.Locator ; 14 import org.xml.sax.SAXException ; 15 import org.xml.sax.SAXNotRecognizedException ; 16 import org.xml.sax.SAXNotSupportedException ; 17 import org.xml.sax.SAXParseException ; 18 import org.xml.sax.XMLReader ; 19 20 import org.xml.sax.helpers.XMLReaderFactory ; 21 22 23 26 public class SAXTest implements ContentHandler , ErrorHandler 27 { 28 29 33 34 37 public static void main (String args[]) 38 { 39 System.out.println("************************************" + 40 "************************************"); 41 System.out.println("* Testing SAX2"); 42 System.out.println("************************************" + 43 "************************************"); 44 System.out.print("\n"); 45 46 50 String driverName = "org.htmlparser.sax.XMLReader"; 54 System.out.println("SAX driver class: " + 55 driverName + 56 "\n (you can specify a different one using the " + 57 "org.xml.sax.driver property)"); 58 System.out.print("\n"); 59 60 61 65 System.out.println("Now, we'll try to create an instance of the " + 66 "driver, using XMLReaderFactory"); 67 XMLReader reader = null; 68 try { 69 reader = XMLReaderFactory.createXMLReader(driverName); 70 } catch (SAXException e) { 71 System.out.println("Failed to create XMLReader: " + 72 e.getMessage() + 73 "\nMake sure that the class actually " + 74 "exists and is present on your CLASSPATH" + 75 "\nor specify a different class using the " + 76 "org.xml.sax.driver property"); 77 System.exit(1); 78 } 79 System.out.println("XMLReader created successfully\n"); 80 81 82 System.out.println("Checking defaults for some well-known features:"); 86 checkFeature(reader, "http://xml.org/sax/features/namespaces"); 87 checkFeature(reader, "http://xml.org/sax/features/namespace-prefixes"); 88 checkFeature(reader, "http://xml.org/sax/features/string-interning"); 89 checkFeature(reader, "http://xml.org/sax/features/validation"); 90 checkFeature(reader, 91 "http://xml.org/sax/features/external-general-entities"); 92 checkFeature(reader, 93 "http://xml.org/sax/features/external-parameter-entities"); 94 System.out.print("\n"); 95 96 97 System.out.println("Creating and assigning handlers\n"); 101 SAXTest handler = new SAXTest(); 102 reader.setContentHandler(handler); 103 reader.setErrorHandler(handler); 104 105 if (args.length > 0) { 109 for (int i = 0; i < args.length; i++) { 110 String systemId = makeAbsoluteURL(args[i]); 111 System.out.println("Trying file " + systemId); 112 try { 113 reader.parse(systemId); 114 } catch (SAXException e1) { 115 System.out.println(systemId + 116 " failed with XML error: " + 117 e1.getMessage()); 118 } catch (IOException e2) { 119 System.out.println(systemId + 120 " failed with I/O error: " + 121 e2.getMessage()); 122 } 123 System.out.print("\n"); 124 } 125 } else { 126 System.out.println("No documents supplied on command line; " + 127 "parsing skipped."); 128 } 129 130 131 System.out.println("SAX2 test finished."); 135 } 136 137 138 141 private static void checkFeature (XMLReader reader, String name) 142 { 143 try { 144 System.out.println(" " + 145 name + 146 " = " + 147 reader.getFeature(name)); 148 } catch (SAXNotRecognizedException e) { 149 System.out.println("XMLReader does not recognize feature " + 150 name); 151 } catch (SAXNotSupportedException e) { 152 System.out.println("XMLReader recognizes feature " + 153 name + 154 " but does not support checking its value"); 155 } 156 } 157 158 159 170 private static String makeAbsoluteURL (String url) 171 { 172 URL baseURL; 173 174 String currentDirectory = System.getProperty("user.dir"); 175 String fileSep = System.getProperty("file.separator"); 176 String file = currentDirectory.replace(fileSep.charAt(0), '/') + '/'; 177 178 if (file.charAt(0) != '/') { 179 file = "/" + file; 180 } 181 182 try { 183 baseURL = new URL ("file", null, file); 184 return new URL (baseURL, url).toString(); 185 } catch (MalformedURLException e) { 186 System.err.println(url + ": " + e.getMessage()); 187 return url; 188 } 189 } 190 191 private static String makeNSName (String uri, String localName, 192 String qName) 193 { 194 if (uri.equals("")) 195 uri = "[none]"; 196 if (localName.equals("")) 197 localName = "[none]"; 198 if (qName.equals("")) 199 qName = "[none]"; 200 return uri + '/' + localName + '/' + qName; 201 } 202 203 private static String escapeData (char ch[], int start, int length) 204 { 205 StringBuffer buf = new StringBuffer (); 206 for (int i = start; i < start + length; i++) { 207 switch(ch[i]) { 208 case '\n': 209 buf.append("\\n"); 210 break; 211 case '\t': 212 buf.append("\\t"); 213 break; 214 case '\r': 215 buf.append("\\r"); 216 break; 217 default: 218 buf.append(ch[i]); 219 break; 220 } 221 } 222 return buf.toString(); 223 } 224 225 226 230 public void setDocumentLocator (Locator locator) 231 { 232 System.out.println(" EVENT: setDocumentLocator"); 233 } 234 235 public void startDocument () 236 throws SAXException 237 { 238 System.out.println(" EVENT: startDocument"); 239 } 240 241 public void endDocument () 242 throws SAXException 243 { 244 System.out.println(" EVENT: endDocument"); 245 } 246 247 public void startPrefixMapping (String prefix, String uri) 248 throws SAXException 249 { 250 System.out.println(" EVENT: startPrefixMapping " + 251 prefix + " = " + uri); 252 } 253 254 public void endPrefixMapping (String prefix) 255 throws SAXException 256 { 257 System.out.println(" EVENT: endPrefixMapping " + prefix); 258 } 259 260 public void startElement (String namespaceURI, String localName, 261 String qName, Attributes atts) 262 throws SAXException 263 { 264 System.out.println(" EVENT: startElement " + 265 makeNSName(namespaceURI, localName, qName)); 266 int attLen = atts.getLength(); 267 for (int i = 0; i < attLen; i++) { 268 char ch[] = atts.getValue(i).toCharArray(); 269 System.out.println(" Attribute " + 270 makeNSName(atts.getURI(i), 271 atts.getLocalName(i), 272 atts.getQName(i)) + 273 '=' + 274 escapeData(ch, 0, ch.length)); 275 } 276 } 277 278 public void endElement (String namespaceURI, String localName, 279 String qName) 280 throws SAXException 281 { 282 System.out.println(" EVENT: endElement " + 283 makeNSName(namespaceURI, localName, qName)); 284 } 285 286 public void characters (char ch[], int start, int length) 287 throws SAXException 288 { 289 System.out.println(" EVENT: characters " + 290 escapeData(ch, start, length)); 291 } 292 293 public void ignorableWhitespace (char ch[], int start, int length) 294 throws SAXException 295 { 296 System.out.println(" EVENT: ignorableWhitespace " + 297 escapeData(ch, start, length)); 298 } 299 300 public void processingInstruction (String target, String data) 301 throws SAXException 302 { 303 System.out.println(" EVENT: processingInstruction " + 304 target + ' ' + data); 305 } 306 307 public void skippedEntity (String name) 308 throws SAXException 309 { 310 System.out.println(" EVENT: skippedEntity " + name); 311 } 312 313 314 318 public void warning (SAXParseException e) 319 throws SAXException 320 { 321 System.out.println(" EVENT: warning " + 322 e.getMessage() + ' ' + 323 e.getSystemId() + ' ' + 324 e.getLineNumber() + ' ' + 325 e.getColumnNumber()); 326 } 327 328 public void error (SAXParseException e) 329 throws SAXException 330 { 331 System.out.println(" EVENT: error " + 332 e.getMessage() + ' ' + 333 e.getSystemId() + ' ' + 334 e.getLineNumber() + ' ' + 335 e.getColumnNumber()); 336 } 337 338 public void fatalError (SAXParseException e) 339 throws SAXException 340 { 341 System.out.println(" EVENT: fatal error " + 342 e.getMessage() + ' ' + 343 e.getSystemId() + ' ' + 344 e.getLineNumber() + ' ' + 345 e.getColumnNumber()); 346 } 347 348 } 349 350 | Popular Tags |