1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.changes.schema;
20
21 import javax.inject.Named;
22 import javax.inject.Singleton;
23 import javax.xml.transform.stream.StreamSource;
24 import javax.xml.validation.Schema;
25 import javax.xml.validation.SchemaFactory;
26 import javax.xml.validation.Validator;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.Reader;
32 import java.util.HashMap;
33 import java.util.Map;
34
35 import org.apache.commons.io.input.XmlStreamReader;
36 import org.xml.sax.SAXException;
37
38
39
40
41
42
43 @Named
44 @Singleton
45 public class DefaultChangesSchemaValidator implements ChangesSchemaValidator {
46
47
48 public static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
49
50 public static final String CHANGES_SCHEMA_PATH = "META-INF/changes/xsd/";
51
52 private Map<String, Schema> compiledSchemas = new HashMap<>();
53
54 public XmlValidationHandler validateXmlWithSchema(File file, String schemaVersion, boolean failOnValidationError)
55 throws SchemaValidatorException {
56 try {
57 String schemaPath = CHANGES_SCHEMA_PATH + "changes-" + schemaVersion + ".xsd";
58
59 Schema schema = getSchema(schemaPath);
60
61 Validator validator = schema.newValidator();
62
63 XmlValidationHandler baseHandler = new XmlValidationHandler(failOnValidationError);
64
65 validator.setErrorHandler(baseHandler);
66
67 try (Reader reader = XmlStreamReader.builder().setFile(file).get()) {
68 validator.validate(new StreamSource(reader));
69 }
70
71 return baseHandler;
72 } catch (IOException e) {
73 throw new SchemaValidatorException("IOException : " + e.getMessage(), e);
74 } catch (SAXException e) {
75 throw new SchemaValidatorException("SAXException : " + e.getMessage(), e);
76 } catch (Exception e) {
77 throw new SchemaValidatorException("Exception : " + e.getMessage(), e);
78 }
79 }
80
81 public Schema getSchema(String schemaPath) throws SAXException, IOException {
82 if (this.compiledSchemas.containsKey(schemaPath)) {
83 return this.compiledSchemas.get(schemaPath);
84 }
85 Schema schema = this.compileJAXPSchema(schemaPath);
86
87 this.compiledSchemas.put(schemaPath, schema);
88
89 return schema;
90 }
91
92 private Schema compileJAXPSchema(String uriSchema) throws IOException, SAXException, NullPointerException {
93 try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(uriSchema)) {
94 if (in == null) {
95 throw new NullPointerException(" impossible to load schema with path " + uriSchema);
96 }
97
98
99 final Schema schema = SchemaFactory.newInstance(W3C_XML_SCHEMA).newSchema(new StreamSource(in));
100 return schema;
101 }
102 }
103
104 public void loadSchema(String uriSchema) throws SchemaValidatorException {
105 try {
106 this.getSchema(uriSchema);
107 } catch (SAXException e) {
108 throw new SchemaValidatorException("SAXException : " + e.getMessage(), e);
109 } catch (IOException e) {
110 throw new SchemaValidatorException("IOException : " + e.getMessage(), e);
111 }
112 }
113 }