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 java.util.ArrayList;
22 import java.util.List;
23
24 import org.xml.sax.Attributes;
25 import org.xml.sax.SAXException;
26 import org.xml.sax.SAXParseException;
27 import org.xml.sax.helpers.DefaultHandler;
28
29
30
31
32
33
34 public class XmlValidationHandler extends DefaultHandler {
35
36 private boolean parsingError = false;
37
38 private List<SAXParseException> errors = new ArrayList<>();
39
40 private List<SAXParseException> fatalErrors = new ArrayList<>();
41
42 private List<SAXParseException> warnings = new ArrayList<>();
43
44 private boolean failOnValidationError;
45
46
47
48
49 public XmlValidationHandler(boolean failOnValidationError) {
50 this.failOnValidationError = failOnValidationError;
51 }
52
53
54
55
56 public void error(SAXParseException excp) throws SAXException {
57 this.setErrorParsing(true);
58 this.errors.add(excp);
59 if (this.failOnValidationError) {
60 throw new SAXException(excp.getMessage(), excp);
61 }
62 }
63
64
65
66
67 public void fatalError(SAXParseException excp) throws SAXException {
68 this.fatalErrors.add(excp);
69 if (this.failOnValidationError) {
70 throw new SAXException(excp.getMessage(), excp);
71 }
72 }
73
74
75
76
77 public void warning(SAXParseException excp) {
78 this.warnings.add(excp);
79 }
80
81
82
83
84 public void startElement(String uri, String localName, String qName, Attributes attributes) {
85
86 }
87
88
89
90
91 public boolean isErrorParsing() {
92 return this.parsingError;
93 }
94
95
96
97
98 public void setErrorParsing(boolean error) {
99 this.parsingError = error;
100 }
101
102 public List<SAXParseException> getErrors() {
103 return errors;
104 }
105
106 public void setErrors(List<SAXParseException> errors) {
107 this.errors = errors;
108 }
109
110 public List<SAXParseException> getFatalErrors() {
111 return fatalErrors;
112 }
113
114 public void setFatalErrors(List<SAXParseException> fatalErrors) {
115 this.fatalErrors = fatalErrors;
116 }
117
118 public List<SAXParseException> getWarnings() {
119 return warnings;
120 }
121
122 public void setWarnings(List<SAXParseException> warnings) {
123 this.warnings = warnings;
124 }
125 }