KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jaxp > InlineSchemaValidator


1 /*
2  * Copyright 2005 The Apache Software Foundation.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */

16
17 package jaxp;
18
19 import java.io.PrintWriter JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.NoSuchElementException JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 import javax.xml.XMLConstants JavaDoc;
28 import javax.xml.namespace.NamespaceContext JavaDoc;
29 import javax.xml.parsers.DocumentBuilder JavaDoc;
30 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
31 import javax.xml.transform.Source JavaDoc;
32 import javax.xml.transform.dom.DOMSource JavaDoc;
33 import javax.xml.validation.Schema JavaDoc;
34 import javax.xml.validation.SchemaFactory JavaDoc;
35 import javax.xml.validation.Validator JavaDoc;
36 import javax.xml.xpath.XPath JavaDoc;
37 import javax.xml.xpath.XPathConstants JavaDoc;
38 import javax.xml.xpath.XPathExpression JavaDoc;
39 import javax.xml.xpath.XPathFactory JavaDoc;
40
41 import org.w3c.dom.Document JavaDoc;
42 import org.w3c.dom.NodeList JavaDoc;
43 import org.xml.sax.ErrorHandler JavaDoc;
44 import org.xml.sax.SAXException JavaDoc;
45 import org.xml.sax.SAXNotRecognizedException JavaDoc;
46 import org.xml.sax.SAXNotSupportedException JavaDoc;
47 import org.xml.sax.SAXParseException JavaDoc;
48
49 /**
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>
55  *
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>
62  *
63  * <p><strong>Note:</strong> The results produced by this program
64  * should never be accepted as true performance measurements.</p>
65  *
66  * @author Michael Glavassevich, IBM
67  *
68  * @version $Id: InlineSchemaValidator.java,v 1.1 2005/06/01 04:48:36 mrglavas Exp $
69  */

70 public class InlineSchemaValidator
71     implements ErrorHandler JavaDoc, NamespaceContext JavaDoc {
72     
73     //
74
// Constants
75
//
76

77     // feature ids
78

79     /** Schema full checking feature id (http://apache.org/xml/features/validation/schema-full-checking). */
80     protected static final String JavaDoc SCHEMA_FULL_CHECKING_FEATURE_ID = "http://apache.org/xml/features/validation/schema-full-checking";
81     
82     /** Honour all schema locations feature id (http://apache.org/xml/features/honour-all-schemaLocations). */
83     protected static final String JavaDoc HONOUR_ALL_SCHEMA_LOCATIONS_ID = "http://apache.org/xml/features/honour-all-schemaLocations";
84     
85     /** Validate schema annotations feature id (http://apache.org/xml/features/validate-annotations) */
86     protected static final String JavaDoc VALIDATE_ANNOTATIONS_ID = "http://apache.org/xml/features/validate-annotations";
87     
88     /** Generate synthetic schema annotations feature id (http://apache.org/xml/features/generate-synthetic-annotations). */
89     protected static final String JavaDoc GENERATE_SYNTHETIC_ANNOTATIONS_ID = "http://apache.org/xml/features/generate-synthetic-annotations";
90     
91     // default settings
92

93     /** Default repetition (1). */
94     protected static final int DEFAULT_REPETITION = 1;
95     
96     /** Default schema full checking support (false). */
97     protected static final boolean DEFAULT_SCHEMA_FULL_CHECKING = false;
98     
99     /** Default honour all schema locations (false). */
100     protected static final boolean DEFAULT_HONOUR_ALL_SCHEMA_LOCATIONS = false;
101     
102     /** Default validate schema annotations (false). */
103     protected static final boolean DEFAULT_VALIDATE_ANNOTATIONS = false;
104     
105     /** Default generate synthetic schema annotations (false). */
106     protected static final boolean DEFAULT_GENERATE_SYNTHETIC_ANNOTATIONS = false;
107     
108     /** Default memory usage report (false). */
109     protected static final boolean DEFAULT_MEMORY_USAGE = false;
110     
111     //
112
// Data
113
//
114

115     /** Print writer. */
116     protected PrintWriter JavaDoc fOut = new PrintWriter JavaDoc(System.out);
117     
118     /** Prefix to URI mappings for the NamespaceContext. */
119     protected HashMap JavaDoc fPrefixToURIMappings;
120     
121     /** URI to prefix mappings for the NamespaceContext. */
122     protected HashMap JavaDoc fURIToPrefixMappings;
123
124     //
125
// Constructors
126
//
127

128     public InlineSchemaValidator(HashMap JavaDoc prefixToURIMappings, HashMap JavaDoc uriToPrefixMappings) {
129         fPrefixToURIMappings = prefixToURIMappings;
130         fURIToPrefixMappings = uriToPrefixMappings;
131     } // <init>(HashMap,HashMap)
132

133     //
134
// Public methods
135
//
136

137     public void validate(Validator JavaDoc validator,
138             Source JavaDoc source, String JavaDoc systemId,
139             int repetitions, boolean memoryUsage) {
140         try {
141             long timeBefore = System.currentTimeMillis();
142             long memoryBefore = Runtime.getRuntime().freeMemory();
143             for (int j = 0; j < repetitions; ++j) {
144                 validator.validate(source);
145             }
146             long memoryAfter = Runtime.getRuntime().freeMemory();
147             long timeAfter = System.currentTimeMillis();
148
149             long time = timeAfter - timeBefore;
150             long memory = memoryUsage
151                         ? memoryBefore - memoryAfter : Long.MIN_VALUE;
152             printResults(fOut, systemId, time, memory, repetitions);
153         }
154         catch (SAXParseException JavaDoc e) {
155             // ignore
156
}
157         catch (Exception JavaDoc e) {
158             System.err.println("error: Parse error occurred - "+e.getMessage());
159             Exception JavaDoc se = e;
160             if (e instanceof SAXException JavaDoc) {
161                 se = ((SAXException JavaDoc)e).getException();
162             }
163             if (se != null)
164               se.printStackTrace(System.err);
165             else
166               e.printStackTrace(System.err);
167
168         }
169     } // validate(Validator,Source,String,int,boolean)
170

171     /** Prints the results. */
172     public void printResults(PrintWriter JavaDoc out, String JavaDoc uri, long time,
173                              long memory, int repetition) {
174         
175         // filename.xml: 631 ms
176
out.print(uri);
177         out.print(": ");
178         if (repetition == 1) {
179             out.print(time);
180         }
181         else {
182             out.print(time);
183             out.print('/');
184             out.print(repetition);
185             out.print('=');
186             out.print(((float)time)/repetition);
187         }
188         out.print(" ms");
189         if (memory != Long.MIN_VALUE) {
190             out.print(", ");
191             out.print(memory);
192             out.print(" bytes");
193         }
194         out.println();
195         out.flush();
196
197     } // printResults(PrintWriter,String,long,long,int)
198

199     //
200
// ErrorHandler methods
201
//
202

203     /** Warning. */
204     public void warning(SAXParseException JavaDoc ex) throws SAXException JavaDoc {
205         printError("Warning", ex);
206     } // warning(SAXParseException)
207

208     /** Error. */
209     public void error(SAXParseException JavaDoc ex) throws SAXException JavaDoc {
210         printError("Error", ex);
211     } // error(SAXParseException)
212

213     /** Fatal error. */
214     public void fatalError(SAXParseException JavaDoc ex) throws SAXException JavaDoc {
215         printError("Fatal Error", ex);
216         throw ex;
217     } // fatalError(SAXParseException)
218

219     //
220
// NamespaceContext methods
221
//
222

223     public String JavaDoc getNamespaceURI(String JavaDoc prefix) {
224         if (prefix == null) {
225             throw new IllegalArgumentException JavaDoc("Prefix cannot be null.");
226         }
227         else if (XMLConstants.XML_NS_PREFIX.equals(prefix)) {
228             return XMLConstants.XML_NS_URI;
229         }
230         else if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) {
231             return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
232         }
233         else if (fPrefixToURIMappings != null) {
234             String JavaDoc uri = (String JavaDoc) fPrefixToURIMappings.get(prefix);
235             if (uri != null) {
236                 return uri;
237             }
238         }
239         return XMLConstants.NULL_NS_URI;
240     } // getNamespaceURI(String)
241

242     public String JavaDoc getPrefix(String JavaDoc namespaceURI) {
243         if (namespaceURI == null) {
244             throw new IllegalArgumentException JavaDoc("Namespace URI cannot be null.");
245         }
246         else if (XMLConstants.XML_NS_URI.equals(namespaceURI)) {
247             return XMLConstants.XML_NS_PREFIX;
248         }
249         else if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI)) {
250             return XMLConstants.XMLNS_ATTRIBUTE;
251         }
252         else if (fURIToPrefixMappings != null) {
253             HashSet JavaDoc prefixes = (HashSet JavaDoc) fURIToPrefixMappings.get(namespaceURI);
254             if (prefixes != null && prefixes.size() > 0) {
255                 return (String JavaDoc) prefixes.iterator().next();
256             }
257         }
258         return null;
259     } // getPrefix(String)
260

261     public Iterator JavaDoc getPrefixes(String JavaDoc namespaceURI) {
262         if (namespaceURI == null) {
263             throw new IllegalArgumentException JavaDoc("Namespace URI cannot be null.");
264         }
265         else if (XMLConstants.XML_NS_URI.equals(namespaceURI)) {
266             return new Iterator JavaDoc() {
267                 boolean more = true;
268                 public boolean hasNext() {
269                     return more;
270                 }
271                 public Object JavaDoc next() {
272                     if (!hasNext()) {
273                         throw new NoSuchElementException JavaDoc();
274                     }
275                     more = false;
276                     return XMLConstants.XML_NS_PREFIX;
277                 }
278                 public void remove() {
279                     throw new UnsupportedOperationException JavaDoc();
280                 }
281             };
282         }
283         else if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI)) {
284             return new Iterator JavaDoc() {
285                 boolean more = true;
286                 public boolean hasNext() {
287                     return more;
288                 }
289                 public Object JavaDoc next() {
290                     if (!hasNext()) {
291                         throw new NoSuchElementException JavaDoc();
292                     }
293                     more = false;
294                     return XMLConstants.XMLNS_ATTRIBUTE;
295                 }
296                 public void remove() {
297                     throw new UnsupportedOperationException JavaDoc();
298                 }
299             };
300         }
301         else if (fURIToPrefixMappings != null) {
302             HashSet JavaDoc prefixes = (HashSet JavaDoc) fURIToPrefixMappings.get(namespaceURI);
303             if (prefixes != null && prefixes.size() > 0) {
304                 return prefixes.iterator();
305             }
306         }
307         return Collections.EMPTY_LIST.iterator();
308     } // getPrefixes(String)
309

310     //
311
// Protected methods
312
//
313

314     /** Prints the error message. */
315     protected void printError(String JavaDoc type, SAXParseException JavaDoc ex) {
316
317         System.err.print("[");
318         System.err.print(type);
319         System.err.print("] ");
320         String JavaDoc systemId = ex.getSystemId();
321         if (systemId != null) {
322             int index = systemId.lastIndexOf('/');
323             if (index != -1)
324                 systemId = systemId.substring(index + 1);
325             System.err.print(systemId);
326         }
327         System.err.print(':');
328         System.err.print(ex.getLineNumber());
329         System.err.print(':');
330         System.err.print(ex.getColumnNumber());
331         System.err.print(": ");
332         System.err.print(ex.getMessage());
333         System.err.println();
334         System.err.flush();
335
336     } // printError(String,SAXParseException)
337

338     //
339
// MAIN
340
//
341

342     /** Main program entry point. */
343     public static void main (String JavaDoc [] argv) {
344         
345         // is there anything to do?
346
if (argv.length == 0) {
347             printUsage();
348             System.exit(1);
349         }
350         
351         // variables
352
Vector JavaDoc schemas = null;
353         Vector JavaDoc instances = null;
354         HashMap JavaDoc prefixMappings = null;
355         HashMap JavaDoc uriMappings = null;
356         String JavaDoc docURI = argv[argv.length - 1];
357         int repetition = DEFAULT_REPETITION;
358         boolean schemaFullChecking = DEFAULT_SCHEMA_FULL_CHECKING;
359         boolean honourAllSchemaLocations = DEFAULT_HONOUR_ALL_SCHEMA_LOCATIONS;
360         boolean validateAnnotations = DEFAULT_VALIDATE_ANNOTATIONS;
361         boolean generateSyntheticAnnotations = DEFAULT_GENERATE_SYNTHETIC_ANNOTATIONS;
362         boolean memoryUsage = DEFAULT_MEMORY_USAGE;
363         
364         // process arguments
365
for (int i = 0; i < argv.length - 1; ++i) {
366             String JavaDoc arg = argv[i];
367             if (arg.startsWith("-")) {
368                 String JavaDoc option = arg.substring(1);
369                 if (option.equals("x")) {
370                     if (++i == argv.length) {
371                         System.err.println("error: Missing argument to -x option.");
372                         continue;
373                     }
374                     String JavaDoc number = argv[i];
375                     try {
376                         int value = Integer.parseInt(number);
377                         if (value < 1) {
378                             System.err.println("error: Repetition must be at least 1.");
379                             continue;
380                         }
381                         repetition = value;
382                     }
383                     catch (NumberFormatException JavaDoc e) {
384                         System.err.println("error: invalid number ("+number+").");
385                     }
386                     continue;
387                 }
388                 if (arg.equals("-a")) {
389                     // process -a: xpath expressions for schemas
390
if (schemas == null) {
391                         schemas = new Vector JavaDoc();
392                     }
393                     while (i + 1 < argv.length - 1 && !(arg = argv[i + 1]).startsWith("-")) {
394                         schemas.add(arg);
395                         ++i;
396                     }
397                     continue;
398                 }
399                 if (arg.equals("-i")) {
400                     // process -i: xpath expressions for instance documents
401
if (instances == null) {
402                         instances = new Vector JavaDoc();
403                     }
404                     while (i + 1 < argv.length - 1 && !(arg = argv[i + 1]).startsWith("-")) {
405                         instances.add(arg);
406                         ++i;
407                     }
408                     continue;
409                 }
410                 if (arg.equals("-nm")) {
411                     String JavaDoc prefix;
412                     String JavaDoc uri;
413                     while (i + 2 < argv.length - 1 && !(prefix = argv[i + 1]).startsWith("-") &&
414                             !(uri = argv[i + 2]).startsWith("-")) {
415                         if (prefixMappings == null) {
416                             prefixMappings = new HashMap JavaDoc();
417                             uriMappings = new HashMap JavaDoc();
418                         }
419                         prefixMappings.put(prefix, uri);
420                         HashSet JavaDoc prefixes = (HashSet JavaDoc) uriMappings.get(uri);
421                         if (prefixes == null) {
422                             prefixes = new HashSet JavaDoc();
423                             uriMappings.put(uri, prefixes);
424                         }
425                         prefixes.add(prefix);
426                         i += 2;
427                     }
428                     continue;
429                 }
430                 if (option.equalsIgnoreCase("f")) {
431                     schemaFullChecking = option.equals("f");
432                     continue;
433                 }
434                 if (option.equalsIgnoreCase("hs")) {
435                     honourAllSchemaLocations = option.equals("hs");
436                     continue;
437                 }
438                 if (option.equalsIgnoreCase("va")) {
439                     validateAnnotations = option.equals("va");
440                     continue;
441                 }
442                 if (option.equalsIgnoreCase("ga")) {
443                     generateSyntheticAnnotations = option.equals("ga");
444                     continue;
445                 }
446                 if (option.equalsIgnoreCase("m")) {
447                     memoryUsage = option.equals("m");
448                     continue;
449                 }
450                 if (option.equals("h")) {
451                     printUsage();
452                     continue;
453                 }
454                 System.err.println("error: unknown option ("+option+").");
455                 continue;
456             }
457         }
458         
459         try {
460             // Create new instance of inline schema validator.
461
InlineSchemaValidator inlineSchemaValidator = new InlineSchemaValidator(prefixMappings, uriMappings);
462             
463             // Parse document containing schemas and validation roots
464
DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
465             dbf.setNamespaceAware(true);
466             DocumentBuilder JavaDoc db = dbf.newDocumentBuilder();
467             db.setErrorHandler(inlineSchemaValidator);
468             Document JavaDoc doc = db.parse(docURI);
469             
470             // Create XPath factory for selecting schema and validation roots
471
XPathFactory JavaDoc xpf = XPathFactory.newInstance();
472             XPath JavaDoc xpath = xpf.newXPath();
473             xpath.setNamespaceContext(inlineSchemaValidator);
474             
475             // Select schema roots from the DOM
476
NodeList JavaDoc [] schemaNodes = new NodeList JavaDoc[schemas != null ? schemas.size() : 0];
477             for (int i = 0; i < schemaNodes.length; ++i) {
478                 XPathExpression JavaDoc xpathSchema = xpath.compile((String JavaDoc)schemas.elementAt(i));
479                 schemaNodes[i] = (NodeList JavaDoc) xpathSchema.evaluate(doc, XPathConstants.NODESET);
480             }
481             
482             // Select validation roots from the DOM
483
NodeList JavaDoc [] instanceNodes = new NodeList JavaDoc[instances != null ? instances.size() : 0];
484             for (int i = 0; i < instanceNodes.length; ++i) {
485                 XPathExpression JavaDoc xpathInstance = xpath.compile((String JavaDoc)instances.elementAt(i));
486                 instanceNodes[i] = (NodeList JavaDoc) xpathInstance.evaluate(doc, XPathConstants.NODESET);
487             }
488
489             // Create SchemaFactory and configure
490
SchemaFactory JavaDoc factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
491             factory.setErrorHandler(inlineSchemaValidator);
492             
493             try {
494                 factory.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, schemaFullChecking);
495             }
496             catch (SAXNotRecognizedException JavaDoc e) {
497                 System.err.println("warning: SchemaFactory does not recognize feature ("+SCHEMA_FULL_CHECKING_FEATURE_ID+")");
498             }
499             catch (SAXNotSupportedException JavaDoc e) {
500                 System.err.println("warning: SchemaFactory does not support feature ("+SCHEMA_FULL_CHECKING_FEATURE_ID+")");
501             }
502             try {
503                 factory.setFeature(HONOUR_ALL_SCHEMA_LOCATIONS_ID, honourAllSchemaLocations);
504             }
505             catch (SAXNotRecognizedException JavaDoc e) {
506                 System.err.println("warning: SchemaFactory does not recognize feature ("+HONOUR_ALL_SCHEMA_LOCATIONS_ID+")");
507             }
508             catch (SAXNotSupportedException JavaDoc e) {
509                 System.err.println("warning: SchemaFactory does not support feature ("+HONOUR_ALL_SCHEMA_LOCATIONS_ID+")");
510             }
511             try {
512                 factory.setFeature(VALIDATE_ANNOTATIONS_ID, validateAnnotations);
513             }
514             catch (SAXNotRecognizedException JavaDoc e) {
515                 System.err.println("warning: SchemaFactory does not recognize feature ("+VALIDATE_ANNOTATIONS_ID+")");
516             }
517             catch (SAXNotSupportedException JavaDoc e) {
518                 System.err.println("warning: SchemaFactory does not support feature ("+VALIDATE_ANNOTATIONS_ID+")");
519             }
520             try {
521                 factory.setFeature(GENERATE_SYNTHETIC_ANNOTATIONS_ID, generateSyntheticAnnotations);
522             }
523             catch (SAXNotRecognizedException JavaDoc e) {
524                 System.err.println("warning: SchemaFactory does not recognize feature ("+GENERATE_SYNTHETIC_ANNOTATIONS_ID+")");
525             }
526             catch (SAXNotSupportedException JavaDoc e) {
527                 System.err.println("warning: SchemaFactory does not support feature ("+GENERATE_SYNTHETIC_ANNOTATIONS_ID+")");
528             }
529             
530             // Build Schema from sources
531
Schema JavaDoc schema;
532             {
533                 DOMSource JavaDoc [] sources;
534                 int size = 0;
535                 for (int i = 0; i < schemaNodes.length; ++i) {
536                     size += schemaNodes[i].getLength();
537                 }
538                 sources = new DOMSource JavaDoc[size];
539                 if (size == 0) {
540                     schema = factory.newSchema();
541                 }
542                 else {
543                     int count = 0;
544                     for (int i = 0; i < schemaNodes.length; ++i) {
545                         NodeList JavaDoc nodeList = schemaNodes[i];
546                         int nodeListLength = nodeList.getLength();
547                         for (int j = 0; j < nodeListLength; ++j) {
548                             sources[count++] = new DOMSource JavaDoc(nodeList.item(j));
549                         }
550                     }
551                     schema = factory.newSchema(sources);
552                 }
553             }
554             
555             // Setup validator and input source.
556
Validator JavaDoc validator = schema.newValidator();
557             validator.setErrorHandler(inlineSchemaValidator);
558             
559             try {
560                 validator.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, schemaFullChecking);
561             }
562             catch (SAXNotRecognizedException JavaDoc e) {
563                 System.err.println("warning: Validator does not recognize feature ("+SCHEMA_FULL_CHECKING_FEATURE_ID+")");
564             }
565             catch (SAXNotSupportedException JavaDoc e) {
566                 System.err.println("warning: Validator does not support feature ("+SCHEMA_FULL_CHECKING_FEATURE_ID+")");
567             }
568             try {
569                 validator.setFeature(HONOUR_ALL_SCHEMA_LOCATIONS_ID, honourAllSchemaLocations);
570             }
571             catch (SAXNotRecognizedException JavaDoc e) {
572                 System.err.println("warning: Validator does not recognize feature ("+HONOUR_ALL_SCHEMA_LOCATIONS_ID+")");
573             }
574             catch (SAXNotSupportedException JavaDoc e) {
575                 System.err.println("warning: Validator does not support feature ("+HONOUR_ALL_SCHEMA_LOCATIONS_ID+")");
576             }
577             try {
578                 validator.setFeature(VALIDATE_ANNOTATIONS_ID, validateAnnotations);
579             }
580             catch (SAXNotRecognizedException JavaDoc e) {
581                 System.err.println("warning: Validator does not recognize feature ("+VALIDATE_ANNOTATIONS_ID+")");
582             }
583             catch (SAXNotSupportedException JavaDoc e) {
584                 System.err.println("warning: Validator does not support feature ("+VALIDATE_ANNOTATIONS_ID+")");
585             }
586             try {
587                 validator.setFeature(GENERATE_SYNTHETIC_ANNOTATIONS_ID, generateSyntheticAnnotations);
588             }
589             catch (SAXNotRecognizedException JavaDoc e) {
590                 System.err.println("warning: Validator does not recognize feature ("+GENERATE_SYNTHETIC_ANNOTATIONS_ID+")");
591             }
592             catch (SAXNotSupportedException JavaDoc e) {
593                 System.err.println("warning: Validator does not support feature ("+GENERATE_SYNTHETIC_ANNOTATIONS_ID+")");
594             }
595
596             // Validate instance documents
597
for (int i = 0; i < instanceNodes.length; ++i) {
598                 NodeList JavaDoc nodeList = instanceNodes[i];
599                 int nodeListLength = nodeList.getLength();
600                 for (int j = 0; j < nodeListLength; ++j) {
601                     DOMSource JavaDoc source = new DOMSource JavaDoc(nodeList.item(j));
602                     source.setSystemId(docURI);
603                     inlineSchemaValidator.validate(validator, source, docURI, repetition, memoryUsage);
604                 }
605             }
606         }
607         catch (SAXParseException JavaDoc e) {
608             // ignore
609
}
610         catch (Exception JavaDoc e) {
611             System.err.println("error: Parse error occurred - "+e.getMessage());
612             if (e instanceof SAXException JavaDoc) {
613                 Exception JavaDoc nested = ((SAXException JavaDoc)e).getException();
614                 if (nested != null) {
615                     e = nested;
616                 }
617             }
618             e.printStackTrace(System.err);
619         }
620     } // main(String[])
621

622     //
623
// Private static methods
624
//
625

626     /** Prints the usage. */
627     private static void printUsage() {
628         
629         System.err.println("usage: java jaxp.InlineSchemaValidator (options) uri ...");
630         System.err.println();
631         
632         System.err.println("options:");
633         System.err.println(" -x number Select number of repetitions.");
634         System.err.println(" -a xpath ... Provide a list of XPath expressions for schema roots");
635         System.err.println(" -i xpath ... Provide a list of XPath expressions for validation roots");
636         System.err.println(" -nm pre uri ... Provide a list of prefix to namespace URI mappings for the XPath expressions.");
637         System.err.println(" -f | -F Turn on/off Schema full checking.");
638         System.err.println(" NOTE: Not supported by all schema factories and validators.");
639         System.err.println(" -hs | -HS Turn on/off honouring of all schema locations.");
640         System.err.println(" NOTE: Not supported by all schema factories and validators.");
641         System.err.println(" -va | -VA Turn on/off validation of schema annotations.");
642         System.err.println(" NOTE: Not supported by all schema factories and validators.");
643         System.err.println(" -ga | -GA Turn on/off generation of synthetic schema annotations.");
644         System.err.println(" NOTE: Not supported by all schema factories and validators.");
645         System.err.println(" -m | -M Turn on/off memory usage report");
646         System.err.println(" -h This help screen.");
647         
648         System.err.println();
649         System.err.println("defaults:");
650         System.err.println(" Repetition: " + DEFAULT_REPETITION);
651         System.err.print(" Schema full checking: ");
652         System.err.println(DEFAULT_SCHEMA_FULL_CHECKING ? "on" : "off");
653         System.err.print(" Honour all schema locations: ");
654         System.err.println(DEFAULT_HONOUR_ALL_SCHEMA_LOCATIONS ? "on" : "off");
655         System.err.print(" Validate annotations: ");
656         System.err.println(DEFAULT_VALIDATE_ANNOTATIONS ? "on" : "off");
657         System.err.print(" Generate synthetic annotations: ");
658         System.err.println(DEFAULT_GENERATE_SYNTHETIC_ANNOTATIONS ? "on" : "off");
659         System.err.print(" Memory: ");
660         System.err.println(DEFAULT_MEMORY_USAGE ? "on" : "off");
661         
662         System.err.println();
663         System.err.println("notes:");
664         System.err.println(" The speed and memory results from this program should NOT be used as the");
665         System.err.println(" basis of parser performance comparison! Real analytical methods should be");
666         System.err.println(" used. For better results, perform multiple document validations within the");
667         System.err.println(" same virtual machine to remove class loading from parse time and memory usage.");
668         
669     } // printUsage()
670

671 } // class InlineSchemaValidator
672
Popular Tags