1 16 17 package jaxp; 18 19 import java.io.PrintWriter ; 20 import java.util.Vector ; 21 22 import javax.xml.XMLConstants ; 23 import javax.xml.parsers.DocumentBuilder ; 24 import javax.xml.parsers.DocumentBuilderFactory ; 25 import javax.xml.transform.Source ; 26 import javax.xml.transform.dom.DOMSource ; 27 import javax.xml.transform.sax.SAXSource ; 28 import javax.xml.transform.stream.StreamSource ; 29 import javax.xml.validation.Schema ; 30 import javax.xml.validation.SchemaFactory ; 31 import javax.xml.validation.Validator ; 32 33 import org.w3c.dom.Document ; 34 import org.xml.sax.ErrorHandler ; 35 import org.xml.sax.InputSource ; 36 import org.xml.sax.SAXException ; 37 import org.xml.sax.SAXNotRecognizedException ; 38 import org.xml.sax.SAXNotSupportedException ; 39 import org.xml.sax.SAXParseException ; 40 import org.xml.sax.XMLReader ; 41 import org.xml.sax.helpers.XMLReaderFactory ; 42 43 66 public class SourceValidator 67 implements ErrorHandler { 68 69 73 75 76 protected static final String SCHEMA_FULL_CHECKING_FEATURE_ID = "http://apache.org/xml/features/validation/schema-full-checking"; 77 78 79 protected static final String HONOUR_ALL_SCHEMA_LOCATIONS_ID = "http://apache.org/xml/features/honour-all-schemaLocations"; 80 81 82 protected static final String VALIDATE_ANNOTATIONS_ID = "http://apache.org/xml/features/validate-annotations"; 83 84 85 protected static final String GENERATE_SYNTHETIC_ANNOTATIONS_ID = "http://apache.org/xml/features/generate-synthetic-annotations"; 86 87 89 90 protected static final int DEFAULT_REPETITION = 1; 91 92 93 protected static final String DEFAULT_VALIDATION_SOURCE = "sax"; 94 95 96 protected static final boolean DEFAULT_SCHEMA_FULL_CHECKING = false; 97 98 99 protected static final boolean DEFAULT_HONOUR_ALL_SCHEMA_LOCATIONS = false; 100 101 102 protected static final boolean DEFAULT_VALIDATE_ANNOTATIONS = false; 103 104 105 protected static final boolean DEFAULT_GENERATE_SYNTHETIC_ANNOTATIONS = false; 106 107 108 protected static final boolean DEFAULT_MEMORY_USAGE = false; 109 110 114 protected PrintWriter fOut = new PrintWriter (System.out); 115 116 120 121 public SourceValidator() { 122 } 124 128 public void validate(Validator validator, 129 Source source, String systemId, 130 int repetitions, boolean memoryUsage) { 131 try { 132 long timeBefore = System.currentTimeMillis(); 133 long memoryBefore = Runtime.getRuntime().freeMemory(); 134 for (int j = 0; j < repetitions; ++j) { 135 validator.validate(source); 136 } 137 long memoryAfter = Runtime.getRuntime().freeMemory(); 138 long timeAfter = System.currentTimeMillis(); 139 140 long time = timeAfter - timeBefore; 141 long memory = memoryUsage 142 ? memoryBefore - memoryAfter : Long.MIN_VALUE; 143 printResults(fOut, systemId, time, memory, repetitions); 144 } 145 catch (SAXParseException e) { 146 } 148 catch (Exception e) { 149 System.err.println("error: Parse error occurred - "+e.getMessage()); 150 Exception se = e; 151 if (e instanceof SAXException ) { 152 se = ((SAXException )e).getException(); 153 } 154 if (se != null) 155 se.printStackTrace(System.err); 156 else 157 e.printStackTrace(System.err); 158 159 } 160 } 162 163 public void printResults(PrintWriter out, String uri, long time, 164 long memory, int repetition) { 165 166 out.print(uri); 168 out.print(": "); 169 if (repetition == 1) { 170 out.print(time); 171 } 172 else { 173 out.print(time); 174 out.print('/'); 175 out.print(repetition); 176 out.print('='); 177 out.print(((float)time)/repetition); 178 } 179 out.print(" ms"); 180 if (memory != Long.MIN_VALUE) { 181 out.print(", "); 182 out.print(memory); 183 out.print(" bytes"); 184 } 185 out.println(); 186 out.flush(); 187 188 } 190 194 195 public void warning(SAXParseException ex) throws SAXException { 196 printError("Warning", ex); 197 } 199 200 public void error(SAXParseException ex) throws SAXException { 201 printError("Error", ex); 202 } 204 205 public void fatalError(SAXParseException ex) throws SAXException { 206 printError("Fatal Error", ex); 207 throw ex; 208 } 210 214 215 protected void printError(String type, SAXParseException ex) { 216 217 System.err.print("["); 218 System.err.print(type); 219 System.err.print("] "); 220 String systemId = ex.getSystemId(); 221 if (systemId != null) { 222 int index = systemId.lastIndexOf('/'); 223 if (index != -1) 224 systemId = systemId.substring(index + 1); 225 System.err.print(systemId); 226 } 227 System.err.print(':'); 228 System.err.print(ex.getLineNumber()); 229 System.err.print(':'); 230 System.err.print(ex.getColumnNumber()); 231 System.err.print(": "); 232 System.err.print(ex.getMessage()); 233 System.err.println(); 234 System.err.flush(); 235 236 } 238 242 243 public static void main (String [] argv) { 244 245 if (argv.length == 0) { 247 printUsage(); 248 System.exit(1); 249 } 250 251 Vector schemas = null; 253 Vector instances = null; 254 int repetition = DEFAULT_REPETITION; 255 String validationSource = DEFAULT_VALIDATION_SOURCE; 256 boolean schemaFullChecking = DEFAULT_SCHEMA_FULL_CHECKING; 257 boolean honourAllSchemaLocations = DEFAULT_HONOUR_ALL_SCHEMA_LOCATIONS; 258 boolean validateAnnotations = DEFAULT_VALIDATE_ANNOTATIONS; 259 boolean generateSyntheticAnnotations = DEFAULT_GENERATE_SYNTHETIC_ANNOTATIONS; 260 boolean memoryUsage = DEFAULT_MEMORY_USAGE; 261 262 for (int i = 0; i < argv.length; ++i) { 264 String arg = argv[i]; 265 if (arg.startsWith("-")) { 266 String option = arg.substring(1); 267 if (option.equals("x")) { 268 if (++i == argv.length) { 269 System.err.println("error: Missing argument to -x option."); 270 continue; 271 } 272 String number = argv[i]; 273 try { 274 int value = Integer.parseInt(number); 275 if (value < 1) { 276 System.err.println("error: Repetition must be at least 1."); 277 continue; 278 } 279 repetition = value; 280 } 281 catch (NumberFormatException e) { 282 System.err.println("error: invalid number ("+number+")."); 283 } 284 continue; 285 } 286 if (arg.equals("-a")) { 287 if (schemas == null) { 289 schemas = new Vector (); 290 } 291 while (i + 1 < argv.length && !(arg = argv[i + 1]).startsWith("-")) { 292 schemas.add(arg); 293 ++i; 294 } 295 continue; 296 } 297 if (arg.equals("-i")) { 298 if (instances == null) { 300 instances = new Vector (); 301 } 302 while (i + 1 < argv.length && !(arg = argv[i + 1]).startsWith("-")) { 303 instances.add(arg); 304 ++i; 305 } 306 continue; 307 } 308 if (arg.equals("-vs")) { 309 if (i + 1 < argv.length && !(arg = argv[i + 1]).startsWith("-")) { 310 if (arg.equals("sax") || arg.equals("dom") || arg.equals("stream")) { 311 validationSource = arg; 312 } 313 else { 314 System.err.println("error: unknown source type ("+arg+")."); 315 } 316 } 317 continue; 318 } 319 if (option.equalsIgnoreCase("f")) { 320 schemaFullChecking = option.equals("f"); 321 continue; 322 } 323 if (option.equalsIgnoreCase("hs")) { 324 honourAllSchemaLocations = option.equals("hs"); 325 continue; 326 } 327 if (option.equalsIgnoreCase("va")) { 328 validateAnnotations = option.equals("va"); 329 continue; 330 } 331 if (option.equalsIgnoreCase("ga")) { 332 generateSyntheticAnnotations = option.equals("ga"); 333 continue; 334 } 335 if (option.equalsIgnoreCase("m")) { 336 memoryUsage = option.equals("m"); 337 continue; 338 } 339 if (option.equals("h")) { 340 printUsage(); 341 continue; 342 } 343 System.err.println("error: unknown option ("+option+")."); 344 continue; 345 } 346 } 347 348 try { 349 SourceValidator sourceValidator = new SourceValidator(); 351 352 SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 354 factory.setErrorHandler(sourceValidator); 355 356 try { 357 factory.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, schemaFullChecking); 358 } 359 catch (SAXNotRecognizedException e) { 360 System.err.println("warning: SchemaFactory does not recognize feature ("+SCHEMA_FULL_CHECKING_FEATURE_ID+")"); 361 } 362 catch (SAXNotSupportedException e) { 363 System.err.println("warning: SchemaFactory does not support feature ("+SCHEMA_FULL_CHECKING_FEATURE_ID+")"); 364 } 365 try { 366 factory.setFeature(HONOUR_ALL_SCHEMA_LOCATIONS_ID, honourAllSchemaLocations); 367 } 368 catch (SAXNotRecognizedException e) { 369 System.err.println("warning: SchemaFactory does not recognize feature ("+HONOUR_ALL_SCHEMA_LOCATIONS_ID+")"); 370 } 371 catch (SAXNotSupportedException e) { 372 System.err.println("warning: SchemaFactory does not support feature ("+HONOUR_ALL_SCHEMA_LOCATIONS_ID+")"); 373 } 374 try { 375 factory.setFeature(VALIDATE_ANNOTATIONS_ID, validateAnnotations); 376 } 377 catch (SAXNotRecognizedException e) { 378 System.err.println("warning: SchemaFactory does not recognize feature ("+VALIDATE_ANNOTATIONS_ID+")"); 379 } 380 catch (SAXNotSupportedException e) { 381 System.err.println("warning: SchemaFactory does not support feature ("+VALIDATE_ANNOTATIONS_ID+")"); 382 } 383 try { 384 factory.setFeature(GENERATE_SYNTHETIC_ANNOTATIONS_ID, generateSyntheticAnnotations); 385 } 386 catch (SAXNotRecognizedException e) { 387 System.err.println("warning: SchemaFactory does not recognize feature ("+GENERATE_SYNTHETIC_ANNOTATIONS_ID+")"); 388 } 389 catch (SAXNotSupportedException e) { 390 System.err.println("warning: SchemaFactory does not support feature ("+GENERATE_SYNTHETIC_ANNOTATIONS_ID+")"); 391 } 392 393 Schema schema; 395 if (schemas != null && schemas.size() > 0) { 396 final int length = schemas.size(); 397 StreamSource [] sources = new StreamSource [length]; 398 for (int j = 0; j < length; ++j) { 399 sources[j] = new StreamSource ((String ) schemas.elementAt(j)); 400 } 401 schema = factory.newSchema(sources); 402 } 403 else { 404 schema = factory.newSchema(); 405 } 406 407 Validator validator = schema.newValidator(); 409 validator.setErrorHandler(sourceValidator); 410 411 try { 412 validator.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, schemaFullChecking); 413 } 414 catch (SAXNotRecognizedException e) { 415 System.err.println("warning: Validator does not recognize feature ("+SCHEMA_FULL_CHECKING_FEATURE_ID+")"); 416 } 417 catch (SAXNotSupportedException e) { 418 System.err.println("warning: Validator does not support feature ("+SCHEMA_FULL_CHECKING_FEATURE_ID+")"); 419 } 420 try { 421 validator.setFeature(HONOUR_ALL_SCHEMA_LOCATIONS_ID, honourAllSchemaLocations); 422 } 423 catch (SAXNotRecognizedException e) { 424 System.err.println("warning: Validator does not recognize feature ("+HONOUR_ALL_SCHEMA_LOCATIONS_ID+")"); 425 } 426 catch (SAXNotSupportedException e) { 427 System.err.println("warning: Validator does not support feature ("+HONOUR_ALL_SCHEMA_LOCATIONS_ID+")"); 428 } 429 try { 430 validator.setFeature(VALIDATE_ANNOTATIONS_ID, validateAnnotations); 431 } 432 catch (SAXNotRecognizedException e) { 433 System.err.println("warning: Validator does not recognize feature ("+VALIDATE_ANNOTATIONS_ID+")"); 434 } 435 catch (SAXNotSupportedException e) { 436 System.err.println("warning: Validator does not support feature ("+VALIDATE_ANNOTATIONS_ID+")"); 437 } 438 try { 439 validator.setFeature(GENERATE_SYNTHETIC_ANNOTATIONS_ID, generateSyntheticAnnotations); 440 } 441 catch (SAXNotRecognizedException e) { 442 System.err.println("warning: Validator does not recognize feature ("+GENERATE_SYNTHETIC_ANNOTATIONS_ID+")"); 443 } 444 catch (SAXNotSupportedException e) { 445 System.err.println("warning: Validator does not support feature ("+GENERATE_SYNTHETIC_ANNOTATIONS_ID+")"); 446 } 447 448 if (instances != null && instances.size() > 0) { 450 final int length = instances.size(); 451 if (validationSource.equals("sax")) { 452 XMLReader reader = XMLReaderFactory.createXMLReader(); 454 for (int j = 0; j < length; ++j) { 455 String systemId = (String ) instances.elementAt(j); 456 SAXSource source = new SAXSource (reader, new InputSource (systemId)); 457 sourceValidator.validate(validator, source, systemId, repetition, memoryUsage); 458 } 459 } 460 else if (validationSource.equals("dom")) { 461 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 463 dbf.setNamespaceAware(true); 464 DocumentBuilder db = dbf.newDocumentBuilder(); 465 for (int j = 0; j < length; ++j) { 466 String systemId = (String ) instances.elementAt(j); 467 Document doc = db.parse(systemId); 468 DOMSource source = new DOMSource (doc); 469 source.setSystemId(systemId); 470 sourceValidator.validate(validator, source, systemId, repetition, memoryUsage); 471 } 472 } 473 else { 474 for (int j = 0; j < length; ++j) { 476 String systemId = (String ) instances.elementAt(j); 477 StreamSource source = new StreamSource (systemId); 478 sourceValidator.validate(validator, source, systemId, repetition, memoryUsage); 479 } 480 } 481 } 482 } 483 catch (SAXParseException e) { 484 } 486 catch (Exception e) { 487 System.err.println("error: Parse error occurred - "+e.getMessage()); 488 if (e instanceof SAXException ) { 489 Exception nested = ((SAXException )e).getException(); 490 if (nested != null) { 491 e = nested; 492 } 493 } 494 e.printStackTrace(System.err); 495 } 496 } 498 502 503 private static void printUsage() { 504 505 System.err.println("usage: java jaxp.SourceValidator (options) ..."); 506 System.err.println(); 507 508 System.err.println("options:"); 509 System.err.println(" -x number Select number of repetitions."); 510 System.err.println(" -a uri ... Provide a list of schema documents"); 511 System.err.println(" -i uri ... Provide a list of instance documents to validate"); 512 System.err.println(" -vs source Select validation source (sax|dom|stream)"); 513 System.err.println(" -f | -F Turn on/off Schema full checking."); 514 System.err.println(" NOTE: Not supported by all schema factories and validators."); 515 System.err.println(" -hs | -HS Turn on/off honouring of all schema locations."); 516 System.err.println(" NOTE: Not supported by all schema factories and validators."); 517 System.err.println(" -va | -VA Turn on/off validation of schema annotations."); 518 System.err.println(" NOTE: Not supported by all schema factories and validators."); 519 System.err.println(" -ga | -GA Turn on/off generation of synthetic schema annotations."); 520 System.err.println(" NOTE: Not supported by all schema factories and validators."); 521 System.err.println(" -m | -M Turn on/off memory usage report"); 522 System.err.println(" -h This help screen."); 523 524 System.err.println(); 525 System.err.println("defaults:"); 526 System.err.println(" Repetition: " + DEFAULT_REPETITION); 527 System.err.println(" Validation source: " + DEFAULT_VALIDATION_SOURCE); 528 System.err.print(" Schema full checking: "); 529 System.err.println(DEFAULT_SCHEMA_FULL_CHECKING ? "on" : "off"); 530 System.err.print(" Honour all schema locations: "); 531 System.err.println(DEFAULT_HONOUR_ALL_SCHEMA_LOCATIONS ? "on" : "off"); 532 System.err.print(" Validate annotations: "); 533 System.err.println(DEFAULT_VALIDATE_ANNOTATIONS ? "on" : "off"); 534 System.err.print(" Generate synthetic annotations: "); 535 System.err.println(DEFAULT_GENERATE_SYNTHETIC_ANNOTATIONS ? "on" : "off"); 536 System.err.print(" Memory: "); 537 System.err.println(DEFAULT_MEMORY_USAGE ? "on" : "off"); 538 539 System.err.println(); 540 System.err.println("notes:"); 541 System.err.println(" The speed and memory results from this program should NOT be used as the"); 542 System.err.println(" basis of parser performance comparison! Real analytical methods should be"); 543 System.err.println(" used. For better results, perform multiple document validations within the"); 544 System.err.println(" same virtual machine to remove class loading from parse time and memory usage."); 545 546 } 548 } | Popular Tags |