KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > util > DefaultErrorHandler


1 /*
2  * Copyright 2000-2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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
17 package org.apache.xerces.util;
18
19 import java.io.PrintWriter JavaDoc;
20
21 import org.apache.xerces.xni.XNIException;
22 import org.apache.xerces.xni.parser.XMLErrorHandler;
23 import org.apache.xerces.xni.parser.XMLParseException;
24
25 /**
26  * Default error handler.
27  *
28  * @author Andy Clark, IBM
29  *
30  * @version $Id: DefaultErrorHandler.java,v 1.5 2004/02/24 23:15:53 mrglavas Exp $
31  */

32 public class DefaultErrorHandler
33     implements XMLErrorHandler {
34
35     //
36
// Data
37
//
38

39     /** Print writer. */
40     protected PrintWriter JavaDoc fOut;
41
42     //
43
// Constructors
44
//
45

46     /**
47      * Constructs an error handler that prints error messages to
48      * <code>System.err</code>.
49      */

50     public DefaultErrorHandler() {
51         this(new PrintWriter JavaDoc(System.err));
52     } // <init>()
53

54     /**
55      * Constructs an error handler that prints error messages to the
56      * specified <code>PrintWriter</code.
57      */

58     public DefaultErrorHandler(PrintWriter JavaDoc out) {
59         fOut = out;
60     } // <init>(PrintWriter)
61

62     //
63
// ErrorHandler methods
64
//
65

66     /** Warning. */
67     public void warning(String JavaDoc domain, String JavaDoc key, XMLParseException ex)
68         throws XNIException {
69         printError("Warning", ex);
70     } // warning(XMLParseException)
71

72     /** Error. */
73     public void error(String JavaDoc domain, String JavaDoc key, XMLParseException ex)
74         throws XNIException {
75         printError("Error", ex);
76     } // error(XMLParseException)
77

78     /** Fatal error. */
79     public void fatalError(String JavaDoc domain, String JavaDoc key, XMLParseException ex)
80         throws XNIException {
81         printError("Fatal Error", ex);
82         throw ex;
83     } // fatalError(XMLParseException)
84

85     //
86
// Private methods
87
//
88

89     /** Prints the error message. */
90     private void printError(String JavaDoc type, XMLParseException ex) {
91
92         fOut.print("[");
93         fOut.print(type);
94         fOut.print("] ");
95         String JavaDoc systemId = ex.getExpandedSystemId();
96         if (systemId != null) {
97             int index = systemId.lastIndexOf('/');
98             if (index != -1)
99                 systemId = systemId.substring(index + 1);
100             fOut.print(systemId);
101         }
102         fOut.print(':');
103         fOut.print(ex.getLineNumber());
104         fOut.print(':');
105         fOut.print(ex.getColumnNumber());
106         fOut.print(": ");
107         fOut.print(ex.getMessage());
108         fOut.println();
109         fOut.flush();
110
111     } // printError(String,SAXParseException)
112

113 } // class DefaultErrorHandler
114
Popular Tags