View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.changes.schema;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  import javax.xml.transform.stream.StreamSource;
24  import javax.xml.validation.Schema;
25  import javax.xml.validation.SchemaFactory;
26  import javax.xml.validation.Validator;
27  
28  import java.io.File;
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.io.Reader;
32  import java.util.HashMap;
33  import java.util.Map;
34  
35  import org.apache.commons.io.input.XmlStreamReader;
36  import org.xml.sax.SAXException;
37  
38  /**
39   * @author Olivier Lamy
40   * @since 28 juil. 2008
41   * @version $Id$
42   */
43  @Named
44  @Singleton
45  public class DefaultChangesSchemaValidator implements ChangesSchemaValidator {
46  
47      /** property schema */
48      public static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
49  
50      public static final String CHANGES_SCHEMA_PATH = "META-INF/changes/xsd/";
51  
52      private Map<String, Schema> compiledSchemas = new HashMap<>();
53  
54      public XmlValidationHandler validateXmlWithSchema(File file, String schemaVersion, boolean failOnValidationError)
55              throws SchemaValidatorException {
56          try {
57              String schemaPath = CHANGES_SCHEMA_PATH + "changes-" + schemaVersion + ".xsd";
58  
59              Schema schema = getSchema(schemaPath);
60  
61              Validator validator = schema.newValidator();
62  
63              XmlValidationHandler baseHandler = new XmlValidationHandler(failOnValidationError);
64  
65              validator.setErrorHandler(baseHandler);
66  
67              try (Reader reader = XmlStreamReader.builder().setFile(file).get()) {
68                  validator.validate(new StreamSource(reader));
69              }
70  
71              return baseHandler;
72          } catch (IOException e) {
73              throw new SchemaValidatorException("IOException : " + e.getMessage(), e);
74          } catch (SAXException e) {
75              throw new SchemaValidatorException("SAXException : " + e.getMessage(), e);
76          } catch (Exception e) {
77              throw new SchemaValidatorException("Exception : " + e.getMessage(), e);
78          }
79      }
80  
81      public Schema getSchema(String schemaPath) throws SAXException, IOException {
82          if (this.compiledSchemas.containsKey(schemaPath)) {
83              return this.compiledSchemas.get(schemaPath);
84          }
85          Schema schema = this.compileJAXPSchema(schemaPath);
86  
87          this.compiledSchemas.put(schemaPath, schema);
88  
89          return schema;
90      }
91  
92      private Schema compileJAXPSchema(String uriSchema) throws IOException, SAXException, NullPointerException {
93          try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(uriSchema)) {
94              if (in == null) {
95                  throw new NullPointerException(" impossible to load schema with path " + uriSchema);
96              }
97  
98              // newInstance de SchemaFactory not ThreadSafe
99              final Schema schema = SchemaFactory.newInstance(W3C_XML_SCHEMA).newSchema(new StreamSource(in));
100             return schema;
101         }
102     }
103 
104     public void loadSchema(String uriSchema) throws SchemaValidatorException {
105         try {
106             this.getSchema(uriSchema);
107         } catch (SAXException e) {
108             throw new SchemaValidatorException("SAXException : " + e.getMessage(), e);
109         } catch (IOException e) {
110             throw new SchemaValidatorException("IOException : " + e.getMessage(), e);
111         }
112     }
113 }