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