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 | 6 | 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 | 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 | |
|
115 | |
|
116 | |
|
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 | |
|
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 | |
} |