KickJava   Java API By Example, From Geeks To Geeks.

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


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: XercesDemo.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.samples.AbstractDemo;
13
14 import org.dom4j.Document;
15 import org.dom4j.io.OutputFormat;
16 import org.dom4j.io.SAXReader;
17 import org.dom4j.io.XMLWriter;
18 import org.dom4j.util.XMLErrorHandler;
19
20 /**
21  * Validates a document using Xerces and an XML Schema.
22  *
23  * @author <a HREF="mailto:jstrachan@apache.org">James Strachan </a>
24  * @version $Revision: 1.4 $
25  */

26 public class XercesDemo extends AbstractDemo {
27
28     public static void main(String JavaDoc[] args) {
29         run(new XercesDemo(), args);
30     }
31
32     public XercesDemo() {
33     }
34
35     public void run(String JavaDoc[] args) throws Exception JavaDoc {
36         if (args.length < 1) {
37             printUsage("no XML document URL specified");
38             return;
39         }
40
41         parse(args[0]);
42     }
43
44     protected Document parse(String JavaDoc uri) throws Exception JavaDoc {
45         SAXReader reader = new SAXReader();
46
47         reader.setValidation(true);
48
49         // specify the schema to use
50
reader
51                 .setProperty(
52                         "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
53                         "personal.xsd");
54
55         // add an error handler which turns any errors into XML
56
XMLErrorHandler errorHandler = new XMLErrorHandler();
57         reader.setErrorHandler(errorHandler);
58
59         // now lets parse the document
60
Document document = reader.read(uri);
61
62         // now lets output the errors as XML
63
XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint());
64         writer.write(errorHandler.getErrors());
65
66         return document;
67     }
68 }
69
70 /*
71  * Redistribution and use of this software and associated documentation
72  * ("Software"), with or without modification, are permitted provided that the
73  * following conditions are met:
74  *
75  * 1. Redistributions of source code must retain copyright statements and
76  * notices. Redistributions must also contain a copy of this document.
77  *
78  * 2. Redistributions in binary form must reproduce the above copyright notice,
79  * this list of conditions and the following disclaimer in the documentation
80  * and/or other materials provided with the distribution.
81  *
82  * 3. The name "DOM4J" must not be used to endorse or promote products derived
83  * from this Software without prior written permission of MetaStuff, Ltd. For
84  * written permission, please contact dom4j-info@metastuff.com.
85  *
86  * 4. Products derived from this Software may not be called "DOM4J" nor may
87  * "DOM4J" appear in their names without prior written permission of MetaStuff,
88  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
89  *
90  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
91  *
92  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
93  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
95  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
96  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
97  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
98  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
99  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
100  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
102  * POSSIBILITY OF SUCH DAMAGE.
103  *
104  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
105  *
106  * $Id: XercesDemo.java,v 1.4 2005/01/29 14:53:14 maartenc Exp $
107  */

108
Popular Tags