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.validation.Validator; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 import org.springframework.core.io.Resource; 25 import org.springframework.util.Assert; 26 import org.springframework.xml.JaxpVersion; 27 28 /** 29 * Factory for {@link XmlValidator} objects, being aware of JAXP 1.3 {@link Validator}s, and JAXP 1.0 parsing 30 * capababilities. Mainly for internal use within the framework. 31 * <p/> 32 * The goal of this class is to avoid runtime dependencies on JAXP 1.3 by using the best validation implementation that 33 * is available. Prefers JAXP 1.3 {@link XmlValidator} implementations to a custom, SAX-based implementation. 34 * 35 * @author Arjen Poutsma 36 * @see XmlValidator 37 * @since 1.0.0 38 */ 39 public abstract class XmlValidatorFactory { 40 41 private static final Log logger = LogFactory.getLog(XmlValidatorFactory.class); 42 43 /** Constant that defines a W3C XML Schema. */ 44 public static final String SCHEMA_W3C_XML = "http://www.w3.org/2001/XMLSchema"; 45 46 /** Constant that defines a RELAX NG Schema. */ 47 public static final String SCHEMA_RELAX_NG = "http://relaxng.org/ns/structure/1.0"; 48 49 /** 50 * Create a {@link XmlValidator} with the given schema resource and schema language type. The schema language must 51 * be one of the <code>SCHEMA_XXX</code> constants. 52 * 53 * @param schemaResource a resource that locates the schema to validate against 54 * @param schemaLanguage the language of the schema 55 * @return a validator 56 * @throws IOException if the schema resource cannot be read 57 * @throws IllegalArgumentException if the schema language is not supported 58 * @throws IllegalStateException if JAXP 1.0 cannot be located 59 * @throws XmlValidationException if a <code>XmlValidator</code> cannot be created 60 * @see #SCHEMA_RELAX_NG 61 * @see #SCHEMA_W3C_XML 62 */ 63 public static XmlValidator createValidator(Resource schemaResource, String schemaLanguage) throws IOException { 64 return createValidator(new Resource[]{schemaResource}, schemaLanguage); 65 } 66 67 /** 68 * Create a {@link XmlValidator} with the given schema resources and schema language type. The schema language must 69 * be one of the <code>SCHEMA_XXX</code> constants. 70 * 71 * @param schemaResources an array of resource that locate the schemas to validate against 72 * @param schemaLanguage the language of the schemas 73 * @return a validator 74 * @throws IOException if the schema resource cannot be read 75 * @throws IllegalArgumentException if the schema language is not supported 76 * @throws IllegalStateException if JAXP 1.0 cannot be located 77 * @throws XmlValidationException if a <code>XmlValidator</code> cannot be created 78 * @see #SCHEMA_RELAX_NG 79 * @see #SCHEMA_W3C_XML 80 */ 81 public static XmlValidator createValidator(Resource[] schemaResources, String schemaLanguage) throws IOException { 82 Assert.notEmpty(schemaResources, "No resources given"); 83 Assert.hasLength(schemaLanguage, "No schema language provided"); 84 Assert.isTrue(SCHEMA_W3C_XML.equals(schemaLanguage) || SCHEMA_RELAX_NG.equals(schemaLanguage), 85 "Invalid schema language: " + schemaLanguage); 86 for (int i = 0; i < schemaResources.length; i++) { 87 Assert.isTrue(schemaResources[i].exists(), "schema [" + schemaResources[i] + "] does not exist"); 88 } 89 if (JaxpVersion.getJaxpVersion() >= JaxpVersion.JAXP_13) { 90 logger.trace("Creating JAXP 1.3 XmlValidator"); 91 return Jaxp13ValidatorFactory.createValidator(schemaResources, schemaLanguage); 92 } 93 else if (JaxpVersion.getJaxpVersion() >= JaxpVersion.JAXP_10) { 94 logger.trace("Creating JAXP 1.0 XmlValidator"); 95 return Jaxp10ValidatorFactory.createValidator(schemaResources, schemaLanguage); 96 } 97 else { 98 throw new IllegalStateException("Could not locate JAXP 1.0 or higher."); 99 } 100 } 101 102 }