View Javadoc

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 java.util.ArrayList;
21  import java.util.List;
22  import javax.xml.transform.Source;
23  import javax.xml.validation.Schema;
24  
25  import org.springframework.core.io.Resource;
26  import org.xml.sax.ErrorHandler;
27  import org.xml.sax.SAXException;
28  import org.xml.sax.SAXParseException;
29  
30  /**
31   * Internal class that uses JAXP 1.0 features to create <code>XmlValidator</code> instances.
32   *
33   * @author Arjen Poutsma
34   * @since 1.0.0
35   */
36  abstract class Jaxp13ValidatorFactory {
37  
38      static XmlValidator createValidator(Resource[] resources, String schemaLanguage) throws IOException {
39          try {
40              Schema schema = SchemaLoaderUtils.loadSchema(resources, schemaLanguage);
41              return new Jaxp13Validator(schema);
42          }
43          catch (SAXException ex) {
44              throw new XmlValidationException("Could not create Schema: " + ex.getMessage(), ex);
45          }
46      }
47  
48      private static class Jaxp13Validator implements XmlValidator {
49  
50          private Schema schema;
51  
52          public Jaxp13Validator(Schema schema) {
53              this.schema = schema;
54          }
55  
56          public SAXParseException[] validate(Source source) throws IOException {
57              javax.xml.validation.Validator validator = schema.newValidator();
58              ValidationErrorHandler errorHandler = new ValidationErrorHandler();
59              validator.setErrorHandler(errorHandler);
60              try {
61                  validator.validate(source);
62                  return errorHandler.getErrors();
63              }
64              catch (SAXException ex) {
65                  throw new XmlValidationException("Could not validate source: " + ex.getMessage(), ex);
66              }
67          }
68      }
69  
70      /** <code>ErrorHandler</code> implementation that stores errors and fatal errors in a list. */
71      private static class ValidationErrorHandler implements ErrorHandler {
72  
73          private List errors = new ArrayList();
74  
75          private SAXParseException[] getErrors() {
76              return (SAXParseException[]) errors.toArray(new SAXParseException[errors.size()]);
77          }
78  
79          public void warning(SAXParseException ex) throws SAXException {
80          }
81  
82          public void error(SAXParseException ex) throws SAXException {
83              errors.add(ex);
84          }
85  
86          public void fatalError(SAXParseException ex) throws SAXException {
87              errors.add(ex);
88          }
89      }
90  }