/maven-changes-plugin/src/main/java/org/apache/maven/plugin/changes/schema/DefaultChangesSchemaValidator.java

https://github.com/SciSysUK/maven-plugins · Java · 154 lines · 93 code · 25 blank · 36 comment · 3 complexity · a3730a29ada98ef7d6c3c1aaa3079c75 MD5 · raw file

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