~ubuntu-branches/ubuntu/karmic/libxerces2-java/karmic

« back to all changes in this revision

Viewing changes to samples/jaxp/InlineSchemaValidator.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-12-04 17:37:55 UTC
  • mfrom: (2.1.2 etch)
  • Revision ID: james.westby@ubuntu.com-20061204173755-hb6ybrrrk097zhx7
Tags: 2.8.1-1ubuntu1
* Merge with Debian unstable; remaining changes:
  - Build -gcj package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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;
 
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;
 
26
 
 
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;
 
40
 
 
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;
 
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 329638 2005-10-30 20:49:20Z mrglavas $
 
69
 */
 
70
public class InlineSchemaValidator 
 
71
    implements ErrorHandler, NamespaceContext {
 
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 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 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 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 GENERATE_SYNTHETIC_ANNOTATIONS_ID = "http://apache.org/xml/features/generate-synthetic-annotations";
 
90
    
 
91
    // default settings
 
92
    
 
93
    /** Default schema language (http://www.w3.org/2001/XMLSchema). */
 
94
    protected static final String DEFAULT_SCHEMA_LANGUAGE = XMLConstants.W3C_XML_SCHEMA_NS_URI;
 
95
    
 
96
    /** Default repetition (1). */
 
97
    protected static final int DEFAULT_REPETITION = 1;
 
98
    
 
99
    /** Default schema full checking support (false). */
 
100
    protected static final boolean DEFAULT_SCHEMA_FULL_CHECKING = false;
 
101
    
 
102
    /** Default honour all schema locations (false). */
 
103
    protected static final boolean DEFAULT_HONOUR_ALL_SCHEMA_LOCATIONS = false;
 
104
    
 
105
    /** Default validate schema annotations (false). */
 
106
    protected static final boolean DEFAULT_VALIDATE_ANNOTATIONS = false;
 
107
    
 
108
    /** Default generate synthetic schema annotations (false). */
 
109
    protected static final boolean DEFAULT_GENERATE_SYNTHETIC_ANNOTATIONS = false;
 
110
    
 
111
    /** Default memory usage report (false). */
 
112
    protected static final boolean DEFAULT_MEMORY_USAGE = false;
 
113
    
 
114
    //
 
115
    // Data
 
116
    //
 
117
    
 
118
    /** Print writer. */
 
119
    protected PrintWriter fOut = new PrintWriter(System.out);
 
120
    
 
121
    /** Prefix to URI mappings for the NamespaceContext. */
 
122
    protected HashMap fPrefixToURIMappings;
 
123
    
 
124
    /** URI to prefix mappings for the NamespaceContext. */
 
125
    protected HashMap fURIToPrefixMappings;
 
126
 
 
127
    //
 
128
    // Constructors
 
129
    //
 
130
 
 
131
    public InlineSchemaValidator(HashMap prefixToURIMappings, HashMap uriToPrefixMappings) {
 
132
        fPrefixToURIMappings = prefixToURIMappings;
 
133
        fURIToPrefixMappings = uriToPrefixMappings;
 
134
    } // <init>(HashMap,HashMap)
 
135
    
 
136
    //
 
137
    // Public methods
 
138
    //
 
139
    
 
140
    public void validate(Validator validator, 
 
141
            Source source, String systemId,
 
142
            int repetitions, boolean memoryUsage) {
 
143
        try {
 
144
            long timeBefore = System.currentTimeMillis();
 
145
            long memoryBefore = Runtime.getRuntime().freeMemory();
 
146
            for (int j = 0; j < repetitions; ++j) {
 
147
                validator.validate(source);
 
148
            }
 
149
            long memoryAfter = Runtime.getRuntime().freeMemory();
 
150
            long timeAfter = System.currentTimeMillis();
 
151
 
 
152
            long time = timeAfter - timeBefore;
 
153
            long memory = memoryUsage
 
154
                        ? memoryBefore - memoryAfter : Long.MIN_VALUE;
 
155
            printResults(fOut, systemId, time, memory, repetitions);
 
156
        }
 
157
        catch (SAXParseException e) {
 
158
            // ignore
 
159
        }
 
160
        catch (Exception e) {
 
161
            System.err.println("error: Parse error occurred - "+e.getMessage());
 
162
            Exception se = e;
 
163
            if (e instanceof SAXException) {
 
164
                se = ((SAXException)e).getException();
 
165
            }
 
166
            if (se != null)
 
167
              se.printStackTrace(System.err);
 
168
            else
 
169
              e.printStackTrace(System.err);
 
170
 
 
171
        }
 
172
    } // validate(Validator,Source,String,int,boolean)
 
173
    
 
174
    /** Prints the results. */
 
175
    public void printResults(PrintWriter out, String uri, long time,
 
176
                             long memory, int repetition) {
 
177
        
 
178
        // filename.xml: 631 ms
 
179
        out.print(uri);
 
180
        out.print(": ");
 
181
        if (repetition == 1) {
 
182
            out.print(time);
 
183
        }
 
184
        else {
 
185
            out.print(time);
 
186
            out.print('/');
 
187
            out.print(repetition);
 
188
            out.print('=');
 
189
            out.print(((float)time)/repetition);
 
190
        }
 
191
        out.print(" ms");
 
192
        if (memory != Long.MIN_VALUE) {
 
193
            out.print(", ");
 
194
            out.print(memory);
 
195
            out.print(" bytes");
 
196
        }
 
197
        out.println();
 
198
        out.flush();
 
199
 
 
200
    } // printResults(PrintWriter,String,long,long,int)
 
201
    
 
202
    //
 
203
    // ErrorHandler methods
 
204
    //
 
205
 
 
206
    /** Warning. */
 
207
    public void warning(SAXParseException ex) throws SAXException {
 
208
        printError("Warning", ex);
 
209
    } // warning(SAXParseException)
 
210
 
 
211
    /** Error. */
 
212
    public void error(SAXParseException ex) throws SAXException {
 
213
        printError("Error", ex);
 
214
    } // error(SAXParseException)
 
215
 
 
216
    /** Fatal error. */
 
217
    public void fatalError(SAXParseException ex) throws SAXException {
 
218
        printError("Fatal Error", ex);
 
219
        throw ex;
 
220
    } // fatalError(SAXParseException)
 
221
    
 
222
    //
 
223
    // NamespaceContext methods
 
224
    //
 
225
    
 
226
    public String getNamespaceURI(String prefix) {
 
227
        if (prefix == null) {
 
228
            throw new IllegalArgumentException("Prefix cannot be null.");
 
229
        }
 
230
        else if (XMLConstants.XML_NS_PREFIX.equals(prefix)) {
 
231
            return XMLConstants.XML_NS_URI;
 
232
        }
 
233
        else if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) {
 
234
            return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
 
235
        }
 
236
        else if (fPrefixToURIMappings != null) {
 
237
            String uri = (String) fPrefixToURIMappings.get(prefix);
 
238
            if (uri != null) {
 
239
                return uri;
 
240
            }
 
241
        }
 
242
        return XMLConstants.NULL_NS_URI;
 
243
    } // getNamespaceURI(String)
 
244
 
 
245
    public String getPrefix(String namespaceURI) {
 
246
        if (namespaceURI == null) {
 
247
            throw new IllegalArgumentException("Namespace URI cannot be null.");
 
248
        }
 
249
        else if (XMLConstants.XML_NS_URI.equals(namespaceURI)) {
 
250
            return XMLConstants.XML_NS_PREFIX;
 
251
        }
 
252
        else if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI)) {
 
253
            return XMLConstants.XMLNS_ATTRIBUTE;
 
254
        }
 
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();
 
259
            }
 
260
        }
 
261
        return null;
 
262
    } // getPrefix(String)
 
263
 
 
264
    public Iterator getPrefixes(String namespaceURI) {
 
265
        if (namespaceURI == null) {
 
266
            throw new IllegalArgumentException("Namespace URI cannot be null.");
 
267
        }
 
268
        else if (XMLConstants.XML_NS_URI.equals(namespaceURI)) {
 
269
            return new Iterator() {
 
270
                boolean more = true;
 
271
                public boolean hasNext() {
 
272
                    return more;
 
273
                }
 
274
                public Object next() {
 
275
                    if (!hasNext()) {
 
276
                        throw new NoSuchElementException();
 
277
                    }
 
278
                    more = false;
 
279
                    return XMLConstants.XML_NS_PREFIX;
 
280
                }
 
281
                public void remove() {
 
282
                    throw new UnsupportedOperationException();                   
 
283
                }  
 
284
            };
 
285
        }
 
286
        else if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI)) {
 
287
            return new Iterator() {
 
288
                boolean more = true;
 
289
                public boolean hasNext() {
 
290
                    return more;
 
291
                }
 
292
                public Object next() {
 
293
                    if (!hasNext()) {
 
294
                        throw new NoSuchElementException();
 
295
                    }
 
296
                    more = false;
 
297
                    return XMLConstants.XMLNS_ATTRIBUTE;
 
298
                }
 
299
                public void remove() {
 
300
                    throw new UnsupportedOperationException();                   
 
301
                }  
 
302
            };
 
303
        }
 
304
        else if (fURIToPrefixMappings != null) {
 
305
            HashSet prefixes = (HashSet) fURIToPrefixMappings.get(namespaceURI);
 
306
            if (prefixes != null && prefixes.size() > 0) {
 
307
                return prefixes.iterator();
 
308
            }
 
309
        }
 
310
        return Collections.EMPTY_LIST.iterator();
 
311
    } // getPrefixes(String)
 
312
    
 
313
    //
 
314
    // Protected methods
 
315
    //
 
316
    
 
317
    /** Prints the error message. */
 
318
    protected void printError(String type, SAXParseException ex) {
 
319
 
 
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('/');
 
326
            if (index != -1)
 
327
                systemId = systemId.substring(index + 1);
 
328
            System.err.print(systemId);
 
329
        }
 
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();
 
337
        System.err.flush();
 
338
 
 
339
    } // printError(String,SAXParseException)
 
340
    
 
341
    //
 
342
    // MAIN
 
343
    //
 
344
 
 
345
    /** Main program entry point. */
 
346
    public static void main (String [] argv) {
 
347
        
 
348
        // is there anything to do?
 
349
        if (argv.length == 0) {
 
350
            printUsage();
 
351
            System.exit(1);
 
352
        }
 
353
        
 
354
        // variables
 
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;
 
367
        
 
368
        // process arguments
 
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.");
 
377
                    }
 
378
                    else {
 
379
                        schemaLanguage = argv[i];
 
380
                    }
 
381
                    continue;
 
382
                }
 
383
                if (option.equals("x")) {
 
384
                    if (++i == argv.length) {
 
385
                        System.err.println("error: Missing argument to -x option.");
 
386
                        continue;
 
387
                    }
 
388
                    String number = argv[i];
 
389
                    try {
 
390
                        int value = Integer.parseInt(number);
 
391
                        if (value < 1) {
 
392
                            System.err.println("error: Repetition must be at least 1.");
 
393
                            continue;
 
394
                        }
 
395
                        repetition = value;
 
396
                    }
 
397
                    catch (NumberFormatException e) {
 
398
                        System.err.println("error: invalid number ("+number+").");
 
399
                    }
 
400
                    continue;
 
401
                }
 
402
                if (arg.equals("-a")) {
 
403
                    // process -a: xpath expressions for schemas
 
404
                    if (schemas == null) {
 
405
                        schemas = new Vector();
 
406
                    }
 
407
                    while (i + 1 < argv.length - 1 && !(arg = argv[i + 1]).startsWith("-")) {
 
408
                        schemas.add(arg);
 
409
                        ++i;
 
410
                    }
 
411
                    continue;
 
412
                }
 
413
                if (arg.equals("-i")) {
 
414
                    // process -i: xpath expressions for instance documents
 
415
                    if (instances == null) {
 
416
                        instances = new Vector();
 
417
                    }
 
418
                    while (i + 1 < argv.length - 1 && !(arg = argv[i + 1]).startsWith("-")) {
 
419
                        instances.add(arg);
 
420
                        ++i;
 
421
                    }
 
422
                    continue;
 
423
                }
 
424
                if (arg.equals("-nm")) {
 
425
                    String prefix;
 
426
                    String uri;
 
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();
 
432
                        }
 
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);
 
438
                        }
 
439
                        prefixes.add(prefix);
 
440
                        i += 2;
 
441
                    }
 
442
                    continue;
 
443
                }
 
444
                if (option.equalsIgnoreCase("f")) {
 
445
                    schemaFullChecking = option.equals("f");
 
446
                    continue;
 
447
                }
 
448
                if (option.equalsIgnoreCase("hs")) {
 
449
                    honourAllSchemaLocations = option.equals("hs");
 
450
                    continue;
 
451
                }
 
452
                if (option.equalsIgnoreCase("va")) {
 
453
                    validateAnnotations = option.equals("va");
 
454
                    continue;
 
455
                }
 
456
                if (option.equalsIgnoreCase("ga")) {
 
457
                    generateSyntheticAnnotations = option.equals("ga");
 
458
                    continue;
 
459
                }
 
460
                if (option.equalsIgnoreCase("m")) {
 
461
                    memoryUsage = option.equals("m");
 
462
                    continue;
 
463
                }
 
464
                if (option.equals("h")) {
 
465
                    printUsage();
 
466
                    continue;
 
467
                }
 
468
                System.err.println("error: unknown option ("+option+").");
 
469
                continue;
 
470
            }
 
471
        }
 
472
        
 
473
        try {
 
474
            // Create new instance of inline schema validator.
 
475
            InlineSchemaValidator inlineSchemaValidator = new InlineSchemaValidator(prefixMappings, uriMappings);
 
476
            
 
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);
 
483
            
 
484
            // Create XPath factory for selecting schema and validation roots
 
485
            XPathFactory xpf = XPathFactory.newInstance();
 
486
            XPath xpath = xpf.newXPath();
 
487
            xpath.setNamespaceContext(inlineSchemaValidator);
 
488
            
 
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);
 
494
            }
 
495
            
 
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);
 
501
            }
 
502
 
 
503
            // Create SchemaFactory and configure
 
504
            SchemaFactory factory = SchemaFactory.newInstance(schemaLanguage);
 
505
            factory.setErrorHandler(inlineSchemaValidator);
 
506
            
 
507
            try {
 
508
                factory.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, schemaFullChecking);
 
509
            }
 
510
            catch (SAXNotRecognizedException e) {
 
511
                System.err.println("warning: SchemaFactory does not recognize feature ("+SCHEMA_FULL_CHECKING_FEATURE_ID+")");
 
512
            }
 
513
            catch (SAXNotSupportedException e) {
 
514
                System.err.println("warning: SchemaFactory does not support feature ("+SCHEMA_FULL_CHECKING_FEATURE_ID+")");
 
515
            }
 
516
            try {
 
517
                factory.setFeature(HONOUR_ALL_SCHEMA_LOCATIONS_ID, honourAllSchemaLocations);
 
518
            }
 
519
            catch (SAXNotRecognizedException e) {
 
520
                System.err.println("warning: SchemaFactory does not recognize feature ("+HONOUR_ALL_SCHEMA_LOCATIONS_ID+")");
 
521
            }
 
522
            catch (SAXNotSupportedException e) {
 
523
                System.err.println("warning: SchemaFactory does not support feature ("+HONOUR_ALL_SCHEMA_LOCATIONS_ID+")");
 
524
            }
 
525
            try {
 
526
                factory.setFeature(VALIDATE_ANNOTATIONS_ID, validateAnnotations);
 
527
            }
 
528
            catch (SAXNotRecognizedException e) {
 
529
                System.err.println("warning: SchemaFactory does not recognize feature ("+VALIDATE_ANNOTATIONS_ID+")");
 
530
            }
 
531
            catch (SAXNotSupportedException e) {
 
532
                System.err.println("warning: SchemaFactory does not support feature ("+VALIDATE_ANNOTATIONS_ID+")");
 
533
            }
 
534
            try {
 
535
                factory.setFeature(GENERATE_SYNTHETIC_ANNOTATIONS_ID, generateSyntheticAnnotations);
 
536
            }
 
537
            catch (SAXNotRecognizedException e) {
 
538
                System.err.println("warning: SchemaFactory does not recognize feature ("+GENERATE_SYNTHETIC_ANNOTATIONS_ID+")");
 
539
            }
 
540
            catch (SAXNotSupportedException e) {
 
541
                System.err.println("warning: SchemaFactory does not support feature ("+GENERATE_SYNTHETIC_ANNOTATIONS_ID+")");
 
542
            }
 
543
            
 
544
            // Build Schema from sources
 
545
            Schema schema;
 
546
            {
 
547
                DOMSource [] sources;
 
548
                int size = 0;
 
549
                for (int i = 0; i < schemaNodes.length; ++i) {
 
550
                    size += schemaNodes[i].getLength();
 
551
                }
 
552
                sources = new DOMSource[size];
 
553
                if (size == 0) {
 
554
                    schema = factory.newSchema();
 
555
                }
 
556
                else {
 
557
                    int count = 0;
 
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));
 
563
                        }
 
564
                    }
 
565
                    schema = factory.newSchema(sources);
 
566
                }
 
567
            }
 
568
            
 
569
            // Setup validator and input source.
 
570
            Validator validator = schema.newValidator();
 
571
            validator.setErrorHandler(inlineSchemaValidator);
 
572
            
 
573
            try {
 
574
                validator.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, schemaFullChecking);
 
575
            }
 
576
            catch (SAXNotRecognizedException e) {
 
577
                System.err.println("warning: Validator does not recognize feature ("+SCHEMA_FULL_CHECKING_FEATURE_ID+")");
 
578
            }
 
579
            catch (SAXNotSupportedException e) {
 
580
                System.err.println("warning: Validator does not support feature ("+SCHEMA_FULL_CHECKING_FEATURE_ID+")");
 
581
            }
 
582
            try {
 
583
                validator.setFeature(HONOUR_ALL_SCHEMA_LOCATIONS_ID, honourAllSchemaLocations);
 
584
            }
 
585
            catch (SAXNotRecognizedException e) {
 
586
                System.err.println("warning: Validator does not recognize feature ("+HONOUR_ALL_SCHEMA_LOCATIONS_ID+")");
 
587
            }
 
588
            catch (SAXNotSupportedException e) {
 
589
                System.err.println("warning: Validator does not support feature ("+HONOUR_ALL_SCHEMA_LOCATIONS_ID+")");
 
590
            }
 
591
            try {
 
592
                validator.setFeature(VALIDATE_ANNOTATIONS_ID, validateAnnotations);
 
593
            }
 
594
            catch (SAXNotRecognizedException e) {
 
595
                System.err.println("warning: Validator does not recognize feature ("+VALIDATE_ANNOTATIONS_ID+")");
 
596
            }
 
597
            catch (SAXNotSupportedException e) {
 
598
                System.err.println("warning: Validator does not support feature ("+VALIDATE_ANNOTATIONS_ID+")");
 
599
            }
 
600
            try {
 
601
                validator.setFeature(GENERATE_SYNTHETIC_ANNOTATIONS_ID, generateSyntheticAnnotations);
 
602
            }
 
603
            catch (SAXNotRecognizedException e) {
 
604
                System.err.println("warning: Validator does not recognize feature ("+GENERATE_SYNTHETIC_ANNOTATIONS_ID+")");
 
605
            }
 
606
            catch (SAXNotSupportedException e) {
 
607
                System.err.println("warning: Validator does not support feature ("+GENERATE_SYNTHETIC_ANNOTATIONS_ID+")");
 
608
            }
 
609
 
 
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);
 
618
                }
 
619
            }
 
620
        }
 
621
        catch (SAXParseException e) {
 
622
            // ignore
 
623
        }
 
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) {
 
629
                    e = nested;
 
630
                } 
 
631
            }
 
632
            e.printStackTrace(System.err);
 
633
        }
 
634
    } // main(String[])
 
635
    
 
636
    //
 
637
    // Private static methods
 
638
    //
 
639
    
 
640
    /** Prints the usage. */
 
641
    private static void printUsage() {
 
642
        
 
643
        System.err.println("usage: java jaxp.InlineSchemaValidator (options) uri ...");
 
644
        System.err.println();
 
645
        
 
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.");
 
662
        
 
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");
 
677
        
 
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.");
 
684
        
 
685
    } // printUsage()
 
686
 
 
687
} // class InlineSchemaValidator