aligned schema API with that of XSL transformation.
[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.LSInput;
33 import org.w3c.dom.ls.LSResourceResolver;
34 import org.xml.sax.SAXException;
35
36 /**
37  * Represents an XML Schema.
38  * 
39  * @author Erik Brakkee
40  */
41 public class XMLSchema implements XMLProcessor {
42
43     private Schema schema;
44     private Validator validator;
45
46     public XMLSchema(URI aUri) throws XMLException {
47         try {
48             initialize(aUri.toString(), aUri.toURL().openStream(), null);
49         } catch (MalformedURLException e) {
50             throw new XMLException("Could not read '" + aUri + "'", e);
51         } catch (IOException e) {
52             throw new XMLException("Could not read '" + aUri + "'", e);
53         }
54     }
55
56     public XMLSchema(String aSystemId, LSResourceResolver aResolver)
57         throws XMLException {
58         LSInput input = aResolver.resolveResource(null, null, null, aSystemId,
59             null);
60         if (input == null) {
61             throw new XMLException("Schema classpath resource '" + aSystemId +
62                 "' not found");
63         }
64         InputStream is = input.getByteStream();
65         initialize(aSystemId, is, aResolver);
66     }
67
68     private void initialize(String aSystemId, InputStream aIs,
69         LSResourceResolver aResolver) throws XMLException {
70         try {
71             SchemaFactory factory = SchemaFactory
72                 .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
73             if (aResolver != null) {
74                 factory.setResourceResolver(aResolver);
75             }
76             Source source = new StreamSource(aIs);
77             source.setSystemId(aSystemId);
78             schema = factory.newSchema(source);
79             validator = schema.newValidator();
80         } catch (SAXException e) {
81             throw new XMLException(e.getMessage(), e);
82         }
83     }
84
85     public static XMLSchema schema(URI aUri) throws XMLException {
86         return new XMLSchema(aUri);
87     }
88
89     public static XMLSchema schema(String aSystemId,
90         LSResourceResolver aResolver) throws XMLException {
91         return new XMLSchema(aSystemId, aResolver);
92     }
93
94     private void validateImpl(DOMSource aDoc) throws XMLException {
95         try {
96             validator.validate(aDoc);
97         } catch (SAXException e) {
98             throw new XMLException(e.getMessage(), e);
99         } catch (IOException e) {
100             throw new XMLException(e.getMessage(), e);
101         }
102     }
103
104     public void validate(XMLDocument aDocument) throws XMLException {
105         validateImpl(aDocument.getDOMSource());
106     }
107
108     public void validate(DOMSource aDocument) throws XMLException {
109         validateImpl(aDocument);
110     }
111
112     @Override
113     public DOMSource process(DOMSource aDocument) throws XMLException {
114         validate(aDocument);
115         return aDocument;
116     }
117
118 }