1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.xml.xsd;
18
19 import java.io.IOException;
20 import javax.xml.namespace.QName;
21 import javax.xml.parsers.DocumentBuilder;
22 import javax.xml.parsers.DocumentBuilderFactory;
23 import javax.xml.parsers.ParserConfigurationException;
24 import javax.xml.transform.Source;
25 import javax.xml.transform.dom.DOMSource;
26
27 import org.w3c.dom.Document;
28 import org.w3c.dom.Element;
29 import org.xml.sax.SAXException;
30
31 import org.springframework.beans.factory.InitializingBean;
32 import org.springframework.core.io.Resource;
33 import org.springframework.util.Assert;
34 import org.springframework.xml.namespace.QNameUtils;
35 import org.springframework.xml.sax.SaxUtils;
36 import org.springframework.xml.validation.XmlValidator;
37 import org.springframework.xml.validation.XmlValidatorFactory;
38
39
40
41
42
43
44
45
46
47
48
49 public class SimpleXsdSchema implements XsdSchema, InitializingBean {
50
51 private static DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
52
53 private static final String SCHEMA_NAMESPACE = "http://www.w3.org/2001/XMLSchema";
54
55 private static final QName SCHEMA_NAME = QNameUtils.createQName(SCHEMA_NAMESPACE, "schema", "xsd");
56
57 private Resource xsdResource;
58
59 private Element schemaElement;
60
61 static {
62 documentBuilderFactory.setNamespaceAware(true);
63 }
64
65
66
67
68
69
70 public SimpleXsdSchema() {
71 }
72
73
74
75
76
77
78
79 public SimpleXsdSchema(Resource xsdResource) {
80 Assert.notNull(xsdResource, "xsdResource must not be null");
81 this.xsdResource = xsdResource;
82 }
83
84
85
86
87
88
89 public void setXsd(Resource xsdResource) {
90 this.xsdResource = xsdResource;
91 }
92
93 public String getTargetNamespace() {
94 return schemaElement.getAttribute("targetNamespace");
95 }
96
97 public Source getSource() {
98 return new DOMSource(schemaElement);
99 }
100
101 public XmlValidator createValidator() throws IOException {
102 return XmlValidatorFactory.createValidator(xsdResource, XmlValidatorFactory.SCHEMA_W3C_XML);
103 }
104
105 public void afterPropertiesSet() throws ParserConfigurationException, IOException, SAXException {
106 Assert.notNull(xsdResource, "'xsd' is required");
107 Assert.isTrue(this.xsdResource.exists(), "xsd '" + this.xsdResource + "' does not exit");
108 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
109 loadSchema(documentBuilder);
110 }
111
112 private void loadSchema(DocumentBuilder documentBuilder) throws SAXException, IOException {
113 Document schemaDocument = documentBuilder.parse(SaxUtils.createInputSource(xsdResource));
114 schemaElement = schemaDocument.getDocumentElement();
115 Assert.isTrue(SCHEMA_NAME.getLocalPart().equals(schemaElement.getLocalName()),
116 xsdResource + " has invalid root element : [" + schemaElement.getLocalName() + "] instead of [schema]");
117 Assert.isTrue(SCHEMA_NAME.getNamespaceURI().equals(schemaElement.getNamespaceURI()), xsdResource +
118 " has invalid root element: [" + schemaElement.getNamespaceURI() + "] instead of [" +
119 SCHEMA_NAME.getNamespaceURI() + "]");
120 Assert.hasText(getTargetNamespace(), xsdResource + " has no targetNamespace");
121 }
122
123 public String toString() {
124 StringBuffer buffer = new StringBuffer("SimpleXsdSchema");
125 buffer.append('{');
126 buffer.append(getTargetNamespace());
127 buffer.append('}');
128 return buffer.toString();
129 }
130
131 }