956de70116556a3559a6855e19e2011d122edfe9
[utils] / support / general / src / main / java / org / wamblee / xml / XMLSchema.java
1 /*
2  * Copyright 2005-2011 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.xml;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.net.MalformedURLException;
21 import java.net.URI;
22
23 import javax.xml.XMLConstants;
24 import javax.xml.transform.Source;
25 import javax.xml.transform.dom.DOMSource;
26 import javax.xml.transform.stream.StreamSource;
27 import javax.xml.validation.Schema;
28 import javax.xml.validation.SchemaFactory;
29 import javax.xml.validation.Validator;
30
31 import org.w3c.dom.Document;
32 import org.w3c.dom.ls.LSResourceResolver;
33 import org.xml.sax.SAXException;
34
35 /**
36  * Represents an XML Schema. 
37  * 
38  * @author Erik Brakkee
39  */
40 public class XMLSchema implements XMLProcessor {
41
42     private Schema schema;
43     private Validator validator;
44
45     public XMLSchema(URI aUri) throws XMLException {
46         try {
47             initialize(aUri.toString(), aUri.toURL().openStream(), null);
48         } catch (MalformedURLException e) {
49             throw new XMLException("Could not read '" + aUri + "'", e);
50         } catch (IOException e) {
51             throw new XMLException("Could not read '" + aUri + "'", e);
52         }
53     }
54
55     public XMLSchema(String aSystemId, InputStream aIs, LSResourceResolver aResolver) throws XMLException {
56         initialize(aSystemId, aIs, aResolver);
57     }
58
59     private void initialize(String aSystemId, InputStream aIs, LSResourceResolver aResolver)
60         throws XMLException {
61         try {
62             SchemaFactory factory = SchemaFactory
63                 .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
64             if (aResolver != null) {
65                 factory.setResourceResolver(aResolver);
66             }
67             Source source = new StreamSource(aIs);
68             source.setSystemId(aSystemId);
69             schema = factory.newSchema(source);
70             validator = schema.newValidator();
71         } catch (SAXException e) {
72             throw new XMLException(e.getMessage(), e);
73         }
74     }
75
76     public static XMLSchema schema(URI aUri) throws XMLException {
77         return new XMLSchema(aUri);
78     }
79
80     public static XMLSchema schema(String aSystemId, InputStream aIs, LSResourceResolver aResolver) throws XMLException {
81         return new XMLSchema(aSystemId, aIs, aResolver);
82     }
83     
84     private void validateImpl(DOMSource aDoc) throws XMLException { 
85         try {
86             validator.validate(aDoc);
87         } catch (SAXException e) {
88             throw new XMLException(e.getMessage(), e);
89         } catch (IOException e) {
90             throw new XMLException(e.getMessage(), e);
91         }
92     }
93
94     public void validate(XMLDocument aDocument) throws XMLException {
95         validateImpl(aDocument.getDOMSource());
96     }
97
98     public void validate(DOMSource aDocument) throws XMLException {
99         validateImpl(aDocument);
100     }
101
102     @Override
103     public DOMSource process(DOMSource aDocument) throws XMLException {
104         validate(aDocument); 
105         return aDocument;
106     }
107
108 }