View Javadoc

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  
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.springframework.beans.factory.InitializingBean;
28  import org.springframework.core.io.Resource;
29  import org.springframework.util.Assert;
30  import org.springframework.xml.namespace.QNameUtils;
31  import org.springframework.xml.sax.SaxUtils;
32  import org.springframework.xml.validation.XmlValidator;
33  import org.springframework.xml.validation.XmlValidatorFactory;
34  
35  import org.w3c.dom.Document;
36  import org.w3c.dom.Element;
37  import org.xml.sax.SAXException;
38  
39  /**
40   * The default [email protected] XsdSchema} implementation.
41   * <p/>
42   * Allows a XSD to be set by the [email protected] #setXsd(Resource)}, or directly in the [email protected] #SimpleXsdSchema(Resource)
43   * constructor}.
44   *
45   * @author Mark LaFond
46   * @author Arjen Poutsma
47   * @since 1.5.0
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       * Create a new instance of the [email protected] SimpleXsdSchema} class.
67       * <p/>
68       * A subsequent call to the [email protected] #setXsd(Resource)} method is required.
69       */
70      public SimpleXsdSchema() {
71      }
72  
73      /**
74       * Create a new instance of the  [email protected] SimpleXsdSchema} class with the specified resource.
75       *
76       * @param xsdResource the XSD resource; must not be <code>null</code>
77       * @throws IllegalArgumentException if the supplied <code>xsdResource</code> is <code>null</code>
78       */
79      public SimpleXsdSchema(Resource xsdResource) {
80          Assert.notNull(xsdResource, "xsdResource must not be null");
81          this.xsdResource = xsdResource;
82      }
83  
84      /**
85       * Set the XSD resource to be exposed by calls to this instances' [email protected] #getSource()} method.
86       *
87       * @param xsdResource the XSD resource
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 exist");
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         StringBuilder builder = new StringBuilder("SimpleXsdSchema");
125         builder.append('{');
126         builder.append(getTargetNamespace());
127         builder.append('}');
128         return builder.toString();
129     }
130 
131 }