View Javadoc

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