001: // $Id: ErrorHandler.java 126 2005-03-31 00:45:13Z jg_hamburg $
002: /********************************************************************************
003: * DDTUnit, a Datadriven Approach to Unit- and Moduletesting
004: * Copyright (c) 2004, Joerg and Kai Gellien
005: * All rights reserved.
006: *
007: * The Software is provided under the terms of the Common Public License 1.0
008: * as provided with the distribution of DDTUnit in the file cpl-v10.html.
009: * Redistribution and use in source and binary forms, with or without
010: * modification, are permitted provided that the following conditions
011: * are met:
012: *
013: * + Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * + Redistributions in binary form must reproduce the above
017: * copyright notice, this list of conditions and the following
018: * disclaimer in the documentation and/or other materials provided
019: * with the distribution.
020: *
021: * + Neither the name of the authors or DDTUnit, nor the
022: * names of its contributors may be used to endorse or promote
023: * products derived from this software without specific prior
024: * written permission.
025: *
026: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
027: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
028: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
029: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
030: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
031: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
032: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
033: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
036: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: ********************************************************************************/package junitx.ddtunit.data.processing.parser;
038:
039: import org.apache.log4j.Logger;
040: import org.xml.sax.SAXException;
041: import org.xml.sax.SAXParseException;
042:
043: /**
044: * Basic class idea taken from : <br/>SAX2 - David Brownell, O'Reilly, Köln,
045: * Paris 2002, p.51ff
046: *
047: * @author jg
048: *
049: * To change the template for this generated type comment go to
050: * Window>Preferences>Java>Code Generation>Code and Comments
051: */
052: class ErrorHandler implements org.xml.sax.ErrorHandler {
053: private final static int ERR_PRINT = 1;
054:
055: private final static int ERR_IGNORE = 2;
056:
057: private final static int WARN_PRINT = 4;
058:
059: private final static int WARN_IGNORE = 8;
060:
061: private final static int FATAL_PRINT = 16;
062:
063: private final static int FATAL_IGNORE = 32;
064:
065: private static final String LF = System
066: .getProperty("line.separator");
067:
068: private Logger log = Logger.getLogger(ErrorHandler.class);
069:
070: private int flags;
071:
072: /**
073: *
074: */
075: public ErrorHandler() {
076: flags = ERR_PRINT + WARN_PRINT + FATAL_PRINT;
077: }
078:
079: /**
080: * @param exception propagated from sax parser
081: * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
082: */
083: public void error(SAXParseException exception) throws SAXException {
084: log.error(printParseException("Error", exception), exception);
085:
086: if ((flags & ERR_IGNORE) == 0) {
087: throw exception;
088: }
089: }
090:
091: /**
092: * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
093: */
094: public void fatalError(SAXParseException exception)
095: throws SAXException {
096: log.error(printParseException("Error", exception), exception);
097:
098: if ((flags & FATAL_IGNORE) == 0) {
099: throw new SAXException(printParseException("Error",
100: exception));
101: }
102: }
103:
104: /**
105: * @param exception propagated from sax parser
106: * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
107: */
108: public void warning(SAXParseException exception)
109: throws SAXException {
110: log.warn(printParseException("Warning", exception), exception);
111:
112: if ((flags & WARN_IGNORE) == 0) {
113: throw exception;
114: }
115: }
116:
117: private String printParseException(String message,
118: SAXParseException e) {
119: StringBuffer sb = new StringBuffer();
120: int temp;
121:
122: sb.append("** ").append(message).append(": ").append(
123: e.getMessage()).append(LF);
124:
125: // most such exceptions include the (absolute) URI forthe text
126: if (e.getSystemId() != null) {
127: sb.append(" URI: ").append(e.getSystemId()).append(" ");
128: }
129:
130: // many include approximate line and column numbers
131: if ((temp = e.getLineNumber()) != -1) {
132: sb.append(" - line: ").append(temp).append(" ");
133: }
134:
135: if ((temp = e.getColumnNumber()) != -1) {
136: sb.append(", col : ").append(temp);
137: }
138:
139: // public id might be available, but is seldom useful
140: return sb.toString();
141: }
142: }
|