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:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023: import org.springframework.core.io.Resource;
024: import org.springframework.util.Assert;
025: import org.springframework.xml.JaxpVersion;
026:
027: /**
028: * Factory for <code>XmlValidator</code>s, being aware of JAXP 1.3 <code>XmlValidator</code>s, and JAXP 1.0 parsing
029: * capababilities. Mainly for internal use within the framework.
030: * <p/>
031: * The goal of this class is to avoid runtime dependencies on JAXP 1.3 by using the best validation implementation that
032: * is available. Prefers JAXP 1.3 <code>XmlValidator</code> implementations to a custom, SAX-based implementation.
033: *
034: * @author Arjen Poutsma
035: * @see XmlValidator
036: * @since 1.0.0
037: */
038: public abstract class XmlValidatorFactory {
039:
040: private static final Log logger = LogFactory
041: .getLog(XmlValidatorFactory.class);
042:
043: /** Constant that defines a W3C XML Schema. */
044: public static final String SCHEMA_W3C_XML = "http://www.w3.org/2001/XMLSchema";
045:
046: /** Constant that defines a RELAX NG Schema. */
047: public static final String SCHEMA_RELAX_NG = "http://relaxng.org/ns/structure/1.0";
048:
049: /**
050: * Create a <code>XmlValidator</code> with the given schema resource and schema language type. The schema language
051: * must be one of the <code>SCHEMA_XXX</code> constants.
052: *
053: * @param schemaResource a resource that locates the schema to validate against
054: * @param schemaLanguage the language of the schema
055: * @return a validator
056: * @throws IOException if the schema resource cannot be read
057: * @throws IllegalArgumentException if the schema language is not supported
058: * @throws IllegalStateException if JAXP 1.0 cannot be located
059: * @throws XmlValidationException if a <code>XmlValidator</code> cannot be created
060: * @see #SCHEMA_RELAX_NG
061: * @see #SCHEMA_W3C_XML
062: */
063: public static XmlValidator createValidator(Resource schemaResource,
064: String schemaLanguage) throws IOException {
065: return createValidator(new Resource[] { schemaResource },
066: schemaLanguage);
067: }
068:
069: /**
070: * Create a <code>XmlValidator</code> with the given schema resources and schema language type. The schema language
071: * must be one of the <code>SCHEMA_XXX</code> constants.
072: *
073: * @param schemaResources an array of resource that locate the schemas to validate against
074: * @param schemaLanguage the language of the schemas
075: * @return a validator
076: * @throws IOException if the schema resource cannot be read
077: * @throws IllegalArgumentException if the schema language is not supported
078: * @throws IllegalStateException if JAXP 1.0 cannot be located
079: * @throws XmlValidationException if a <code>XmlValidator</code> cannot be created
080: * @see #SCHEMA_RELAX_NG
081: * @see #SCHEMA_W3C_XML
082: */
083: public static XmlValidator createValidator(
084: Resource[] schemaResources, String schemaLanguage)
085: throws IOException {
086: Assert.notEmpty(schemaResources, "No resources given");
087: Assert.hasLength(schemaLanguage, "No schema language provided");
088: Assert.isTrue(SCHEMA_W3C_XML.equals(schemaLanguage)
089: || SCHEMA_RELAX_NG.equals(schemaLanguage),
090: "Invalid schema language: " + schemaLanguage);
091: for (int i = 0; i < schemaResources.length; i++) {
092: Assert.isTrue(schemaResources[i].exists(), "schema ["
093: + schemaResources[i] + "] does not exist");
094: }
095: if (JaxpVersion.getJaxpVersion() >= JaxpVersion.JAXP_13) {
096: logger.trace("Creating JAXP 1.3 XmlValidator");
097: return Jaxp13ValidatorFactory.createValidator(
098: schemaResources, schemaLanguage);
099: } else if (JaxpVersion.getJaxpVersion() >= JaxpVersion.JAXP_10) {
100: logger.trace("Creating JAXP 1.0 XmlValidator");
101: return Jaxp10ValidatorFactory.createValidator(
102: schemaResources, schemaLanguage);
103: } else {
104: throw new IllegalStateException(
105: "Could not locate JAXP 1.0 or higher.");
106: }
107: }
108:
109: }
|