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 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.codehaus.plexus.util.IOUtil; |
35 | |
import org.codehaus.plexus.util.xml.XmlStreamReader; |
36 | |
import org.xml.sax.SAXException; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 3 | public class DefaultChangesSchemaValidator |
47 | |
implements ChangesSchemaValidator |
48 | |
{ |
49 | |
|
50 | |
|
51 | |
public static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema"; |
52 | |
|
53 | |
public static final String CHANGES_SCHEMA_PATH = "META-INF/changes/xsd/"; |
54 | |
|
55 | 3 | private Map compiledSchemas = new FastMap(); |
56 | |
|
57 | |
public XmlValidationHandler validateXmlWithSchema( File file, String schemaVersion, boolean failOnValidationError ) |
58 | |
throws SchemaValidatorException |
59 | |
{ |
60 | 3 | Reader reader = null; |
61 | |
try |
62 | |
{ |
63 | 3 | String schemaPath = CHANGES_SCHEMA_PATH + "changes-" + schemaVersion + ".xsd"; |
64 | |
|
65 | 3 | Schema schema = getSchema( schemaPath ); |
66 | |
|
67 | 3 | Validator validator = schema.newValidator(); |
68 | |
|
69 | 3 | XmlValidationHandler baseHandler = new XmlValidationHandler( failOnValidationError ); |
70 | |
|
71 | 3 | validator.setErrorHandler( baseHandler ); |
72 | |
|
73 | 3 | reader = new XmlStreamReader( file ); |
74 | |
|
75 | 3 | validator.validate( new StreamSource( reader ) ); |
76 | |
|
77 | 2 | return baseHandler; |
78 | |
} |
79 | 0 | catch ( IOException e ) |
80 | |
{ |
81 | 0 | throw new SchemaValidatorException( "IOException : " + e.getMessage(), e ); |
82 | |
} |
83 | 1 | catch ( SAXException e ) |
84 | |
{ |
85 | 1 | throw new SchemaValidatorException( "SAXException : " + e.getMessage(), e ); |
86 | |
} |
87 | 0 | catch ( Exception e ) |
88 | |
{ |
89 | 0 | throw new SchemaValidatorException( "Exception : " + e.getMessage(), e ); |
90 | |
} |
91 | |
finally |
92 | |
{ |
93 | 3 | IOUtil.close( reader ); |
94 | |
} |
95 | |
} |
96 | |
|
97 | |
public Schema getSchema( String schemaPath ) |
98 | |
throws SAXException |
99 | |
{ |
100 | 3 | if ( this.compiledSchemas.containsKey( schemaPath ) ) |
101 | |
{ |
102 | 0 | return (Schema) this.compiledSchemas.get( schemaPath ); |
103 | |
} |
104 | 3 | Schema schema = this.compileJAXPSchema( schemaPath ); |
105 | |
|
106 | 3 | this.compiledSchemas.put( schemaPath, schema ); |
107 | |
|
108 | 3 | return schema; |
109 | |
} |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
private Schema compileJAXPSchema( String uriSchema ) |
117 | |
throws SAXException, NullPointerException |
118 | |
{ |
119 | |
|
120 | 3 | InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( uriSchema ); |
121 | |
|
122 | 3 | if ( is == null ) |
123 | |
{ |
124 | 0 | throw new NullPointerException( " impossible to load schema with path " + uriSchema ); |
125 | |
} |
126 | |
|
127 | |
try |
128 | |
{ |
129 | |
|
130 | 3 | return SchemaFactory.newInstance( W3C_XML_SCHEMA ).newSchema( new StreamSource( is ) ); |
131 | |
} |
132 | |
finally |
133 | |
{ |
134 | 3 | IOUtil.close( is ); |
135 | |
} |
136 | |
} |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
public void loadSchema( String uriSchema ) |
142 | |
throws SchemaValidatorException |
143 | |
{ |
144 | |
try |
145 | |
{ |
146 | 0 | this.getSchema( uriSchema ); |
147 | |
} |
148 | 0 | catch ( SAXException e ) |
149 | |
{ |
150 | 0 | throw new SchemaValidatorException( "SAXException : " + e.getMessage(), e ); |
151 | 0 | } |
152 | |
|
153 | 0 | } |
154 | |
} |