Coverage Report - org.apache.maven.plugin.changes.schema.DefaultChangesSchemaValidator
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultChangesSchemaValidator
66%
22/33
50%
2/4
4.5
 
 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  6
 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  6
     private Map compiledSchemas = new FastMap();
 58  
 
 59  
     public XmlValidationHandler validateXmlWithSchema( File file, String schemaVersion, boolean failOnValidationError )
 60  
         throws SchemaValidatorException
 61  
     {
 62  6
         Reader reader = null;
 63  
         try
 64  
         {
 65  6
             String schemaPath = CHANGES_SCHEMA_PATH + "changes-" + schemaVersion + ".xsd";
 66  
 
 67  6
             Schema schema = getSchema( schemaPath );
 68  
 
 69  6
             Validator validator = schema.newValidator();
 70  
 
 71  6
             XmlValidationHandler baseHandler = new XmlValidationHandler( failOnValidationError );
 72  
 
 73  6
             validator.setErrorHandler( baseHandler );
 74  
 
 75  6
             reader = new XmlStreamReader( file );
 76  
 
 77  6
             validator.validate( new StreamSource( reader ) );
 78  
 
 79  4
             return baseHandler;
 80  
         }
 81  0
         catch ( IOException e )
 82  
         {
 83  0
             throw new SchemaValidatorException( "IOException : " + e.getMessage(), e );
 84  
         }
 85  2
         catch ( SAXException e )
 86  
         {
 87  2
             throw new SchemaValidatorException( "SAXException : " + e.getMessage(), e );
 88  
         }
 89  0
         catch ( Exception e )
 90  
         {
 91  0
             throw new SchemaValidatorException( "Exception : " + e.getMessage(), e );
 92  
         }
 93  
         finally
 94  
         {
 95  6
             IOUtil.close( reader );
 96  
         }
 97  
     }
 98  
 
 99  
     public Schema getSchema( String schemaPath )
 100  
         throws SAXException
 101  
     {
 102  6
         if ( this.compiledSchemas.containsKey( schemaPath ) )
 103  
         {
 104  0
             return (Schema) this.compiledSchemas.get( schemaPath );
 105  
         }
 106  6
         Schema schema = this.compileJAXPSchema( schemaPath );
 107  
 
 108  6
         this.compiledSchemas.put( schemaPath, schema );
 109  
 
 110  6
         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  6
         InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( uriSchema );
 123  
 
 124  6
         if ( is == null )
 125  
         {
 126  0
             throw new NullPointerException( " impossible to load schema with path " + uriSchema );
 127  
         }
 128  
 
 129  
         try
 130  
         {
 131  
             //newInstance de SchemaFactory not ThreadSafe
 132  6
             return SchemaFactory.newInstance( W3C_XML_SCHEMA ).newSchema( new StreamSource( is ) );
 133  
         }
 134  
         finally
 135  
         {
 136  6
             IOUtil.close( is );
 137  
         }
 138  
     }
 139  
 
 140  
     public void loadSchema( String uriSchema )
 141  
         throws SchemaValidatorException
 142  
     {
 143  
         try
 144  
         {
 145  0
             this.getSchema( uriSchema );
 146  
         }
 147  0
         catch ( SAXException e )
 148  
         {
 149  0
             throw new SchemaValidatorException( "SAXException : " + e.getMessage(), e );
 150  0
         }
 151  
 
 152  0
     }
 153  
 }