1 // $Id: ErrorHandler.java 351 2008-08-14 20:20:56Z jg_hamburg $
2 /********************************************************************************
3 * DDTUnit, a Datadriven Approach to Unit- and Moduletesting
4 * Copyright (c) 2004, Joerg and Kai Gellien
5 * All rights reserved.
6 *
7 * The Software is provided under the terms of the Common Public License 1.0
8 * as provided with the distribution of DDTUnit in the file cpl-v10.html.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * + Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * + Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer in the documentation and/or other materials provided
19 * with the distribution.
20 *
21 * + Neither the name of the authors or DDTUnit, nor the
22 * names of its contributors may be used to endorse or promote
23 * products derived from this software without specific prior
24 * written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 ********************************************************************************/
38 package junitx.ddtunit.data.processing.parser;
39
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.xml.sax.SAXException;
43 import org.xml.sax.SAXParseException;
44
45 /**
46 * Basic class idea taken from : <br/>SAX2 - David Brownell, O'Reilly, Kˆln,
47 * Paris 2002, p.51ff
48 *
49 * @author jg
50 *
51 * To change the template for this generated type comment go to
52 * Window>Preferences>Java>Code Generation>Code and Comments
53 */
54 class ErrorHandler implements org.xml.sax.ErrorHandler {
55 private final static int ERR_PRINT = 1;
56
57 private final static int ERR_IGNORE = 2;
58
59 private final static int WARN_PRINT = 4;
60
61 private final static int WARN_IGNORE = 8;
62
63 private final static int FATAL_PRINT = 16;
64
65 private final static int FATAL_IGNORE = 32;
66
67 private static final String LF = System.getProperty("line.separator");
68
69 private Logger log = LoggerFactory.getLogger(ErrorHandler.class);
70
71 private int flags;
72
73 /**
74 *
75 */
76 public ErrorHandler() {
77 flags = ERR_PRINT + WARN_PRINT + FATAL_PRINT;
78 }
79
80 /**
81 * @param exception propagated from sax parser
82 * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
83 */
84 public void error(SAXParseException exception) throws SAXException {
85 log.error(printParseException("Error", exception), exception);
86
87 if ((flags & ERR_IGNORE) == 0) {
88 throw exception;
89 }
90 }
91
92 /**
93 * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
94 */
95 public void fatalError(SAXParseException exception) throws SAXException {
96 log.error(printParseException("Error", exception), exception);
97
98 if ((flags & FATAL_IGNORE) == 0) {
99 throw new SAXException(printParseException("Error", exception));
100 }
101 }
102
103 /**
104 * @param exception propagated from sax parser
105 * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
106 */
107 public void warning(SAXParseException exception) throws SAXException {
108 log.warn(printParseException("Warning", exception), exception);
109
110 if ((flags & WARN_IGNORE) == 0) {
111 throw exception;
112 }
113 }
114
115 private String printParseException(String message, SAXParseException e) {
116 StringBuffer sb = new StringBuffer();
117 int temp;
118
119 sb.append("** ").append(message).append(": ").append(e.getMessage())
120 .append(LF);
121
122 // most such exceptions include the (absolute) URI forthe text
123 if (e.getSystemId() != null) {
124 sb.append(" URI: ").append(e.getSystemId()).append(" ");
125 }
126
127 // many include approximate line and column numbers
128 if ((temp = e.getLineNumber()) != -1) {
129 sb.append(" - line: ").append(temp).append(" ");
130 }
131
132 if ((temp = e.getColumnNumber()) != -1) {
133 sb.append(", col : ").append(temp);
134 }
135
136 // public id might be available, but is seldom useful
137 return sb.toString();
138 }
139 }