KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > util > DOMErrorHandlerWrapper


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001, 2002 The Apache Software Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58
59 package com.sun.org.apache.xerces.internal.util;
60
61 import com.sun.org.apache.xerces.internal.xni.XNIException;
62 import com.sun.org.apache.xerces.internal.xni.parser.XMLErrorHandler;
63 import com.sun.org.apache.xerces.internal.xni.parser.XMLParseException;
64                              
65 import org.w3c.dom.Node JavaDoc;
66 import org.w3c.dom.DOMError JavaDoc;
67 import org.w3c.dom.DOMLocator JavaDoc;
68 import org.w3c.dom.DOMErrorHandler JavaDoc;
69 import com.sun.org.apache.xerces.internal.dom.DOMErrorImpl;
70 import com.sun.org.apache.xerces.internal.dom.DOMLocatorImpl;
71 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter;
72
73 import java.io.PrintWriter JavaDoc;
74 import java.util.Hashtable JavaDoc;
75
76 /**
77  * This class handles DOM errors .
78  *
79  * @see DOMErrorHandler
80  *
81  * @author Gopal Sharma, SUN Microsystems Inc.
82  * @version $Id: DOMErrorHandlerWrapper.java,v 1.12 2004/04/23 04:40:38 mrglavas Exp $
83  */

84
85 // REVISIT: current implementations wraps error several times:
86
// XMLErrorReport.reportError creates XMLParserException (by wrapping all info)
87
// and goes via switch to send errors.
88
// DOMErrorHandlerWrapper catches calls, copies info from XMLParserException and
89
// sends one call back to the application
90
// I think we can avoid this indirection if we modify XMLErrorReporter. --el
91

92 public class DOMErrorHandlerWrapper
93     implements XMLErrorHandler, DOMErrorHandler JavaDoc {
94
95     /** Map for converting internal error codes to DOM error types. **/
96     private static Hashtable JavaDoc fgDOMErrorTypeTable;
97     
98     // It keeps the reference of DOMErrorHandler of application
99
protected DOMErrorHandler JavaDoc fDomErrorHandler;
100
101     // Error Status
102
boolean eStatus = true ;
103
104     // Print writer
105
protected PrintWriter JavaDoc fOut;
106
107     // some components may set error node
108
// @see DOMNormalizer.
109
public Node JavaDoc fCurrentNode;
110
111     /** Error code for comparisons. **/
112     protected final XMLErrorCode fErrorCode = new XMLErrorCode(null, null);
113     
114     protected final DOMErrorImpl fDOMError = new DOMErrorImpl();
115     
116     static {
117         // initialize error type table: internal error codes (represented by domain and key) need to be mapped to a DOM error type.
118
fgDOMErrorTypeTable = new Hashtable JavaDoc();
119         fgDOMErrorTypeTable.put(new XMLErrorCode(XMLMessageFormatter.XML_DOMAIN, "DoctypeNotAllowed"), "doctype-not-allowed");
120         fgDOMErrorTypeTable.put(new XMLErrorCode(XMLMessageFormatter.XML_DOMAIN, "ElementUnterminated"), "wf-invalid-character-in-node-name");
121         fgDOMErrorTypeTable.put(new XMLErrorCode(XMLMessageFormatter.XML_DOMAIN, "EncodingDeclInvalid"), "unsupported-encoding");
122         fgDOMErrorTypeTable.put(new XMLErrorCode(XMLMessageFormatter.XML_DOMAIN, "EqRequiredInAttribute"), "wf-invalid-character-in-node-name");
123         fgDOMErrorTypeTable.put(new XMLErrorCode(XMLMessageFormatter.XML_DOMAIN, "LessthanInAttValue"), "wf-invalid-character");
124     }
125
126     //
127
// Constructors
128
//
129

130     // Default constructor /
131

132     public DOMErrorHandlerWrapper() {
133         fOut = new PrintWriter JavaDoc(System.err);
134     }
135
136
137     public DOMErrorHandlerWrapper(DOMErrorHandler JavaDoc domErrorHandler) {
138         fDomErrorHandler = domErrorHandler;
139     } // DOMErrorHandlerWrapper(DOMErrorHandler domErrorHandler)
140

141
142     //
143
// Public methods
144
//
145

146     /** Sets the DOM error handler. */
147     public void setErrorHandler(DOMErrorHandler JavaDoc errorHandler) {
148         fDomErrorHandler = errorHandler;
149     } // setErrorHandler(ErrorHandler)
150

151
152     public DOMErrorHandler JavaDoc getErrorHandler(){
153         return fDomErrorHandler;
154     } //getErrorHandler()
155

156     //
157
// XMLErrorHandler methods
158
//
159

160     /**
161      * Reports a warning. Warnings are non-fatal and can be safely ignored
162      * by most applications.
163      *
164      * @param domain The domain of the warning. The domain can be any
165      * string but is suggested to be a valid URI. The
166      * domain can be used to conveniently specify a web
167      * site location of the relevent specification or
168      * document pertaining to this warning.
169      * @param key The warning key. This key can be any string and
170      * is implementation dependent.
171      * @param exception Exception.
172      *
173      * @throws XNIException Thrown to signal that the parser should stop
174      * parsing the document.
175      */

176
177     public void warning(String JavaDoc domain, String JavaDoc key,
178                         XMLParseException exception) throws XNIException {
179         fDOMError.fSeverity = DOMError.SEVERITY_WARNING;
180         fDOMError.fException = exception;
181         fDOMError.fType = key;
182         fDOMError.fRelatedData = fDOMError.fMessage = exception.getMessage();
183         DOMLocatorImpl locator = fDOMError.fLocator;
184         if (locator != null) {
185             locator.fColumnNumber = exception.getColumnNumber();
186             locator.fLineNumber = exception.getLineNumber();
187             locator.fUri = exception.getExpandedSystemId();
188             locator.fRelatedNode = fCurrentNode;
189         }
190         if (fDomErrorHandler != null) {
191             fDomErrorHandler.handleError(fDOMError);
192         }
193     } // warning(String,String,XMLParseException)
194

195     /**
196      * Reports an error. Errors are non-fatal and usually signify that the
197      * document is invalid with respect to its grammar(s).
198      *
199      * @param domain The domain of the error. The domain can be any
200      * string but is suggested to be a valid URI. The
201      * domain can be used to conveniently specify a web
202      * site location of the relevent specification or
203      * document pertaining to this error.
204      * @param key The error key. This key can be any string and
205      * is implementation dependent.
206      * @param exception Exception.
207      *
208      * @throws XNIException Thrown to signal that the parser should stop
209      * parsing the document.
210      */

211     public void error(String JavaDoc domain, String JavaDoc key,
212                       XMLParseException exception) throws XNIException {
213         fDOMError.fSeverity = DOMError.SEVERITY_ERROR;
214         fDOMError.fException = exception;
215         fDOMError.fType = key;
216         fDOMError.fRelatedData = fDOMError.fMessage = exception.getMessage();
217         DOMLocatorImpl locator = fDOMError.fLocator;
218         if (locator != null) {
219             locator.fColumnNumber = exception.getColumnNumber();
220             locator.fLineNumber = exception.getLineNumber();
221             locator.fUri = exception.getExpandedSystemId();
222             locator.fRelatedNode= fCurrentNode;
223         }
224         if (fDomErrorHandler != null) {
225             fDomErrorHandler.handleError(fDOMError);
226         }
227     } // error(String,String,XMLParseException)
228

229     /**
230      * Report a fatal error. Fatal errors usually occur when the document
231      * is not well-formed and signifies that the parser cannot continue
232      * normal operation.
233      * <p>
234      * <strong>Note:</strong> The error handler should <em>always</em>
235      * throw an <code>XNIException</code> from this method. This exception
236      * can either be the same exception that is passed as a parameter to
237      * the method or a new XNI exception object. If the registered error
238      * handler fails to throw an exception, the continuing operation of
239      * the parser is undetermined.
240      *
241      * @param domain The domain of the fatal error. The domain can be
242      * any string but is suggested to be a valid URI. The
243      * domain can be used to conveniently specify a web
244      * site location of the relevent specification or
245      * document pertaining to this fatal error.
246      * @param key The fatal error key. This key can be any string
247      * and is implementation dependent.
248      * @param exception Exception.
249      *
250      * @throws XNIException Thrown to signal that the parser should stop
251      * parsing the document.
252      */

253     public void fatalError(String JavaDoc domain, String JavaDoc key,
254                            XMLParseException exception) throws XNIException {
255         fDOMError.fSeverity = DOMError.SEVERITY_FATAL_ERROR;
256         fDOMError.fException = exception;
257         fErrorCode.setValues(domain, key);
258         String JavaDoc domErrorType = (String JavaDoc) fgDOMErrorTypeTable.get(fErrorCode);
259         fDOMError.fType = (domErrorType != null) ? domErrorType : key;
260         fDOMError.fRelatedData = fDOMError.fMessage = exception.getMessage();
261         DOMLocatorImpl locator = fDOMError.fLocator;
262         if (locator != null) {
263             locator.fColumnNumber = exception.getColumnNumber();
264             locator.fLineNumber = exception.getLineNumber();
265             locator.fUri = exception.getExpandedSystemId();
266             locator.fRelatedNode = fCurrentNode;
267         }
268         if (fDomErrorHandler != null) {
269             fDomErrorHandler.handleError(fDOMError);
270         }
271     } // fatalError(String,String,XMLParseException)
272

273
274     public boolean handleError(DOMError JavaDoc error) {
275         printError(error);
276         return eStatus;
277     }
278
279     /** Prints the error message. */
280
281     private void printError(DOMError JavaDoc error) {
282         int severity = error.getSeverity();
283         fOut.print("[");
284         if ( severity == DOMError.SEVERITY_WARNING) {
285             fOut.print("Warning");
286         } else if ( severity == DOMError.SEVERITY_ERROR) {
287             fOut.print("Error");
288         } else {
289             fOut.print("FatalError");
290             eStatus = false ; //REVISIT: Abort processing if fatal error, do we need to??
291
}
292         fOut.print("] ");
293         DOMLocator JavaDoc locator = error.getLocation();
294         if (locator != null) {
295             fOut.print(locator.getLineNumber());
296             fOut.print(":");
297             fOut.print(locator.getColumnNumber());
298             fOut.print(":");
299             fOut.print(locator.getByteOffset());
300             fOut.print(",");
301             fOut.print(locator.getUtf16Offset());
302             Node JavaDoc node = locator.getRelatedNode();
303             if (node != null) {
304                 fOut.print("[");
305                 fOut.print(node.getNodeName());
306                 fOut.print("]");
307             }
308             String JavaDoc systemId = locator.getUri();
309             if (systemId != null) {
310                 int index = systemId.lastIndexOf('/');
311                 if (index != -1)
312                     systemId = systemId.substring(index + 1);
313                 fOut.print(": ");
314                 fOut.print(systemId);
315             }
316
317         }
318
319         fOut.print(":");
320         fOut.print(error.getMessage());
321         fOut.println();
322         fOut.flush();
323
324     } // printError(DOMError)
325

326 } // class DOMErrorHandlerWrapper
327
Popular Tags