1 package org.apache.maven.plugin.changes.schema;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.xml.sax.Attributes;
26 import org.xml.sax.SAXException;
27 import org.xml.sax.SAXParseException;
28 import org.xml.sax.helpers.DefaultHandler;
29
30 /**
31 * @author <a href="mailto:olamy@apache.org">olamy</a>
32 * @since 28 juil. 2008
33 * @version $Id: XmlValidationHandler.html 816592 2012-05-08 12:40:21Z hboutemy $
34 */
35 public class XmlValidationHandler
36 extends DefaultHandler
37 {
38
39 private boolean parsingError = false;
40
41 private List /* SAXParseException */errors = new ArrayList();
42
43 private List /* SAXParseException */fatalErrors = new ArrayList();
44
45 private List /* SAXParseException */warnings = new ArrayList();
46
47 private boolean failOnValidationError;
48
49 /**
50 * see name
51 */
52 public XmlValidationHandler( boolean failOnValidationError )
53 {
54 this.failOnValidationError = failOnValidationError;
55 }
56
57 /**
58 * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
59 */
60 public void error( SAXParseException excp )
61 throws SAXException
62 {
63 this.setErrorParsing( true );
64 this.errors.add( excp );
65 if ( this.failOnValidationError )
66 {
67 throw new SAXException( excp.getMessage(), excp );
68 }
69 }
70
71 /**
72 * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
73 */
74 public void fatalError( SAXParseException excp )
75 throws SAXException
76 {
77 this.fatalErrors.add( excp );
78 if ( this.failOnValidationError )
79 {
80 throw new SAXException( excp.getMessage(), excp );
81 }
82 }
83
84 /**
85 * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
86 */
87 public void warning( SAXParseException excp )
88 throws SAXException
89 {
90 this.warnings.add( excp );
91 }
92
93 /**
94 * @see org.xml.sax.ContentHandler#startElement(String, String, String, Attributes)
95 */
96 public void startElement( String uri, String localName, String qName, Attributes attributes )
97 throws SAXException
98 {
99 // nothing
100 }
101
102 /**
103 * @return Returns the errorParsing.
104 */
105 public boolean isErrorParsing()
106 {
107 return this.parsingError;
108 }
109
110 /**
111 * @param error The errorParsing to set.
112 */
113 public void setErrorParsing( boolean error )
114 {
115 this.parsingError = error;
116 }
117
118 public List /* SAXParseException */getErrors()
119 {
120 return errors;
121 }
122
123 public void setErrors( List errors )
124 {
125 this.errors = errors;
126 }
127
128 public List /* SAXParseException */getFatalErrors()
129 {
130 return fatalErrors;
131 }
132
133 public void setFatalErrors( List fatalErrors )
134 {
135 this.fatalErrors = fatalErrors;
136 }
137
138 public List /* SAXParseException */ getWarnings()
139 {
140 return warnings;
141 }
142
143 public void setWarnings( List warnings )
144 {
145 this.warnings = warnings;
146 }
147 }