KickJava   Java API By Example, From Geeks To Geeks.

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


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: SAXValidatorDemo.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.DocumentException;
16 import org.dom4j.DocumentType;
17 import org.dom4j.Element;
18 import org.dom4j.io.OutputFormat;
19 import org.dom4j.io.SAXReader;
20 import org.dom4j.io.SAXValidator;
21 import org.dom4j.io.XMLWriter;
22 import org.dom4j.util.XMLErrorHandler;
23 import org.xml.sax.SAXException JavaDoc;
24
25 /**
26  * A sample program demonstrating the use of validation using SAXValidator
27  *
28  * @author <a HREF="mailto:jstrachan@apache.org">James Strachan </a>
29  * @version $Revision: 1.4 $
30  */

31 public class SAXValidatorDemo extends AbstractDemo {
32
33     public static void main(String JavaDoc[] args) {
34         run(new SAXValidatorDemo(), args);
35     }
36
37     public SAXValidatorDemo() {
38     }
39
40     public void run(String JavaDoc[] args) throws Exception JavaDoc {
41         if (args.length < 1) {
42             printUsage("<xmlFileNameOrURL> <onParse>");
43         }
44
45         String JavaDoc fileName = args[0];
46         boolean validateOnParse = false;
47         if (args.length > 1) {
48             String JavaDoc boolText = args[1];
49             if (boolText.equalsIgnoreCase("true")) {
50                 validateOnParse = true;
51             }
52         }
53
54         validate(fileName, validateOnParse);
55     }
56
57     protected void validate(String JavaDoc url, boolean validateOnParse)
58             throws Exception JavaDoc {
59         println("Parsing: " + url + " with validation mode: " + validateOnParse);
60
61         XMLErrorHandler errorHandler = new XMLErrorHandler();
62
63         if (validateOnParse) {
64             // validate as we parse
65
SAXReader reader = new SAXReader(true);
66             reader.setErrorHandler(errorHandler);
67
68             try {
69                 Document document = reader.read(url);
70                 println("Document: " + url + " is valid!");
71             } catch (DocumentException e) {
72                 println("Document: " + url + " is not valid");
73                 println("Exception: " + e);
74             }
75         } else {
76             // parse without validating, then do that later
77
SAXReader reader = new SAXReader();
78             Document document = reader.read(url);
79
80             println("Document URI: " + document.getName());
81
82             // now lets set a doc type if one isn't set
83
DocumentType docType = document.getDocType();
84             if (docType == null) {
85                 println("Adding an NITF doc type");
86                 document.addDocType("nitf", null, "nitf.dtd");
87             }
88
89             // now lets validate
90
try {
91                 SAXValidator validator = new SAXValidator();
92                 validator.setErrorHandler(errorHandler);
93                 validator.validate(document);
94
95                 println("Document: " + url + " is valid!");
96             } catch (SAXException JavaDoc e) {
97                 println("Document: " + url + " is not valid");
98                 println("Exception: " + e);
99             }
100         }
101
102         // now lets output any errors as XML
103
Element errors = errorHandler.getErrors();
104         if (errors.hasContent()) {
105             XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint());
106             writer.write(errors);
107         }
108     }
109 }
110
111 /*
112  * Redistribution and use of this software and associated documentation
113  * ("Software"), with or without modification, are permitted provided that the
114  * following conditions are met:
115  *
116  * 1. Redistributions of source code must retain copyright statements and
117  * notices. Redistributions must also contain a copy of this document.
118  *
119  * 2. Redistributions in binary form must reproduce the above copyright notice,
120  * this list of conditions and the following disclaimer in the documentation
121  * and/or other materials provided with the distribution.
122  *
123  * 3. The name "DOM4J" must not be used to endorse or promote products derived
124  * from this Software without prior written permission of MetaStuff, Ltd. For
125  * written permission, please contact dom4j-info@metastuff.com.
126  *
127  * 4. Products derived from this Software may not be called "DOM4J" nor may
128  * "DOM4J" appear in their names without prior written permission of MetaStuff,
129  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
130  *
131  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
132  *
133  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
134  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
135  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
136  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
137  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
138  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
139  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
140  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
141  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
142  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
143  * POSSIBILITY OF SUCH DAMAGE.
144  *
145  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
146  *
147  * $Id: SAXValidatorDemo.java,v 1.4 2005/01/29 14:53:14 maartenc Exp $
148  */

149
Popular Tags