001: /*
002: * Copyright 2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.xml.validation;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import javax.xml.transform.stream.StreamSource;
022: import javax.xml.validation.Schema;
023: import javax.xml.validation.SchemaFactory;
024:
025: import org.springframework.core.io.Resource;
026: import org.springframework.util.Assert;
027: import org.xml.sax.SAXException;
028:
029: /**
030: * Convenient utility methods for loading of <code>javax.xml.validation.Schema</code> objects, performing standard
031: * handling of input streams.
032: *
033: * @author Arjen Poutsma
034: * @since 1.0.0
035: */
036: public abstract class SchemaLoaderUtils {
037:
038: /**
039: * Load schema from the given resource.
040: *
041: * @param resource the resource to load from
042: * @param schemaLanguage the language of the schema. Can be <code>XMLConstants.W3C_XML_SCHEMA_NS_URI</code> or
043: * <code>XMLConstants.RELAXNG_NS_URI</code>.
044: * @throws IOException if loading failed
045: * @throws SAXException if loading failed
046: * @see javax.xml.XMLConstants#W3C_XML_SCHEMA_NS_URI
047: * @see javax.xml.XMLConstants#RELAXNG_NS_URI
048: */
049: public static Schema loadSchema(Resource resource,
050: String schemaLanguage) throws IOException, SAXException {
051: return loadSchema(new Resource[] { resource }, schemaLanguage);
052: }
053:
054: /**
055: * Load schema from the given resource.
056: *
057: * @param resources the resources to load from
058: * @param schemaLanguage the language of the schema. Can be <code>XMLConstants.W3C_XML_SCHEMA_NS_URI</code> or
059: * <code>XMLConstants.RELAXNG_NS_URI</code>.
060: * @throws IOException if loading failed
061: * @throws SAXException if loading failed
062: * @see javax.xml.XMLConstants#W3C_XML_SCHEMA_NS_URI
063: * @see javax.xml.XMLConstants#RELAXNG_NS_URI
064: */
065: public static Schema loadSchema(Resource[] resources,
066: String schemaLanguage) throws IOException, SAXException {
067: Assert.notEmpty(resources, "No resources given");
068: Assert.hasLength(schemaLanguage, "No schema language provided");
069: StreamSource[] schemaSources = new StreamSource[resources.length];
070: try {
071: for (int i = 0; i < resources.length; i++) {
072: Assert.notNull(resources[i], "Resource is null");
073: Assert.isTrue(resources[i].exists(), "Resource "
074: + resources[i] + " does not exist");
075: schemaSources[i] = new StreamSource(resources[i]
076: .getInputStream(), getSystemId(resources[i]));
077: }
078: SchemaFactory schemaFactory = SchemaFactory
079: .newInstance(schemaLanguage);
080: return schemaFactory.newSchema(schemaSources);
081: } finally {
082: for (int i = 0; i < schemaSources.length; i++) {
083: if (schemaSources[i] != null) {
084: InputStream inputStream = schemaSources[i]
085: .getInputStream();
086: if (inputStream != null) {
087: inputStream.close();
088: }
089: }
090: }
091: }
092: }
093:
094: /** Retrieves the URL from the given resource as System ID. Returns <code>null</code> if it cannot be openened. */
095: public static String getSystemId(Resource resource) {
096: try {
097: return resource.getURL().toString();
098: } catch (IOException e) {
099: return null;
100: }
101: }
102: }
|