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