2 * Copyright 2005-2011 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.xml;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.net.MalformedURLException;
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;
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;
37 * Represents an XML Schema.
39 * @author Erik Brakkee
41 public class XMLSchema implements XMLProcessor {
43 private Schema schema;
44 private Validator validator;
46 public XMLSchema(URI aUri) throws XMLException {
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);
56 public XMLSchema(String aSystemId, LSResourceResolver aResolver)
58 LSInput input = aResolver.resolveResource(null, null, null, aSystemId,
61 throw new XMLException("Schema classpath resource '" + aSystemId +
64 InputStream is = input.getByteStream();
65 initialize(aSystemId, is, aResolver);
68 private void initialize(String aSystemId, InputStream aIs,
69 LSResourceResolver aResolver) throws XMLException {
71 SchemaFactory factory = SchemaFactory
72 .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
73 if (aResolver != null) {
74 factory.setResourceResolver(aResolver);
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);
85 public static XMLSchema schema(URI aUri) throws XMLException {
86 return new XMLSchema(aUri);
89 public static XMLSchema schema(String aSystemId,
90 LSResourceResolver aResolver) throws XMLException {
91 return new XMLSchema(aSystemId, aResolver);
94 private void validateImpl(DOMSource aDoc) throws XMLException {
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);
104 public void validate(XMLDocument aDocument) throws XMLException {
105 validateImpl(aDocument.getDOMSource());
108 public void validate(DOMSource aDocument) throws XMLException {
109 validateImpl(aDocument);
113 public DOMSource process(DOMSource aDocument) throws XMLException {