KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SchemaValidatorExample


1
2 import org.w3c.dom.ls.LSInput JavaDoc;
3 import org.w3c.dom.ls.LSResourceResolver JavaDoc;
4 import org.xml.sax.ErrorHandler JavaDoc;
5 import org.xml.sax.SAXException JavaDoc;
6 import org.xml.sax.SAXParseException JavaDoc;
7
8 import javax.xml.transform.stream.StreamSource JavaDoc;
9 import javax.xml.validation.Schema JavaDoc;
10 import javax.xml.validation.SchemaFactory JavaDoc;
11 import javax.xml.validation.Validator JavaDoc;
12 import java.io.File JavaDoc;
13
14 /**
15  * SchemaValidatorExample demonstrates the use of jaxp validation apis.
16  *
17  * This version was modified by Michael Kay from the version (named SchemaValidator)
18  * issued with the JAXP 1.3 distribution. It has been changed to set system properties
19  * that force loading of Saxon, to improve the error reporting, and to improve the
20  * comments.
21  *
22  * The original file contained no explicit terms and conditions or copyright statement,
23  * but it should be assumed that it is subject to the usual Apache rules.
24  */

25
26 public class SchemaValidatorExample {
27
28     /**
29      * A custom SAX error handler
30      */

31
32     protected static class Handler implements ErrorHandler JavaDoc {
33
34         /**
35          * Report a non-fatal error
36          * @param ex the error condition
37          */

38         public void error(SAXParseException JavaDoc ex) {
39             System.err.println("At line " + ex.getLineNumber() + " of " + ex.getSystemId() + ":");
40             System.err.println(ex.getMessage());
41         }
42
43         /**
44          * Report a fatal error
45          * @param ex the error condition
46          */

47
48         public void fatalError(SAXParseException JavaDoc ex) {
49             System.err.println("At line " + ex.getLineNumber() + " of " + ex.getSystemId() + ":");
50             System.err.println(ex.getMessage());
51         }
52
53         /**
54          * Report a warning
55          * @param ex the warning condition
56          */

57         public void warning(org.xml.sax.SAXParseException JavaDoc ex) {
58             System.err.println("At line " + ex.getLineNumber() + " of " + ex.getSystemId() + ":");
59             System.err.println(ex.getMessage());
60         }
61
62     }
63
64     /**
65      * Inner class to implement a resource resolver. This version always returns null, which
66      * has the same effect as not supplying a resource resolver at all. The LSResourceResolver
67      * is part of the DOM Level 3 load/save module.
68      */

69
70     protected static class Resolver implements LSResourceResolver JavaDoc{
71
72         /**
73          * Resolve a reference to a resource
74          * @param type The type of resource, for example a schema, source XML document, or query
75          * @param namespace The target namespace (in the case of a schema document)
76          * @param publicId The public ID
77          * @param systemId The system identifier (as written, possibly a relative URI)
78          * @param baseURI The base URI against which the system identifier should be resolved
79          * @return an LSInput object typically containing the character stream or byte stream identified
80          * by the supplied parameters; or null if the reference cannot be resolved or if the resolver chooses
81          * not to resolve it.
82          */

83
84         public LSInput JavaDoc resolveResource(String JavaDoc type, String JavaDoc namespace, String JavaDoc publicId, String JavaDoc systemId, String JavaDoc baseURI) {
85             return null;
86         }
87
88     }
89
90     /**
91      * Main entry point. Expects two arguments: the schema document, and the source document.
92      * @param args
93      */

94     public static void main(String JavaDoc [] args) {
95         try {
96             if(args.length != 2){
97                 printUsage();
98                 return;
99             }
100
101             Handler JavaDoc handler = new Handler JavaDoc();
102
103             SchemaFactory JavaDoc schemaFactory;
104
105             // Set a system property to force selection of the Saxon SchemaFactory implementation
106
// This is commented out because it shouldn't be necessary if Saxon-SA is on the classpath;
107
// but in the event of configuration problems, try reinstating it.
108

109 // System.setProperty("javax.xml.validation.SchemaFactory:http://www.w3.org/2001/XMLSchema",
110
// "com.saxonica.schema.SchemaFactoryImpl");
111

112             schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
113             System.err.println("Loaded schema validation provider " + schemaFactory.getClass().getName());
114
115             schemaFactory.setErrorHandler(handler);
116             //create a grammar object.
117
Schema JavaDoc schemaGrammar = schemaFactory.newSchema(new File JavaDoc(args[0]));
118
119             System.err.println("Created Grammar object for schema : "+args[0]);
120
121             Resolver JavaDoc resolver = new Resolver JavaDoc();
122             //create a validator to validate against grammar sch.
123
Validator JavaDoc schemaValidator = schemaGrammar.newValidator();
124             schemaValidator.setResourceResolver(resolver);
125             schemaValidator.setErrorHandler(handler);
126
127             System.err.println("Validating "+args[1] +" against grammar "+args[0]);
128             //validate xml instance against the grammar.
129
schemaValidator.validate(new StreamSource JavaDoc(args[1]));
130
131             System.err.println("Validation successful");
132         } catch (SAXException JavaDoc saxe) {
133             exit(1, "Error: " + saxe.getMessage());
134         } catch (Exception JavaDoc e) {
135             e.printStackTrace();
136             exit(2, "Fatal Error: " + e);
137         }
138     }
139
140     /**
141      *
142      * @param errCode
143      * @param msg
144      */

145     public static void exit(int errCode, String JavaDoc msg) {
146         System.err.println(msg);
147         System.exit(errCode);
148     }
149
150     public static void printUsage(){
151         System.err.println("Usage : SchemaValidatorExample <schemaFile> <xmlFile>");
152     }
153 }
154
Popular Tags