View Javadoc

1   /*
2    * Copyright 2006 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.validation;
18  
19  import java.io.IOException;
20  import javax.xml.transform.Source;
21  import javax.xml.validation.Schema;
22  import javax.xml.validation.SchemaFactory;
23  
24  import org.springframework.core.io.Resource;
25  import org.springframework.util.Assert;
26  import org.springframework.xml.transform.ResourceSource;
27  import org.xml.sax.SAXException;
28  import org.xml.sax.XMLReader;
29  import org.xml.sax.helpers.XMLReaderFactory;
30  
31  /**
32   * Convenient utility methods for loading of {@link Schema} objects, performing standard handling of input streams.
33   *
34   * @author Arjen Poutsma
35   * @since 1.0.0
36   */
37  public abstract class SchemaLoaderUtils {
38  
39      /**
40       * Load schema from the given resource.
41       *
42       * @param resource       the resource to load from
43       * @param schemaLanguage the language of the schema. Can be <code>XMLConstants.W3C_XML_SCHEMA_NS_URI</code> or
44       *                       <code>XMLConstants.RELAXNG_NS_URI</code>.
45       * @throws IOException  if loading failed
46       * @throws SAXException if loading failed
47       * @see javax.xml.XMLConstants#W3C_XML_SCHEMA_NS_URI
48       * @see javax.xml.XMLConstants#RELAXNG_NS_URI
49       */
50      public static Schema loadSchema(Resource resource, String schemaLanguage) throws IOException, SAXException {
51          return loadSchema(new Resource[]{resource}, schemaLanguage);
52      }
53  
54      /**
55       * Load schema from the given resource.
56       *
57       * @param resources      the resources to load from
58       * @param schemaLanguage the language of the schema. Can be <code>XMLConstants.W3C_XML_SCHEMA_NS_URI</code> or
59       *                       <code>XMLConstants.RELAXNG_NS_URI</code>.
60       * @throws IOException  if loading failed
61       * @throws SAXException if loading failed
62       * @see javax.xml.XMLConstants#W3C_XML_SCHEMA_NS_URI
63       * @see javax.xml.XMLConstants#RELAXNG_NS_URI
64       */
65      public static Schema loadSchema(Resource[] resources, String schemaLanguage) throws IOException, SAXException {
66          Assert.notEmpty(resources, "No resources given");
67          Assert.hasLength(schemaLanguage, "No schema language provided");
68          Source[] schemaSources = new Source[resources.length];
69          XMLReader xmlReader = XMLReaderFactory.createXMLReader();
70          xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
71          for (int i = 0; i < resources.length; i++) {
72              Assert.notNull(resources[i], "Resource is null");
73              Assert.isTrue(resources[i].exists(), "Resource " + resources[i] + " does not exist");
74              schemaSources[i] = new ResourceSource(xmlReader, resources[i]);
75          }
76          SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage);
77          return schemaFactory.newSchema(schemaSources);
78      }
79  
80      /** Retrieves the URL from the given resource as System ID. Returns <code>null</code> if it cannot be openened. */
81      public static String getSystemId(Resource resource) {
82          try {
83              return resource.getURL().toString();
84          }
85          catch (IOException e) {
86              return null;
87          }
88      }
89  }