1 package org.apache.maven.plugin.changes.schema;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
43
44
45
46
47
48 public class DefaultChangesSchemaValidator
49 implements ChangesSchemaValidator
50 {
51
52
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
115
116
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
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 }