View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * @author Olivier Lamy
31   * @since 28 juil. 2008
32   * @version $Id$
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       * @param failOnValidationError If fail on validation error.
48       */
49      public XmlValidationHandler(boolean failOnValidationError) {
50          this.failOnValidationError = failOnValidationError;
51      }
52  
53      /**
54       * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
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       * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
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       * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
76       */
77      public void warning(SAXParseException excp) {
78          this.warnings.add(excp);
79      }
80  
81      /**
82       * @see org.xml.sax.ContentHandler#startElement(String, String, String, Attributes)
83       */
84      public void startElement(String uri, String localName, String qName, Attributes attributes) {
85          // nothing
86      }
87  
88      /**
89       * @return Returns the errorParsing.
90       */
91      public boolean isErrorParsing() {
92          return this.parsingError;
93      }
94  
95      /**
96       * @param error The errorParsing to set.
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 }