KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > jpdl > xml > SchemaValidationHelper


1 package org.jbpm.jpdl.xml;
2
3 import java.io.IOException JavaDoc;
4 import java.io.Reader JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.List JavaDoc;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.dom4j.Document;
11 import org.dom4j.DocumentException;
12 import org.dom4j.io.SAXReader;
13 import org.xml.sax.EntityResolver JavaDoc;
14 import org.xml.sax.InputSource JavaDoc;
15 import org.xml.sax.SAXException JavaDoc;
16 import org.xml.sax.SAXParseException JavaDoc;
17 import org.xml.sax.helpers.DefaultHandler JavaDoc;
18
19 /**
20  * Validate an XML document using JAXP techniques
21  * and an XML Schema. This helper class wraps the processing of a schema
22  * to aid in schema validation throughout
23  * the product.
24  *
25  * @author Jim Rigsbee
26  *
27  */

28 public class SchemaValidationHelper
29 {
30     private static final String JavaDoc JAXP_SCHEMA_LANGUAGE =
31                     "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
32     private static final String JavaDoc W3C_XML_SCHEMA =
33                     "http://www.w3.org/2001/XMLSchema";
34   
35   /*
36     private static final String JAXP_SCHEMA_SOURCE =
37                     "http://java.sun.com/xml/jaxp/properties/schemaSource";
38   */

39     
40     private Reader JavaDoc reader;
41     private String JavaDoc schemaName;
42     private String JavaDoc subject;
43     private List JavaDoc errors = new ArrayList JavaDoc();
44     private Document document;
45     private boolean valid = true;
46     
47     /**
48      * Constructs the schema validation helper and prepares it for reading
49      * the XML document.
50      *
51      * @param reader input reader containing the contents of the XML document
52      * @param schemaName physical file name of the XML schema to be used for validation
53      * @param subject descriptive name of file we are parsing to put in error messages
54      */

55     public SchemaValidationHelper(Reader JavaDoc reader, String JavaDoc schemaName, String JavaDoc subject)
56     {
57         this.reader = reader;
58         this.schemaName = schemaName;
59         this.subject = subject;
60     }
61     
62     /**
63      * Performs the schema validation on the document contents in the reader. Full schema
64      * checking is performed using the 2001 XML schema for schemas from W3C. A document
65      * will be validated only if it is given a namespace in its root element.
66      *
67      * @return true - if the document is valid, false - if not valid, use {@link #getProblems()}
68      * to retrieve a list of {@link Problem} objects detailing the schema validation errors.
69      */

70     public boolean isValid()
71     {
72         try
73         {
74                         
75             SAXReader saxReader = new SAXReader( );
76             
77             // Set custom handler and entity resolver (schema lookup)
78
saxReader.setErrorHandler( new Handler JavaDoc() );
79             saxReader.setEntityResolver( new Resolver JavaDoc() );
80                     
81             try
82             {
83                 // Use XML Schema validation
84
saxReader.setFeature("http://apache.org/xml/features/validation/schema", true);
85                 // Do full type checking
86
saxReader.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
87                 // Only do schema validation if a schema is specified as a namespace
88
saxReader.setFeature("http://apache.org/xml/features/validation/dynamic", true);
89                 // Set the language level for the schema
90
saxReader.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
91                 // Force schema location to invoke entity resolver
92
saxReader.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation", "http://jbpm.org/3/jpdl jpdl-3.0.xsd");
93             }
94             catch(SAXException JavaDoc se)
95             {
96                 log.warn("Unable to validate using schema. Make sure Xerces is first in your class path.");
97             }
98             
99             document = saxReader.read( reader );
100             
101         }
102         catch(DocumentException de)
103         {
104             log.error("Parsing problems", de);
105         }
106         
107         return valid;
108     }
109     
110     /**
111      *
112      * @return a {@link java.util.List} of {@link Problem} objects detailing any errors
113      * found during schema validation
114      */

115     public List JavaDoc getProblems()
116     {
117         return errors;
118     }
119     
120     /**
121      * Accessor for the DOM model of the document.
122      *
123      * @return {@link Document} representing the DOM model of the validated XML document
124      */

125     public Document getDocument()
126     {
127         return document;
128     }
129     
130     /**
131      * Specialized schema validation handler which creates {@link Problem} objects
132      * for each error encountered.
133      *
134      */

135     class Handler extends DefaultHandler JavaDoc
136     {
137         public void warning(SAXParseException JavaDoc pe)
138         {
139             errors.add(
140                     new Problem( Problem.LEVEL_WARNING,
141                      subject + " line " + pe.getLineNumber() +
142                      ": " + pe.getMessage() ) );
143         }
144         
145         public void error(SAXParseException JavaDoc pe)
146         {
147             errors.add(
148                     new Problem( Problem.LEVEL_ERROR,
149                      subject + " line " + pe.getLineNumber() +
150                      ": " + pe.getMessage() ) );
151             valid = false;
152         }
153         
154         public void fatalError(SAXParseException JavaDoc pe)
155         {
156             errors.add(
157                     new Problem( Problem.LEVEL_FATAL,
158                      subject + " line " + pe.getLineNumber() +
159                      ": " + pe.getMessage() ) );
160             valid = false;
161         }
162     }
163     
164     /**
165      * Specialized entity resolver to load the schema from the same
166      * path as the validation class.
167      *
168      */

169     class Resolver implements EntityResolver JavaDoc
170     {
171         public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc SystemId)
172                 throws SAXException JavaDoc, IOException JavaDoc
173         {
174             return new InputSource JavaDoc( this.getClass()
175                                         .getResourceAsStream( schemaName )
176                                   );
177         }
178     
179     }
180     
181     private static final Log log = LogFactory.getLog( SchemaValidationHelper.class );
182 }
183
Popular Tags