View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 2000-2002,2004 The Apache Software Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * 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, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package com.sun.org.apache.xerces.internal.util;
22  
23  import java.io.PrintWriter;
24  
25  import com.sun.org.apache.xerces.internal.xni.XNIException;
26  import com.sun.org.apache.xerces.internal.xni.parser.XMLErrorHandler;
27  import com.sun.org.apache.xerces.internal.xni.parser.XMLParseException;
28  
29  /**
30   * Default error handler.
31   *
32   * @author Andy Clark, IBM
33   *
34   */
35  public class DefaultErrorHandler
36      implements XMLErrorHandler {
37  
38      //
39      // Data
40      //
41  
42      /** Print writer. */
43      protected PrintWriter fOut;
44  
45      //
46      // Constructors
47      //
48  
49      /**
50       * Constructs an error handler that prints error messages to
51       * <code>System.err</code>.
52       */
53      public DefaultErrorHandler() {
54          this(new PrintWriter(System.err));
55      } // <init>()
56  
57      /**
58       * Constructs an error handler that prints error messages to the
59       * specified <code>PrintWriter</code.
60       */
61      public DefaultErrorHandler(PrintWriter out) {
62          fOut = out;
63      } // <init>(PrintWriter)
64  
65      //
66      // ErrorHandler methods
67      //
68  
69      /** Warning. */
70      public void warning(String domain, String key, XMLParseException ex)
71          throws XNIException {
72          printError("Warning", ex);
73      } // warning(XMLParseException)
74  
75      /** Error. */
76      public void error(String domain, String key, XMLParseException ex)
77          throws XNIException {
78          printError("Error", ex);
79      } // error(XMLParseException)
80  
81      /** Fatal error. */
82      public void fatalError(String domain, String key, XMLParseException ex)
83          throws XNIException {
84          printError("Fatal Error", ex);
85          throw ex;
86      } // fatalError(XMLParseException)
87  
88      //
89      // Private methods
90      //
91  
92      /** Prints the error message. */
93      private void printError(String type, XMLParseException ex) {
94  
95          fOut.print("[");
96          fOut.print(type);
97          fOut.print("] ");
98          String systemId = ex.getExpandedSystemId();
99          if (systemId != null) {
100             int index = systemId.lastIndexOf('/');
101             if (index != -1)
102                 systemId = systemId.substring(index + 1);
103             fOut.print(systemId);
104         }
105         fOut.print(':');
106         fOut.print(ex.getLineNumber());
107         fOut.print(':');
108         fOut.print(ex.getColumnNumber());
109         fOut.print(": ");
110         fOut.print(ex.getMessage());
111         fOut.println();
112         fOut.flush();
113 
114     } // printError(String,SAXParseException)
115 
116 } // class DefaultErrorHandler