01: /*
02: * Copyright 2005 Joe Walker
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.directwebremoting.util;
17:
18: import org.apache.commons.logging.LogFactory;
19: import org.apache.commons.logging.Log;
20: import org.xml.sax.ErrorHandler;
21: import org.xml.sax.SAXParseException;
22:
23: /**
24: * An ErrorHandler that writes to the Log class
25: * @author Joe Walker [joe at getahead dot ltd dot uk]
26: */
27: public final class LogErrorHandler implements ErrorHandler {
28: /* (non-Javadoc)
29: * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
30: */
31: public void fatalError(SAXParseException ex) {
32: log.fatal(getMessage(ex));
33: }
34:
35: /* (non-Javadoc)
36: * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
37: */
38: public void error(SAXParseException ex) {
39: log.error(getMessage(ex));
40: }
41:
42: /* (non-Javadoc)
43: * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
44: */
45: public void warning(SAXParseException ex) {
46: log.warn(getMessage(ex));
47: }
48:
49: /**
50: * @param ex The exception to create a message from
51: * @return A summary of what went wrong.
52: */
53: private static String getMessage(SAXParseException ex) {
54: if (ex.getSystemId() != null) {
55: return "SystemID=" + ex.getSystemId() + " Line="
56: + ex.getLineNumber() + ' ' + ex.getMessage();
57: }
58:
59: if (ex.getPublicId() != null) {
60: return "PublicID=" + ex.getPublicId() + " Line="
61: + ex.getLineNumber() + ' ' + ex.getMessage();
62: }
63:
64: return "Line=" + ex.getLineNumber() + ' ' + ex.getMessage();
65: }
66:
67: /**
68: * The log stream
69: */
70: private static final Log log = LogFactory
71: .getLog(LogErrorHandler.class);
72: }
|