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