1 57 58 package sax; 59 60 import org.xml.sax.AttributeList ; 61 import org.xml.sax.HandlerBase ; 62 import org.xml.sax.Parser ; 63 import org.xml.sax.SAXException ; 64 import org.xml.sax.SAXParseException ; 65 import org.xml.sax.XMLReader ; 66 import org.xml.sax.helpers.ParserFactory ; 67 68 import util.Arguments; 69 70 77 public class SAXCount 78 extends HandlerBase { 79 80 84 85 private static final String 86 DEFAULT_PARSER_NAME = "org.enhydra.apache.xerces.parsers.SAXParser"; 87 88 89 private static boolean setValidation = false; private static boolean setNameSpaces = true; 91 private static boolean setLoadExternalDTD = true; 92 private static boolean setSchemaSupport = true; 93 private static boolean setSchemaFullSupport = false; 94 95 96 100 private static boolean warmup = false; 101 102 103 private long elements; 104 105 106 private long attributes; 107 108 109 private long characters; 110 111 112 private long ignorableWhitespace; 113 114 118 119 public static void print(String parserName, String uri, boolean validate) { 120 121 try { 122 SAXCount counter = new SAXCount(); 123 124 Parser parser = ParserFactory.makeParser(parserName); 125 parser.setDocumentHandler(counter); 126 parser.setErrorHandler(counter); 127 try { 128 if ( parser instanceof XMLReader ){ 130 ((XMLReader )parser).setFeature( "http://xml.org/sax/features/validation", 131 validate); 132 ((XMLReader )parser).setFeature( "http://xml.org/sax/features/namespaces", 133 setNameSpaces ); 134 ((XMLReader )parser).setFeature( "http://apache.org/xml/features/validation/schema", 135 setSchemaSupport ); 136 ((XMLReader )parser).setFeature( "http://apache.org/xml/features/nonvalidating/load-external-dtd", 137 setLoadExternalDTD ); 138 ((XMLReader )parser).setFeature( "http://apache.org/xml/features/validation/schema-full-checking", 139 setSchemaFullSupport ); 140 141 } 142 } catch (Exception ex) { 143 } 144 145 if (warmup) { 146 if (parser instanceof XMLReader ) 147 ((XMLReader )parser).setFeature("http://apache.org/xml/features/continue-after-fatal-error", true); 148 149 parser.parse(uri); 150 warmup = false; 151 } 152 long before = System.currentTimeMillis(); 153 parser.parse(uri); 154 long after = System.currentTimeMillis(); 155 counter.printResults(uri, after - before); 156 } catch (org.xml.sax.SAXParseException spe) { 157 spe.printStackTrace(System.err); 158 } catch (org.xml.sax.SAXException se) { 159 if (se.getException() != null) 160 se.getException().printStackTrace(System.err); 161 else 162 se.printStackTrace(System.err); 163 } catch (Exception e) { 164 e.printStackTrace(System.err); 165 } 166 167 } 169 173 174 public void startDocument() { 175 176 if (warmup) 177 return; 178 179 elements = 0; 180 attributes = 0; 181 characters = 0; 182 ignorableWhitespace = 0; 183 184 } 186 187 public void startElement(String name, AttributeList attrs) { 188 189 if (warmup) 190 return; 191 192 elements++; 193 if (attrs != null) { 194 attributes += attrs.getLength(); 195 } 196 197 } 199 200 public void characters(char ch[], int start, int length) { 201 202 if (warmup) 203 return; 204 205 characters += length; 206 207 } 209 210 public void ignorableWhitespace(char ch[], int start, int length) { 211 212 if (warmup) 213 return; 214 215 ignorableWhitespace += length; 216 217 } 219 223 224 public void warning(SAXParseException ex) { 225 if (warmup) 226 return; 227 228 System.err.println("[Warning] "+ 229 getLocationString(ex)+": "+ 230 ex.getMessage()); 231 } 232 233 234 public void error(SAXParseException ex) { 235 if (warmup) 236 return; 237 238 System.err.println("[Error] "+ 239 getLocationString(ex)+": "+ 240 ex.getMessage()); 241 } 242 243 244 public void fatalError(SAXParseException ex) throws SAXException { 245 if (warmup) 246 return; 247 248 System.err.println("[Fatal Error] "+ 249 getLocationString(ex)+": "+ 250 ex.getMessage()); 251 } 253 254 255 private String getLocationString(SAXParseException ex) { 256 StringBuffer str = new StringBuffer (); 257 258 String systemId = ex.getSystemId(); 259 if (systemId != null) { 260 int index = systemId.lastIndexOf('/'); 261 if (index != -1) 262 systemId = systemId.substring(index + 1); 263 str.append(systemId); 264 } 265 str.append(':'); 266 str.append(ex.getLineNumber()); 267 str.append(':'); 268 str.append(ex.getColumnNumber()); 269 270 return str.toString(); 271 272 } 274 278 279 public void printResults(String uri, long time) { 280 281 System.out.print(uri); 283 System.out.print(": "); 284 System.out.print(time); 285 System.out.print(" ms ("); 286 System.out.print(elements); 287 System.out.print(" elems, "); 288 System.out.print(attributes); 289 System.out.print(" attrs, "); 290 System.out.print(ignorableWhitespace); 291 System.out.print(" spaces, "); 292 System.out.print(characters); 293 System.out.print(" chars)"); 294 System.out.println(); 295 } 297 301 302 public static void main(String argv[]) { 303 304 Arguments argopt = new Arguments(); 305 argopt.setUsage( new String [] 306 { "usage: java sax.SAXCount (options) uri ...","", 307 "options:", 308 " -p name Specify SAX parser by name.", 309 " -n | -N Turn on/off namespace [default=on]", 310 " -v | -V Turn on/off validation [default=off]", 311 " -s | -S Turn on/off Schema support [default=on]", 312 " -f | -F Turn on/off Schema full consraint checking [default=off]", 313 " -d | -D Turn on/off deferred DOM [default=on]", 314 " -l | -L Turn on/off external DTD loading [default=on]", 315 " -w Warmup the parser before timing.", 316 " -h This help screen."} ); 317 318 319 if (argv.length == 0) { 321 argopt.printUsage(); 322 System.exit(1); 323 } 324 325 String parserName = DEFAULT_PARSER_NAME; 327 328 argopt.parseArgumentTokens(argv, new char[] { 'p'} ); 329 330 int c; 331 String arg = null; 332 while ( ( arg = argopt.getlistFiles() ) != null ) { 333 outer: 334 while ( (c = argopt.getArguments()) != -1 ){ 335 switch (c) { 336 case 'v': 337 setValidation = true; 338 break; 339 case 'V': 340 setValidation = false; 341 break; 342 case 'N': 343 setNameSpaces = false; 344 break; 345 case 'n': 346 setNameSpaces = true; 347 break; 348 case 'L': 349 setLoadExternalDTD = false; 350 break; 351 case 'l': 352 setLoadExternalDTD = true; 353 break; 354 case 'p': 355 parserName = argopt.getStringParameter(); 356 break; 357 case 's': 358 setSchemaSupport = true; 359 break; 360 case 'S': 361 setSchemaSupport = false; 362 break; 363 case 'f': 364 setSchemaFullSupport = true; 365 break; 366 case 'F': 367 setSchemaFullSupport = false; 368 break; 369 case '?': 370 case 'h': 371 case '-': 372 argopt.printUsage(); 373 System.exit(1); 374 break; 375 case 'w': 376 warmup = true; 377 break; 378 case -1: 379 break outer; 380 default: 381 break; 382 } 383 384 } 385 386 print(parserName, arg, setValidation); 388 } 390 } 392 } | Popular Tags |