KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001-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 org.apache.xerces.xni.XMLLocator;
20 import org.apache.xerces.xni.XNIException;
21 import org.apache.xerces.xni.parser.XMLErrorHandler;
22 import org.apache.xerces.xni.parser.XMLParseException;
23 import org.xml.sax.ErrorHandler JavaDoc;
24 import org.xml.sax.SAXException JavaDoc;
25 import org.xml.sax.SAXParseException JavaDoc;
26
27 /**
28  * This class wraps a SAX error handler in an XNI error handler.
29  *
30  * @see ErrorHandler
31  *
32  * @author Andy Clark, IBM
33  *
34  * @version $Id: ErrorHandlerWrapper.java,v 1.12 2004/09/01 03:06:08 mrglavas Exp $
35  */

36 public class ErrorHandlerWrapper
37     implements XMLErrorHandler {
38
39     //
40
// Data
41
//
42

43     /** The SAX error handler. */
44     protected ErrorHandler fErrorHandler;
45
46     //
47
// Constructors
48
//
49

50     /** Default constructor. */
51     public ErrorHandlerWrapper() {}
52
53     /** Wraps the specified SAX error handler. */
54     public ErrorHandlerWrapper(ErrorHandler errorHandler) {
55         setErrorHandler(errorHandler);
56     } // <init>(ErrorHandler)
57

58     //
59
// Public methods
60
//
61

62     /** Sets the SAX error handler. */
63     public void setErrorHandler(ErrorHandler errorHandler) {
64         fErrorHandler = errorHandler;
65     } // setErrorHandler(ErrorHandler)
66

67     /** Returns the SAX error handler. */
68     public ErrorHandler getErrorHandler() {
69         return fErrorHandler;
70     } // getErrorHandler():ErrorHandler
71

72     //
73
// XMLErrorHandler methods
74
//
75

76     /**
77      * Reports a warning. Warnings are non-fatal and can be safely ignored
78      * by most applications.
79      *
80      * @param domain The domain of the warning. The domain can be any
81      * string but is suggested to be a valid URI. The
82      * domain can be used to conveniently specify a web
83      * site location of the relevent specification or
84      * document pertaining to this warning.
85      * @param key The warning key. This key can be any string and
86      * is implementation dependent.
87      * @param exception Exception.
88      *
89      * @throws XNIException Thrown to signal that the parser should stop
90      * parsing the document.
91      */

92     public void warning(String JavaDoc domain, String JavaDoc key,
93                         XMLParseException exception) throws XNIException {
94
95         if (fErrorHandler != null) {
96             SAXParseException JavaDoc saxException = createSAXParseException(exception);
97             
98             try {
99                 fErrorHandler.warning(saxException);
100             }
101             catch (SAXParseException JavaDoc e) {
102                 throw createXMLParseException(e);
103             }
104             catch (SAXException JavaDoc e) {
105                 throw createXNIException(e);
106             }
107         }
108         
109     } // warning(String,String,XMLParseException)
110

111     /**
112      * Reports an error. Errors are non-fatal and usually signify that the
113      * document is invalid with respect to its grammar(s).
114      *
115      * @param domain The domain of the error. The domain can be any
116      * string but is suggested to be a valid URI. The
117      * domain can be used to conveniently specify a web
118      * site location of the relevent specification or
119      * document pertaining to this error.
120      * @param key The error key. This key can be any string and
121      * is implementation dependent.
122      * @param exception Exception.
123      *
124      * @throws XNIException Thrown to signal that the parser should stop
125      * parsing the document.
126      */

127     public void error(String JavaDoc domain, String JavaDoc key,
128                       XMLParseException exception) throws XNIException {
129         
130         if (fErrorHandler != null) {
131             SAXParseException JavaDoc saxException = createSAXParseException(exception);
132             
133             try {
134                 fErrorHandler.error(saxException);
135             }
136             catch (SAXParseException JavaDoc e) {
137                 throw createXMLParseException(e);
138             }
139             catch (SAXException JavaDoc e) {
140                 throw createXNIException(e);
141             }
142         }
143
144     } // error(String,String,XMLParseException)
145

146     /**
147      * Report a fatal error. Fatal errors usually occur when the document
148      * is not well-formed and signifies that the parser cannot continue
149      * normal operation.
150      * <p>
151      * <strong>Note:</strong> The error handler should <em>always</em>
152      * throw an <code>XNIException</code> from this method. This exception
153      * can either be the same exception that is passed as a parameter to
154      * the method or a new XNI exception object. If the registered error
155      * handler fails to throw an exception, the continuing operation of
156      * the parser is undetermined.
157      *
158      * @param domain The domain of the fatal error. The domain can be
159      * any string but is suggested to be a valid URI. The
160      * domain can be used to conveniently specify a web
161      * site location of the relevent specification or
162      * document pertaining to this fatal error.
163      * @param key The fatal error key. This key can be any string
164      * and is implementation dependent.
165      * @param exception Exception.
166      *
167      * @throws XNIException Thrown to signal that the parser should stop
168      * parsing the document.
169      */

170     public void fatalError(String JavaDoc domain, String JavaDoc key,
171                            XMLParseException exception) throws XNIException {
172                             
173         if (fErrorHandler != null) {
174             SAXParseException JavaDoc saxException = createSAXParseException(exception);
175             
176             try {
177                 fErrorHandler.fatalError(saxException);
178             }
179             catch (SAXParseException JavaDoc e) {
180                 throw createXMLParseException(e);
181             }
182             catch (SAXException JavaDoc e) {
183                 throw createXNIException(e);
184             }
185         }
186
187     } // fatalError(String,String,XMLParseException)
188

189     //
190
// Protected methods
191
//
192

193     /** Creates a SAXParseException from an XMLParseException. */
194     protected static SAXParseException JavaDoc createSAXParseException(XMLParseException exception) {
195         return new SAXParseException JavaDoc(exception.getMessage(),
196                                      exception.getPublicId(),
197                                      exception.getExpandedSystemId(),
198                                      exception.getLineNumber(),
199                                      exception.getColumnNumber(),
200                                      exception.getException());
201     } // createSAXParseException(XMLParseException):SAXParseException
202

203     /** Creates an XMLParseException from a SAXParseException. */
204     protected static XMLParseException createXMLParseException(SAXParseException JavaDoc exception) {
205         final String JavaDoc fPublicId = exception.getPublicId();
206         final String JavaDoc fExpandedSystemId = exception.getSystemId();
207         final int fLineNumber = exception.getLineNumber();
208         final int fColumnNumber = exception.getColumnNumber();
209         XMLLocator location = new XMLLocator() {
210             public String JavaDoc getPublicId() { return fPublicId; }
211             public String JavaDoc getExpandedSystemId() { return fExpandedSystemId; }
212             public String JavaDoc getBaseSystemId() { return null; }
213             public String JavaDoc getLiteralSystemId() { return null; }
214             public int getColumnNumber() { return fColumnNumber; }
215             public int getLineNumber() { return fLineNumber; }
216             public int getCharacterOffset() { return -1; }
217             public String JavaDoc getEncoding() { return null; }
218             public String JavaDoc getXMLVersion() { return null; }
219         };
220         return new XMLParseException(location, exception.getMessage(),exception);
221     } // createXMLParseException(SAXParseException):XMLParseException
222

223     /** Creates an XNIException from a SAXException.
224         NOTE: care should be taken *not* to call this with a SAXParseException; this will
225         lose information!!! */

226     protected static XNIException createXNIException(SAXException JavaDoc exception) {
227         return new XNIException(exception.getMessage(),exception);
228     } // createXNIException(SAXException):XMLParseException
229
} // class ErrorHandlerWrapper
230
Popular Tags