KickJava   Java API By Example, From Geeks To Geeks.

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


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: JARVDemo.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.dom4j.io.SAXWriter;
16 import org.iso_relax.verifier.Schema;
17 import org.iso_relax.verifier.Verifier;
18 import org.iso_relax.verifier.VerifierFactory;
19 import org.iso_relax.verifier.VerifierHandler;
20 import org.xml.sax.ErrorHandler JavaDoc;
21 import org.xml.sax.SAXParseException JavaDoc;
22
23 /**
24  * A sample program which validates an already existing dom4j Document using the
25  * JARV API using Sun's MSV library implementation.
26  *
27  * @author <a HREF="mailto:jstrachan@apache.org">James Strachan </a>
28  * @version $Revision: 1.4 $
29  */

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

138
Popular Tags