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.Map;
27  
28  import org.apache.commons.io.input.XmlStreamReader;
29  
30  import javax.xml.transform.stream.StreamSource;
31  import javax.xml.validation.Schema;
32  import javax.xml.validation.SchemaFactory;
33  import javax.xml.validation.Validator;
34  
35  import org.codehaus.plexus.util.FastMap;
36  import org.codehaus.plexus.util.IOUtil;
37  
38  import org.xml.sax.SAXException;
39  
40  /**
41   *
42   * @author Olivier Lamy
43   * @since 28 juil. 2008
44   * @version $Id: DefaultChangesSchemaValidator.java 1396150 2012-10-09 18:15:29Z krosenvold $
45   *
46   * @plexus.component role="org.apache.maven.plugin.changes.schema.ChangesSchemaValidator" role-hint="default"
47   */
48  public class DefaultChangesSchemaValidator
49      implements ChangesSchemaValidator
50  {
51  
52      /** property schema */
53      public static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
54  
55      public static final String CHANGES_SCHEMA_PATH = "META-INF/changes/xsd/";
56  
57      private Map compiledSchemas = new FastMap();
58  
59      public XmlValidationHandler validateXmlWithSchema( File file, String schemaVersion, boolean failOnValidationError )
60          throws SchemaValidatorException
61      {
62          Reader reader = null;
63          try
64          {
65              String schemaPath = CHANGES_SCHEMA_PATH + "changes-" + schemaVersion + ".xsd";
66  
67              Schema schema = getSchema( schemaPath );
68  
69              Validator validator = schema.newValidator();
70  
71              XmlValidationHandler baseHandler = new XmlValidationHandler( failOnValidationError );
72  
73              validator.setErrorHandler( baseHandler );
74  
75              reader = new XmlStreamReader( file );
76  
77              validator.validate( new StreamSource( reader ) );
78  
79              return baseHandler;
80          }
81          catch ( IOException e )
82          {
83              throw new SchemaValidatorException( "IOException : " + e.getMessage(), e );
84          }
85          catch ( SAXException e )
86          {
87              throw new SchemaValidatorException( "SAXException : " + e.getMessage(), e );
88          }
89          catch ( Exception e )
90          {
91              throw new SchemaValidatorException( "Exception : " + e.getMessage(), e );
92          }
93          finally
94          {
95              IOUtil.close( reader );
96          }
97      }
98  
99      public Schema getSchema( String schemaPath )
100         throws SAXException
101     {
102         if ( this.compiledSchemas.containsKey( schemaPath ) )
103         {
104             return (Schema) this.compiledSchemas.get( schemaPath );
105         }
106         Schema schema = this.compileJAXPSchema( schemaPath );
107 
108         this.compiledSchemas.put( schemaPath, schema );
109 
110         return schema;
111     }
112 
113     /**
114      * @param uriSchema
115      * @return Schema
116      * @throws Exception
117      */
118     private Schema compileJAXPSchema( String uriSchema )
119         throws SAXException, NullPointerException
120     {
121 
122         InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( uriSchema );
123 
124         if ( is == null )
125         {
126             throw new NullPointerException( " impossible to load schema with path " + uriSchema );
127         }
128 
129         try
130         {
131             //newInstance de SchemaFactory not ThreadSafe
132             return SchemaFactory.newInstance( W3C_XML_SCHEMA ).newSchema( new StreamSource( is ) );
133         }
134         finally
135         {
136             IOUtil.close( is );
137         }
138     }
139 
140     public void loadSchema( String uriSchema )
141         throws SchemaValidatorException
142     {
143         try
144         {
145             this.getSchema( uriSchema );
146         }
147         catch ( SAXException e )
148         {
149             throw new SchemaValidatorException( "SAXException : " + e.getMessage(), e );
150         }
151 
152     }
153 }