KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > samples > validate > JARVDemo2


1 /*
2  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
3  *
4  * This software is open source.
5  * See the bottom of this file for the licence.
6  *
7  * $Id: JARVDemo2.java,v 1.4 2005/01/29 14:53:14 maartenc Exp $
8  */

9
10 package org.dom4j.samples.validate;
11
12 import org.dom4j.Document;
13 import org.dom4j.DocumentException;
14 import org.dom4j.io.SAXReader;
15 import org.iso_relax.verifier.Schema;
16 import org.iso_relax.verifier.Verifier;
17 import org.iso_relax.verifier.VerifierFactory;
18 import org.iso_relax.verifier.VerifierFilter;
19 import org.xml.sax.ErrorHandler JavaDoc;
20 import org.xml.sax.SAXParseException JavaDoc;
21
22 /**
23  * A sample program which validates an XML document as is it being parsed using
24  * the JARV API with Sun's MSV library implementation.
25  *
26  * @author <a HREF="mailto:jstrachan@apache.org">James Strachan </a>
27  * @version $Revision: 1.4 $
28  */

29 public class JARVDemo2 {
30
31     public static void main(String JavaDoc[] args) {
32         new JARVDemo2().run(args);
33     }
34
35     public void run(String JavaDoc[] args) {
36         try {
37             if (args.length < 2) {
38                 System.out.println("usage: <xmlDoc> <schemaDoc>");
39                 System.out
40                         .println("Which validates the given XML document against the given schema document");
41                 System.out
42                         .println("The schema can be XML Schema, RelaxNG, Relax or TREX");
43                 return;
44             }
45             String JavaDoc xmlFile = args[0];
46             String JavaDoc schema = args[1];
47
48             SAXReader reader = createSAXReader(schema);
49             Document document = reader.read(xmlFile);
50
51             System.out.println("Parsed document: " + xmlFile + " correctly.");
52         } catch (DocumentException e) {
53             System.out.println("Exception occurred: " + e);
54             Throwable JavaDoc nestedException = e.getNestedException();
55             if (nestedException != null) {
56                 System.out.println("NestedException: " + nestedException);
57                 nestedException.printStackTrace();
58             } else {
59                 e.printStackTrace();
60             }
61         } catch (Throwable JavaDoc t) {
62             System.out.println("Exception occurred: " + t);
63             t.printStackTrace();
64         }
65     }
66
67     /** Registers the Verifier with the SAXReader */
68     protected SAXReader createSAXReader(String JavaDoc schemaURI) throws Exception JavaDoc {
69
70         System.out.println("Loaded schema document: " + schemaURI);
71
72         // use autodetection of schemas
73
VerifierFactory factory = new com.sun.msv.verifier.jarv.TheFactoryImpl();
74         Schema schema = factory.compileSchema(schemaURI);
75
76         Verifier verifier = schema.newVerifier();
77         verifier.setErrorHandler(new ErrorHandler JavaDoc() {
78             public void error(SAXParseException JavaDoc e) {
79                 System.out.println("ERROR: " + e);
80             }
81
82             public void fatalError(SAXParseException JavaDoc e) {
83                 System.out.println("FATAL: " + e);
84             }
85
86             public void warning(SAXParseException JavaDoc e) {
87                 System.out.println("WARNING: " + e);
88             }
89         });
90
91         // now install the verifying filter
92
VerifierFilter filter = verifier.getVerifierFilter();
93         SAXReader reader = new SAXReader();
94         reader.setXMLFilter(filter);
95         return reader;
96     }
97 }
98
99 /*
100  * Redistribution and use of this software and associated documentation
101  * ("Software"), with or without modification, are permitted provided that the
102  * following conditions are met:
103  *
104  * 1. Redistributions of source code must retain copyright statements and
105  * notices. Redistributions must also contain a copy of this document.
106  *
107  * 2. Redistributions in binary form must reproduce the above copyright notice,
108  * this list of conditions and the following disclaimer in the documentation
109  * and/or other materials provided with the distribution.
110  *
111  * 3. The name "DOM4J" must not be used to endorse or promote products derived
112  * from this Software without prior written permission of MetaStuff, Ltd. For
113  * written permission, please contact dom4j-info@metastuff.com.
114  *
115  * 4. Products derived from this Software may not be called "DOM4J" nor may
116  * "DOM4J" appear in their names without prior written permission of MetaStuff,
117  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
118  *
119  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
120  *
121  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
122  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
123  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
124  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
125  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
126  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
127  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
128  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
129  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
130  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
131  * POSSIBILITY OF SUCH DAMAGE.
132  *
133  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
134  *
135  * $Id: JARVDemo2.java,v 1.4 2005/01/29 14:53:14 maartenc Exp $
136  */

137
Popular Tags