KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 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 package com.sun.org.apache.xerces.internal.util;
59
60 import com.sun.org.apache.xerces.internal.xni.XMLLocator;
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 import org.xml.sax.ErrorHandler JavaDoc;
65 import org.xml.sax.SAXException JavaDoc;
66 import org.xml.sax.SAXParseException JavaDoc;
67
68 /**
69  * This class wraps a SAX error handler in an XNI error handler.
70  *
71  * @see ErrorHandler
72  *
73  * @author Andy Clark, IBM
74  *
75  * @version $Id: ErrorHandlerWrapper.java,v 1.10 2003/12/17 08:28:29 neeraj Exp $
76  */

77 public class ErrorHandlerWrapper
78     implements XMLErrorHandler {
79
80     //
81
// Data
82
//
83

84     /** The SAX error handler. */
85     protected ErrorHandler fErrorHandler;
86
87     //
88
// Constructors
89
//
90

91     /** Default constructor. */
92     public ErrorHandlerWrapper() {}
93
94     /** Wraps the specified SAX error handler. */
95     public ErrorHandlerWrapper(ErrorHandler errorHandler) {
96         setErrorHandler(errorHandler);
97     } // <init>(ErrorHandler)
98

99     //
100
// Public methods
101
//
102

103     /** Sets the SAX error handler. */
104     public void setErrorHandler(ErrorHandler errorHandler) {
105         fErrorHandler = errorHandler;
106     } // setErrorHandler(ErrorHandler)
107

108     /** Returns the SAX error handler. */
109     public ErrorHandler getErrorHandler() {
110         return fErrorHandler;
111     } // getErrorHandler():ErrorHandler
112

113     //
114
// XMLErrorHandler methods
115
//
116

117     /**
118      * Reports a warning. Warnings are non-fatal and can be safely ignored
119      * by most applications.
120      *
121      * @param domain The domain of the warning. The domain can be any
122      * string but is suggested to be a valid URI. The
123      * domain can be used to conveniently specify a web
124      * site location of the relevent specification or
125      * document pertaining to this warning.
126      * @param key The warning key. This key can be any string and
127      * is implementation dependent.
128      * @param exception Exception.
129      *
130      * @throws XNIException Thrown to signal that the parser should stop
131      * parsing the document.
132      */

133     public void warning(String JavaDoc domain, String JavaDoc key,
134                         XMLParseException exception) throws XNIException {
135
136         if (fErrorHandler != null) {
137             SAXParseException JavaDoc saxException = createSAXParseException(exception);
138             
139             try {
140                 fErrorHandler.warning(saxException);
141             }
142             catch (SAXParseException JavaDoc e) {
143                 throw createXMLParseException(e);
144             }
145             catch (SAXException JavaDoc e) {
146                 throw createXNIException(e);
147             }
148         }
149         
150     } // warning(String,String,XMLParseException)
151

152     /**
153      * Reports an error. Errors are non-fatal and usually signify that the
154      * document is invalid with respect to its grammar(s).
155      *
156      * @param domain The domain of the error. The domain can be any
157      * string but is suggested to be a valid URI. The
158      * domain can be used to conveniently specify a web
159      * site location of the relevent specification or
160      * document pertaining to this error.
161      * @param key The error key. This key can be any string and
162      * is implementation dependent.
163      * @param exception Exception.
164      *
165      * @throws XNIException Thrown to signal that the parser should stop
166      * parsing the document.
167      */

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

187     /**
188      * Report a fatal error. Fatal errors usually occur when the document
189      * is not well-formed and signifies that the parser cannot continue
190      * normal operation.
191      * <p>
192      * <strong>Note:</strong> The error handler should <em>always</em>
193      * throw an <code>XNIException</code> from this method. This exception
194      * can either be the same exception that is passed as a parameter to
195      * the method or a new XNI exception object. If the registered error
196      * handler fails to throw an exception, the continuing operation of
197      * the parser is undetermined.
198      *
199      * @param domain The domain of the fatal error. The domain can be
200      * any 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 fatal error.
204      * @param key The fatal error key. This key can be any string
205      * and 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 fatalError(String JavaDoc domain, String JavaDoc key,
212                            XMLParseException exception) throws XNIException {
213                             
214         if (fErrorHandler != null) {
215             SAXParseException JavaDoc saxException = createSAXParseException(exception);
216             
217             try {
218                 fErrorHandler.fatalError(saxException);
219             }
220             catch (SAXParseException JavaDoc e) {
221                 throw createXMLParseException(e);
222             }
223             catch (SAXException JavaDoc e) {
224                 throw createXNIException(e);
225             }
226         }
227
228     } // fatalError(String,String,XMLParseException)
229

230     //
231
// Protected methods
232
//
233

234     /** Creates a SAXParseException from an XMLParseException. */
235     protected static SAXParseException JavaDoc createSAXParseException(XMLParseException exception) {
236         return new SAXParseException JavaDoc(exception.getMessage(),
237                                      exception.getPublicId(),
238                                      exception.getExpandedSystemId(),
239                                      exception.getLineNumber(),
240                                      exception.getColumnNumber(),
241                                      exception.getException());
242     } // createSAXParseException(XMLParseException):SAXParseException
243

244     /** Creates an XMLParseException from a SAXParseException. */
245     protected static XMLParseException createXMLParseException(SAXParseException JavaDoc exception) {
246         final String JavaDoc fPublicId = exception.getPublicId();
247         final String JavaDoc fExpandedSystemId = exception.getSystemId();
248         final int fLineNumber = exception.getLineNumber();
249         final int fColumnNumber = exception.getColumnNumber();
250         XMLLocator location = new XMLLocator() {
251             public void setPublicId(String JavaDoc id) {}
252             public String JavaDoc getPublicId() { return fPublicId; }
253             public void setExpandedSystemId( String JavaDoc id) {}
254             public String JavaDoc getExpandedSystemId() { return fExpandedSystemId; }
255             public void setBaseSystemId(String JavaDoc id) {}
256             public String JavaDoc getBaseSystemId() { return null; }
257             public void setLiteralSystemId(String JavaDoc id) {}
258             public String JavaDoc getLiteralSystemId() { return null; }
259             public int getColumnNumber() { return fColumnNumber; }
260             public void setColumnNumber(int col) {}
261             public int getLineNumber() { return fLineNumber; }
262             public void setLineNumber(int line) {}
263             public String JavaDoc getEncoding() { return null; }
264         };
265         return new XMLParseException(location, exception.getMessage(),exception);
266     } // createXMLParseException(SAXParseException):XMLParseException
267

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

271     protected static XNIException createXNIException(SAXException JavaDoc exception) {
272         return new XNIException(exception.getMessage(),exception);
273     } // createXNIException(SAXException):XMLParseException
274
} // class ErrorHandlerWrapper
275
Popular Tags