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.LSResourceResolver;
33 import org.xml.sax.SAXException;
36 * Represents an XML Schema.
38 * @author Erik Brakkee
40 public class XMLSchema implements XMLProcessor {
42 private Schema schema;
43 private Validator validator;
45 public XMLSchema(URI aUri) throws XMLException {
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);
55 public XMLSchema(String aSystemId, InputStream aIs, LSResourceResolver aResolver) throws XMLException {
56 initialize(aSystemId, aIs, aResolver);
59 private void initialize(String aSystemId, InputStream aIs, LSResourceResolver aResolver)
62 SchemaFactory factory = SchemaFactory
63 .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
64 if (aResolver != null) {
65 factory.setResourceResolver(aResolver);
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);
76 public static XMLSchema schema(URI aUri) throws XMLException {
77 return new XMLSchema(aUri);
80 public static XMLSchema schema(String aSystemId, InputStream aIs, LSResourceResolver aResolver) throws XMLException {
81 return new XMLSchema(aSystemId, aIs, aResolver);
84 private void validateImpl(DOMSource aDoc) throws XMLException {
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);
94 public void validate(XMLDocument aDocument) throws XMLException {
95 validateImpl(aDocument.getDOMSource());
98 public void validate(DOMSource aDocument) throws XMLException {
99 validateImpl(aDocument);
103 public DOMSource process(DOMSource aDocument) throws XMLException {