2
* Copyright 2005 The Apache Software Foundation.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
19
import java.io.PrintWriter;
20
import java.util.Collections;
21
import java.util.HashMap;
22
import java.util.HashSet;
23
import java.util.Iterator;
24
import java.util.NoSuchElementException;
25
import java.util.Vector;
27
import javax.xml.XMLConstants;
28
import javax.xml.namespace.NamespaceContext;
29
import javax.xml.parsers.DocumentBuilder;
30
import javax.xml.parsers.DocumentBuilderFactory;
31
import javax.xml.transform.Source;
32
import javax.xml.transform.dom.DOMSource;
33
import javax.xml.validation.Schema;
34
import javax.xml.validation.SchemaFactory;
35
import javax.xml.validation.Validator;
36
import javax.xml.xpath.XPath;
37
import javax.xml.xpath.XPathConstants;
38
import javax.xml.xpath.XPathExpression;
39
import javax.xml.xpath.XPathFactory;
41
import org.w3c.dom.Document;
42
import org.w3c.dom.NodeList;
43
import org.xml.sax.ErrorHandler;
44
import org.xml.sax.SAXException;
45
import org.xml.sax.SAXNotRecognizedException;
46
import org.xml.sax.SAXNotSupportedException;
47
import org.xml.sax.SAXParseException;
50
* <p>A sample demonstrating how to use the JAXP 1.3 Validation API
51
* to create a validator and use the validator to validate input
52
* from a DOM which contains inline schemas and multiple validation
53
* roots. The output of this program shows the time spent executing
54
* the Validator.validate(Source) method.</p>
56
* <p>This class is useful as a "poor-man's" performance tester to
57
* compare the speed of various JAXP 1.3 validators with different
58
* input sources. However, it is important to note that the first
59
* validation time of a validator will include both VM class load time
60
* and validator initialization that would not be present in subsequent
61
* validations with the same document.</p>
63
* <p><strong>Note:</strong> The results produced by this program
64
* should never be accepted as true performance measurements.</p>
66
* @author Michael Glavassevich, IBM
68
* @version $Id: InlineSchemaValidator.java 329638 2005-10-30 20:49:20Z mrglavas $
70
public class InlineSchemaValidator
71
implements ErrorHandler, NamespaceContext {
79
/** Schema full checking feature id (http://apache.org/xml/features/validation/schema-full-checking). */
80
protected static final String SCHEMA_FULL_CHECKING_FEATURE_ID = "http://apache.org/xml/features/validation/schema-full-checking";
82
/** Honour all schema locations feature id (http://apache.org/xml/features/honour-all-schemaLocations). */
83
protected static final String HONOUR_ALL_SCHEMA_LOCATIONS_ID = "http://apache.org/xml/features/honour-all-schemaLocations";
85
/** Validate schema annotations feature id (http://apache.org/xml/features/validate-annotations) */
86
protected static final String VALIDATE_ANNOTATIONS_ID = "http://apache.org/xml/features/validate-annotations";
88
/** Generate synthetic schema annotations feature id (http://apache.org/xml/features/generate-synthetic-annotations). */
89
protected static final String GENERATE_SYNTHETIC_ANNOTATIONS_ID = "http://apache.org/xml/features/generate-synthetic-annotations";
93
/** Default schema language (http://www.w3.org/2001/XMLSchema). */
94
protected static final String DEFAULT_SCHEMA_LANGUAGE = XMLConstants.W3C_XML_SCHEMA_NS_URI;
96
/** Default repetition (1). */
97
protected static final int DEFAULT_REPETITION = 1;
99
/** Default schema full checking support (false). */
100
protected static final boolean DEFAULT_SCHEMA_FULL_CHECKING = false;
102
/** Default honour all schema locations (false). */
103
protected static final boolean DEFAULT_HONOUR_ALL_SCHEMA_LOCATIONS = false;
105
/** Default validate schema annotations (false). */
106
protected static final boolean DEFAULT_VALIDATE_ANNOTATIONS = false;
108
/** Default generate synthetic schema annotations (false). */
109
protected static final boolean DEFAULT_GENERATE_SYNTHETIC_ANNOTATIONS = false;
111
/** Default memory usage report (false). */
112
protected static final boolean DEFAULT_MEMORY_USAGE = false;
119
protected PrintWriter fOut = new PrintWriter(System.out);
121
/** Prefix to URI mappings for the NamespaceContext. */
122
protected HashMap fPrefixToURIMappings;
124
/** URI to prefix mappings for the NamespaceContext. */
125
protected HashMap fURIToPrefixMappings;
131
public InlineSchemaValidator(HashMap prefixToURIMappings, HashMap uriToPrefixMappings) {
132
fPrefixToURIMappings = prefixToURIMappings;
133
fURIToPrefixMappings = uriToPrefixMappings;
134
} // <init>(HashMap,HashMap)
140
public void validate(Validator validator,
141
Source source, String systemId,
142
int repetitions, boolean memoryUsage) {
144
long timeBefore = System.currentTimeMillis();
145
long memoryBefore = Runtime.getRuntime().freeMemory();
146
for (int j = 0; j < repetitions; ++j) {
147
validator.validate(source);
149
long memoryAfter = Runtime.getRuntime().freeMemory();
150
long timeAfter = System.currentTimeMillis();
152
long time = timeAfter - timeBefore;
153
long memory = memoryUsage
154
? memoryBefore - memoryAfter : Long.MIN_VALUE;
155
printResults(fOut, systemId, time, memory, repetitions);
157
catch (SAXParseException e) {
160
catch (Exception e) {
161
System.err.println("error: Parse error occurred - "+e.getMessage());
163
if (e instanceof SAXException) {
164
se = ((SAXException)e).getException();
167
se.printStackTrace(System.err);
169
e.printStackTrace(System.err);
172
} // validate(Validator,Source,String,int,boolean)
174
/** Prints the results. */
175
public void printResults(PrintWriter out, String uri, long time,
176
long memory, int repetition) {
178
// filename.xml: 631 ms
181
if (repetition == 1) {
187
out.print(repetition);
189
out.print(((float)time)/repetition);
192
if (memory != Long.MIN_VALUE) {
200
} // printResults(PrintWriter,String,long,long,int)
203
// ErrorHandler methods
207
public void warning(SAXParseException ex) throws SAXException {
208
printError("Warning", ex);
209
} // warning(SAXParseException)
212
public void error(SAXParseException ex) throws SAXException {
213
printError("Error", ex);
214
} // error(SAXParseException)
217
public void fatalError(SAXParseException ex) throws SAXException {
218
printError("Fatal Error", ex);
220
} // fatalError(SAXParseException)
223
// NamespaceContext methods
226
public String getNamespaceURI(String prefix) {
227
if (prefix == null) {
228
throw new IllegalArgumentException("Prefix cannot be null.");
230
else if (XMLConstants.XML_NS_PREFIX.equals(prefix)) {
231
return XMLConstants.XML_NS_URI;
233
else if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) {
234
return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
236
else if (fPrefixToURIMappings != null) {
237
String uri = (String) fPrefixToURIMappings.get(prefix);
242
return XMLConstants.NULL_NS_URI;
243
} // getNamespaceURI(String)
245
public String getPrefix(String namespaceURI) {
246
if (namespaceURI == null) {
247
throw new IllegalArgumentException("Namespace URI cannot be null.");
249
else if (XMLConstants.XML_NS_URI.equals(namespaceURI)) {
250
return XMLConstants.XML_NS_PREFIX;
252
else if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI)) {
253
return XMLConstants.XMLNS_ATTRIBUTE;
255
else if (fURIToPrefixMappings != null) {
256
HashSet prefixes = (HashSet) fURIToPrefixMappings.get(namespaceURI);
257
if (prefixes != null && prefixes.size() > 0) {
258
return (String) prefixes.iterator().next();
262
} // getPrefix(String)
264
public Iterator getPrefixes(String namespaceURI) {
265
if (namespaceURI == null) {
266
throw new IllegalArgumentException("Namespace URI cannot be null.");
268
else if (XMLConstants.XML_NS_URI.equals(namespaceURI)) {
269
return new Iterator() {
271
public boolean hasNext() {
274
public Object next() {
276
throw new NoSuchElementException();
279
return XMLConstants.XML_NS_PREFIX;
281
public void remove() {
282
throw new UnsupportedOperationException();
286
else if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI)) {
287
return new Iterator() {
289
public boolean hasNext() {
292
public Object next() {
294
throw new NoSuchElementException();
297
return XMLConstants.XMLNS_ATTRIBUTE;
299
public void remove() {
300
throw new UnsupportedOperationException();
304
else if (fURIToPrefixMappings != null) {
305
HashSet prefixes = (HashSet) fURIToPrefixMappings.get(namespaceURI);
306
if (prefixes != null && prefixes.size() > 0) {
307
return prefixes.iterator();
310
return Collections.EMPTY_LIST.iterator();
311
} // getPrefixes(String)
317
/** Prints the error message. */
318
protected void printError(String type, SAXParseException ex) {
320
System.err.print("[");
321
System.err.print(type);
322
System.err.print("] ");
323
String systemId = ex.getSystemId();
324
if (systemId != null) {
325
int index = systemId.lastIndexOf('/');
327
systemId = systemId.substring(index + 1);
328
System.err.print(systemId);
330
System.err.print(':');
331
System.err.print(ex.getLineNumber());
332
System.err.print(':');
333
System.err.print(ex.getColumnNumber());
334
System.err.print(": ");
335
System.err.print(ex.getMessage());
336
System.err.println();
339
} // printError(String,SAXParseException)
345
/** Main program entry point. */
346
public static void main (String [] argv) {
348
// is there anything to do?
349
if (argv.length == 0) {
355
Vector schemas = null;
356
Vector instances = null;
357
HashMap prefixMappings = null;
358
HashMap uriMappings = null;
359
String docURI = argv[argv.length - 1];
360
String schemaLanguage = DEFAULT_SCHEMA_LANGUAGE;
361
int repetition = DEFAULT_REPETITION;
362
boolean schemaFullChecking = DEFAULT_SCHEMA_FULL_CHECKING;
363
boolean honourAllSchemaLocations = DEFAULT_HONOUR_ALL_SCHEMA_LOCATIONS;
364
boolean validateAnnotations = DEFAULT_VALIDATE_ANNOTATIONS;
365
boolean generateSyntheticAnnotations = DEFAULT_GENERATE_SYNTHETIC_ANNOTATIONS;
366
boolean memoryUsage = DEFAULT_MEMORY_USAGE;
369
for (int i = 0; i < argv.length - 1; ++i) {
370
String arg = argv[i];
371
if (arg.startsWith("-")) {
372
String option = arg.substring(1);
373
if (option.equals("l")) {
374
// get schema language name
375
if (++i == argv.length) {
376
System.err.println("error: Missing argument to -l option.");
379
schemaLanguage = argv[i];
383
if (option.equals("x")) {
384
if (++i == argv.length) {
385
System.err.println("error: Missing argument to -x option.");
388
String number = argv[i];
390
int value = Integer.parseInt(number);
392
System.err.println("error: Repetition must be at least 1.");
397
catch (NumberFormatException e) {
398
System.err.println("error: invalid number ("+number+").");
402
if (arg.equals("-a")) {
403
// process -a: xpath expressions for schemas
404
if (schemas == null) {
405
schemas = new Vector();
407
while (i + 1 < argv.length - 1 && !(arg = argv[i + 1]).startsWith("-")) {
413
if (arg.equals("-i")) {
414
// process -i: xpath expressions for instance documents
415
if (instances == null) {
416
instances = new Vector();
418
while (i + 1 < argv.length - 1 && !(arg = argv[i + 1]).startsWith("-")) {
424
if (arg.equals("-nm")) {
427
while (i + 2 < argv.length - 1 && !(prefix = argv[i + 1]).startsWith("-") &&
428
!(uri = argv[i + 2]).startsWith("-")) {
429
if (prefixMappings == null) {
430
prefixMappings = new HashMap();
431
uriMappings = new HashMap();
433
prefixMappings.put(prefix, uri);
434
HashSet prefixes = (HashSet) uriMappings.get(uri);
435
if (prefixes == null) {
436
prefixes = new HashSet();
437
uriMappings.put(uri, prefixes);
439
prefixes.add(prefix);
444
if (option.equalsIgnoreCase("f")) {
445
schemaFullChecking = option.equals("f");
448
if (option.equalsIgnoreCase("hs")) {
449
honourAllSchemaLocations = option.equals("hs");
452
if (option.equalsIgnoreCase("va")) {
453
validateAnnotations = option.equals("va");
456
if (option.equalsIgnoreCase("ga")) {
457
generateSyntheticAnnotations = option.equals("ga");
460
if (option.equalsIgnoreCase("m")) {
461
memoryUsage = option.equals("m");
464
if (option.equals("h")) {
468
System.err.println("error: unknown option ("+option+").");
474
// Create new instance of inline schema validator.
475
InlineSchemaValidator inlineSchemaValidator = new InlineSchemaValidator(prefixMappings, uriMappings);
477
// Parse document containing schemas and validation roots
478
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
479
dbf.setNamespaceAware(true);
480
DocumentBuilder db = dbf.newDocumentBuilder();
481
db.setErrorHandler(inlineSchemaValidator);
482
Document doc = db.parse(docURI);
484
// Create XPath factory for selecting schema and validation roots
485
XPathFactory xpf = XPathFactory.newInstance();
486
XPath xpath = xpf.newXPath();
487
xpath.setNamespaceContext(inlineSchemaValidator);
489
// Select schema roots from the DOM
490
NodeList [] schemaNodes = new NodeList[schemas != null ? schemas.size() : 0];
491
for (int i = 0; i < schemaNodes.length; ++i) {
492
XPathExpression xpathSchema = xpath.compile((String)schemas.elementAt(i));
493
schemaNodes[i] = (NodeList) xpathSchema.evaluate(doc, XPathConstants.NODESET);
496
// Select validation roots from the DOM
497
NodeList [] instanceNodes = new NodeList[instances != null ? instances.size() : 0];
498
for (int i = 0; i < instanceNodes.length; ++i) {
499
XPathExpression xpathInstance = xpath.compile((String)instances.elementAt(i));
500
instanceNodes[i] = (NodeList) xpathInstance.evaluate(doc, XPathConstants.NODESET);
503
// Create SchemaFactory and configure
504
SchemaFactory factory = SchemaFactory.newInstance(schemaLanguage);
505
factory.setErrorHandler(inlineSchemaValidator);
508
factory.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, schemaFullChecking);
510
catch (SAXNotRecognizedException e) {
511
System.err.println("warning: SchemaFactory does not recognize feature ("+SCHEMA_FULL_CHECKING_FEATURE_ID+")");
513
catch (SAXNotSupportedException e) {
514
System.err.println("warning: SchemaFactory does not support feature ("+SCHEMA_FULL_CHECKING_FEATURE_ID+")");
517
factory.setFeature(HONOUR_ALL_SCHEMA_LOCATIONS_ID, honourAllSchemaLocations);
519
catch (SAXNotRecognizedException e) {
520
System.err.println("warning: SchemaFactory does not recognize feature ("+HONOUR_ALL_SCHEMA_LOCATIONS_ID+")");
522
catch (SAXNotSupportedException e) {
523
System.err.println("warning: SchemaFactory does not support feature ("+HONOUR_ALL_SCHEMA_LOCATIONS_ID+")");
526
factory.setFeature(VALIDATE_ANNOTATIONS_ID, validateAnnotations);
528
catch (SAXNotRecognizedException e) {
529
System.err.println("warning: SchemaFactory does not recognize feature ("+VALIDATE_ANNOTATIONS_ID+")");
531
catch (SAXNotSupportedException e) {
532
System.err.println("warning: SchemaFactory does not support feature ("+VALIDATE_ANNOTATIONS_ID+")");
535
factory.setFeature(GENERATE_SYNTHETIC_ANNOTATIONS_ID, generateSyntheticAnnotations);
537
catch (SAXNotRecognizedException e) {
538
System.err.println("warning: SchemaFactory does not recognize feature ("+GENERATE_SYNTHETIC_ANNOTATIONS_ID+")");
540
catch (SAXNotSupportedException e) {
541
System.err.println("warning: SchemaFactory does not support feature ("+GENERATE_SYNTHETIC_ANNOTATIONS_ID+")");
544
// Build Schema from sources
547
DOMSource [] sources;
549
for (int i = 0; i < schemaNodes.length; ++i) {
550
size += schemaNodes[i].getLength();
552
sources = new DOMSource[size];
554
schema = factory.newSchema();
558
for (int i = 0; i < schemaNodes.length; ++i) {
559
NodeList nodeList = schemaNodes[i];
560
int nodeListLength = nodeList.getLength();
561
for (int j = 0; j < nodeListLength; ++j) {
562
sources[count++] = new DOMSource(nodeList.item(j));
565
schema = factory.newSchema(sources);
569
// Setup validator and input source.
570
Validator validator = schema.newValidator();
571
validator.setErrorHandler(inlineSchemaValidator);
574
validator.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, schemaFullChecking);
576
catch (SAXNotRecognizedException e) {
577
System.err.println("warning: Validator does not recognize feature ("+SCHEMA_FULL_CHECKING_FEATURE_ID+")");
579
catch (SAXNotSupportedException e) {
580
System.err.println("warning: Validator does not support feature ("+SCHEMA_FULL_CHECKING_FEATURE_ID+")");
583
validator.setFeature(HONOUR_ALL_SCHEMA_LOCATIONS_ID, honourAllSchemaLocations);
585
catch (SAXNotRecognizedException e) {
586
System.err.println("warning: Validator does not recognize feature ("+HONOUR_ALL_SCHEMA_LOCATIONS_ID+")");
588
catch (SAXNotSupportedException e) {
589
System.err.println("warning: Validator does not support feature ("+HONOUR_ALL_SCHEMA_LOCATIONS_ID+")");
592
validator.setFeature(VALIDATE_ANNOTATIONS_ID, validateAnnotations);
594
catch (SAXNotRecognizedException e) {
595
System.err.println("warning: Validator does not recognize feature ("+VALIDATE_ANNOTATIONS_ID+")");
597
catch (SAXNotSupportedException e) {
598
System.err.println("warning: Validator does not support feature ("+VALIDATE_ANNOTATIONS_ID+")");
601
validator.setFeature(GENERATE_SYNTHETIC_ANNOTATIONS_ID, generateSyntheticAnnotations);
603
catch (SAXNotRecognizedException e) {
604
System.err.println("warning: Validator does not recognize feature ("+GENERATE_SYNTHETIC_ANNOTATIONS_ID+")");
606
catch (SAXNotSupportedException e) {
607
System.err.println("warning: Validator does not support feature ("+GENERATE_SYNTHETIC_ANNOTATIONS_ID+")");
610
// Validate instance documents
611
for (int i = 0; i < instanceNodes.length; ++i) {
612
NodeList nodeList = instanceNodes[i];
613
int nodeListLength = nodeList.getLength();
614
for (int j = 0; j < nodeListLength; ++j) {
615
DOMSource source = new DOMSource(nodeList.item(j));
616
source.setSystemId(docURI);
617
inlineSchemaValidator.validate(validator, source, docURI, repetition, memoryUsage);
621
catch (SAXParseException e) {
624
catch (Exception e) {
625
System.err.println("error: Parse error occurred - "+e.getMessage());
626
if (e instanceof SAXException) {
627
Exception nested = ((SAXException)e).getException();
628
if (nested != null) {
632
e.printStackTrace(System.err);
637
// Private static methods
640
/** Prints the usage. */
641
private static void printUsage() {
643
System.err.println("usage: java jaxp.InlineSchemaValidator (options) uri ...");
644
System.err.println();
646
System.err.println("options:");
647
System.err.println(" -l name Select schema language by name.");
648
System.err.println(" -x number Select number of repetitions.");
649
System.err.println(" -a xpath ... Provide a list of XPath expressions for schema roots");
650
System.err.println(" -i xpath ... Provide a list of XPath expressions for validation roots");
651
System.err.println(" -nm pre uri ... Provide a list of prefix to namespace URI mappings for the XPath expressions.");
652
System.err.println(" -f | -F Turn on/off Schema full checking.");
653
System.err.println(" NOTE: Not supported by all schema factories and validators.");
654
System.err.println(" -hs | -HS Turn on/off honouring of all schema locations.");
655
System.err.println(" NOTE: Not supported by all schema factories and validators.");
656
System.err.println(" -va | -VA Turn on/off validation of schema annotations.");
657
System.err.println(" NOTE: Not supported by all schema factories and validators.");
658
System.err.println(" -ga | -GA Turn on/off generation of synthetic schema annotations.");
659
System.err.println(" NOTE: Not supported by all schema factories and validators.");
660
System.err.println(" -m | -M Turn on/off memory usage report");
661
System.err.println(" -h This help screen.");
663
System.err.println();
664
System.err.println("defaults:");
665
System.err.println(" Schema language: " + DEFAULT_SCHEMA_LANGUAGE);
666
System.err.println(" Repetition: " + DEFAULT_REPETITION);
667
System.err.print(" Schema full checking: ");
668
System.err.println(DEFAULT_SCHEMA_FULL_CHECKING ? "on" : "off");
669
System.err.print(" Honour all schema locations: ");
670
System.err.println(DEFAULT_HONOUR_ALL_SCHEMA_LOCATIONS ? "on" : "off");
671
System.err.print(" Validate annotations: ");
672
System.err.println(DEFAULT_VALIDATE_ANNOTATIONS ? "on" : "off");
673
System.err.print(" Generate synthetic annotations: ");
674
System.err.println(DEFAULT_GENERATE_SYNTHETIC_ANNOTATIONS ? "on" : "off");
675
System.err.print(" Memory: ");
676
System.err.println(DEFAULT_MEMORY_USAGE ? "on" : "off");
678
System.err.println();
679
System.err.println("notes:");
680
System.err.println(" The speed and memory results from this program should NOT be used as the");
681
System.err.println(" basis of parser performance comparison! Real analytical methods should be");
682
System.err.println(" used. For better results, perform multiple document validations within the");
683
System.err.println(" same virtual machine to remove class loading from parse time and memory usage.");
687
} // class InlineSchemaValidator