View Javadoc

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