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.HashMap;
27 import java.util.Map;
28
29 import javax.xml.transform.stream.StreamSource;
30 import javax.xml.validation.Schema;
31 import javax.xml.validation.SchemaFactory;
32 import javax.xml.validation.Validator;
33
34 import org.apache.commons.io.input.XmlStreamReader;
35 import org.codehaus.plexus.util.IOUtil;
36 import org.xml.sax.SAXException;
37
38
39
40
41
42
43
44
45
46 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 private Map<String, Schema> compiledSchemas = new HashMap<String, Schema>();
56
57 public XmlValidationHandler validateXmlWithSchema( File file, String schemaVersion, boolean failOnValidationError )
58 throws SchemaValidatorException
59 {
60 Reader reader = null;
61 try
62 {
63 String schemaPath = CHANGES_SCHEMA_PATH + "changes-" + schemaVersion + ".xsd";
64
65 Schema schema = getSchema( schemaPath );
66
67 Validator validator = schema.newValidator();
68
69 XmlValidationHandler baseHandler = new XmlValidationHandler( failOnValidationError );
70
71 validator.setErrorHandler( baseHandler );
72
73 reader = new XmlStreamReader( file );
74
75 validator.validate( new StreamSource( reader ) );
76
77 reader.close();
78 reader = null;
79
80 return baseHandler;
81 }
82 catch ( IOException e )
83 {
84 throw new SchemaValidatorException( "IOException : " + e.getMessage(), e );
85 }
86 catch ( SAXException e )
87 {
88 throw new SchemaValidatorException( "SAXException : " + e.getMessage(), e );
89 }
90 catch ( Exception e )
91 {
92 throw new SchemaValidatorException( "Exception : " + e.getMessage(), e );
93 }
94 finally
95 {
96 IOUtil.close( reader );
97 }
98 }
99
100 public Schema getSchema( String schemaPath )
101 throws SAXException, IOException
102 {
103 if ( this.compiledSchemas.containsKey( schemaPath ) )
104 {
105 return (Schema) this.compiledSchemas.get( schemaPath );
106 }
107 Schema schema = this.compileJAXPSchema( schemaPath );
108
109 this.compiledSchemas.put( schemaPath, schema );
110
111 return schema;
112 }
113
114
115
116
117
118
119 private Schema compileJAXPSchema( String uriSchema )
120 throws IOException, SAXException, NullPointerException
121 {
122 InputStream in = null;
123 try
124 {
125 in = Thread.currentThread().getContextClassLoader().getResourceAsStream( uriSchema );
126 if ( in == null )
127 {
128 throw new NullPointerException( " impossible to load schema with path " + uriSchema );
129 }
130
131
132 final Schema schema = SchemaFactory.newInstance( W3C_XML_SCHEMA ).newSchema( new StreamSource( in ) );
133 in.close();
134 in = null;
135 return schema;
136 }
137 finally
138 {
139 IOUtil.close( in );
140 }
141 }
142
143 public void loadSchema( String uriSchema )
144 throws SchemaValidatorException
145 {
146 try
147 {
148 this.getSchema( uriSchema );
149 }
150 catch ( SAXException e )
151 {
152 throw new SchemaValidatorException( "SAXException : " + e.getMessage(), e );
153 }
154 catch ( IOException e )
155 {
156 throw new SchemaValidatorException( "IOException : " + e.getMessage(), e );
157 }
158
159 }
160 }