KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xml > utils > DefaultErrorHandler


1 /*
2  * Copyright 1999-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  * $Id: DefaultErrorHandler.java,v 1.15 2004/02/17 04:21:14 minchau Exp $
18  */

19 package org.apache.xml.utils;
20
21 import java.io.PrintStream JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23
24 import javax.xml.transform.ErrorListener JavaDoc;
25 import javax.xml.transform.SourceLocator JavaDoc;
26 import javax.xml.transform.TransformerException JavaDoc;
27
28 import org.apache.xml.res.XMLErrorResources;
29 import org.apache.xml.res.XMLMessages;
30
31 import org.xml.sax.ErrorHandler JavaDoc;
32 import org.xml.sax.SAXException JavaDoc;
33 import org.xml.sax.SAXParseException JavaDoc;
34
35
36 /**
37  * Implement SAX error handler for default reporting.
38  * @xsl.usage general
39  */

40 public class DefaultErrorHandler implements ErrorHandler JavaDoc, ErrorListener JavaDoc
41 {
42   PrintWriter JavaDoc m_pw;
43
44   /**
45    * Constructor DefaultErrorHandler
46    */

47   public DefaultErrorHandler(PrintWriter JavaDoc pw)
48   {
49     m_pw = pw;
50   }
51   
52   /**
53    * Constructor DefaultErrorHandler
54    */

55   public DefaultErrorHandler(PrintStream JavaDoc pw)
56   {
57     m_pw = new PrintWriter JavaDoc(pw, true);
58   }
59   
60   /**
61    * Constructor DefaultErrorHandler
62    */

63   public DefaultErrorHandler()
64   {
65     m_pw = new PrintWriter JavaDoc(System.err, true);
66   }
67
68
69   /**
70    * Receive notification of a warning.
71    *
72    * <p>SAX parsers will use this method to report conditions that
73    * are not errors or fatal errors as defined by the XML 1.0
74    * recommendation. The default behaviour is to take no action.</p>
75    *
76    * <p>The SAX parser must continue to provide normal parsing events
77    * after invoking this method: it should still be possible for the
78    * application to process the document through to the end.</p>
79    *
80    * @param exception The warning information encapsulated in a
81    * SAX parse exception.
82    * @throws SAXException Any SAX exception, possibly
83    * wrapping another exception.
84    */

85   public void warning(SAXParseException JavaDoc exception) throws SAXException JavaDoc
86   {
87     printLocation(m_pw, exception);
88     m_pw.println("Parser warning: " + exception.getMessage());
89   }
90
91   /**
92    * Receive notification of a recoverable error.
93    *
94    * <p>This corresponds to the definition of "error" in section 1.2
95    * of the W3C XML 1.0 Recommendation. For example, a validating
96    * parser would use this callback to report the violation of a
97    * validity constraint. The default behaviour is to take no
98    * action.</p>
99    *
100    * <p>The SAX parser must continue to provide normal parsing events
101    * after invoking this method: it should still be possible for the
102    * application to process the document through to the end. If the
103    * application cannot do so, then the parser should report a fatal
104    * error even if the XML 1.0 recommendation does not require it to
105    * do so.</p>
106    *
107    * @param exception The error information encapsulated in a
108    * SAX parse exception.
109    * @throws SAXException Any SAX exception, possibly
110    * wrapping another exception.
111    */

112   public void error(SAXParseException JavaDoc exception) throws SAXException JavaDoc
113   {
114     //printLocation(exception);
115
// m_pw.println(exception.getMessage());
116

117     throw exception;
118   }
119
120   /**
121    * Receive notification of a non-recoverable error.
122    *
123    * <p>This corresponds to the definition of "fatal error" in
124    * section 1.2 of the W3C XML 1.0 Recommendation. For example, a
125    * parser would use this callback to report the violation of a
126    * well-formedness constraint.</p>
127    *
128    * <p>The application must assume that the document is unusable
129    * after the parser has invoked this method, and should continue
130    * (if at all) only for the sake of collecting addition error
131    * messages: in fact, SAX parsers are free to stop reporting any
132    * other events once this method has been invoked.</p>
133    *
134    * @param exception The error information encapsulated in a
135    * SAX parse exception.
136    * @throws SAXException Any SAX exception, possibly
137    * wrapping another exception.
138    */

139   public void fatalError(SAXParseException JavaDoc exception) throws SAXException JavaDoc
140   {
141     // printLocation(exception);
142
// m_pw.println(exception.getMessage());
143

144     throw exception;
145   }
146   
147   /**
148    * Receive notification of a warning.
149    *
150    * <p>SAX parsers will use this method to report conditions that
151    * are not errors or fatal errors as defined by the XML 1.0
152    * recommendation. The default behaviour is to take no action.</p>
153    *
154    * <p>The SAX parser must continue to provide normal parsing events
155    * after invoking this method: it should still be possible for the
156    * application to process the document through to the end.</p>
157    *
158    * @param exception The warning information encapsulated in a
159    * SAX parse exception.
160    * @throws javax.xml.transform.TransformerException Any SAX exception, possibly
161    * wrapping another exception.
162    * @see javax.xml.transform.TransformerException
163    */

164   public void warning(TransformerException JavaDoc exception) throws TransformerException JavaDoc
165   {
166     printLocation(m_pw, exception);
167
168     m_pw.println(exception.getMessage());
169   }
170
171   /**
172    * Receive notification of a recoverable error.
173    *
174    * <p>This corresponds to the definition of "error" in section 1.2
175    * of the W3C XML 1.0 Recommendation. For example, a validating
176    * parser would use this callback to report the violation of a
177    * validity constraint. The default behaviour is to take no
178    * action.</p>
179    *
180    * <p>The SAX parser must continue to provide normal parsing events
181    * after invoking this method: it should still be possible for the
182    * application to process the document through to the end. If the
183    * application cannot do so, then the parser should report a fatal
184    * error even if the XML 1.0 recommendation does not require it to
185    * do so.</p>
186    *
187    * @param exception The error information encapsulated in a
188    * SAX parse exception.
189    * @throws javax.xml.transform.TransformerException Any SAX exception, possibly
190    * wrapping another exception.
191    * @see javax.xml.transform.TransformerException
192    */

193   public void error(TransformerException JavaDoc exception) throws TransformerException JavaDoc
194   {
195     // printLocation(exception);
196
// ensureLocationSet(exception);
197

198     throw exception;
199   }
200
201   /**
202    * Receive notification of a non-recoverable error.
203    *
204    * <p>This corresponds to the definition of "fatal error" in
205    * section 1.2 of the W3C XML 1.0 Recommendation. For example, a
206    * parser would use this callback to report the violation of a
207    * well-formedness constraint.</p>
208    *
209    * <p>The application must assume that the document is unusable
210    * after the parser has invoked this method, and should continue
211    * (if at all) only for the sake of collecting addition error
212    * messages: in fact, SAX parsers are free to stop reporting any
213    * other events once this method has been invoked.</p>
214    *
215    * @param exception The error information encapsulated in a
216    * SAX parse exception.
217    * @throws javax.xml.transform.TransformerException Any SAX exception, possibly
218    * wrapping another exception.
219    * @see javax.xml.transform.TransformerException
220    */

221   public void fatalError(TransformerException JavaDoc exception) throws TransformerException JavaDoc
222   {
223     // printLocation(exception);
224
// ensureLocationSet(exception);
225

226     throw exception;
227   }
228   
229   public static void ensureLocationSet(TransformerException JavaDoc exception)
230   {
231     // SourceLocator locator = exception.getLocator();
232
SourceLocator JavaDoc locator = null;
233     Throwable JavaDoc cause = exception;
234     
235     // Try to find the locator closest to the cause.
236
do
237     {
238       if(cause instanceof SAXParseException JavaDoc)
239       {
240         locator = new SAXSourceLocator((SAXParseException JavaDoc)cause);
241       }
242       else if (cause instanceof TransformerException JavaDoc)
243       {
244         SourceLocator JavaDoc causeLocator = ((TransformerException JavaDoc)cause).getLocator();
245         if(null != causeLocator)
246           locator = causeLocator;
247       }
248       
249       if(cause instanceof TransformerException JavaDoc)
250         cause = ((TransformerException JavaDoc)cause).getCause();
251       else if(cause instanceof SAXException JavaDoc)
252         cause = ((SAXException JavaDoc)cause).getException();
253       else
254         cause = null;
255     }
256     while(null != cause);
257     
258     exception.setLocator(locator);
259   }
260   
261   public static void printLocation(PrintStream JavaDoc pw, TransformerException JavaDoc exception)
262   {
263     printLocation(new PrintWriter JavaDoc(pw), exception);
264   }
265   
266   public static void printLocation(java.io.PrintStream JavaDoc pw, org.xml.sax.SAXParseException JavaDoc exception)
267   {
268     printLocation(new PrintWriter JavaDoc(pw), exception);
269   }
270   
271   public static void printLocation(PrintWriter JavaDoc pw, Throwable JavaDoc exception)
272   {
273     SourceLocator JavaDoc locator = null;
274     Throwable JavaDoc cause = exception;
275     
276     // Try to find the locator closest to the cause.
277
do
278     {
279       if(cause instanceof SAXParseException JavaDoc)
280       {
281         locator = new SAXSourceLocator((SAXParseException JavaDoc)cause);
282       }
283       else if (cause instanceof TransformerException JavaDoc)
284       {
285         SourceLocator JavaDoc causeLocator = ((TransformerException JavaDoc)cause).getLocator();
286         if(null != causeLocator)
287           locator = causeLocator;
288       }
289       if(cause instanceof TransformerException JavaDoc)
290         cause = ((TransformerException JavaDoc)cause).getCause();
291       else if(cause instanceof WrappedRuntimeException)
292         cause = ((WrappedRuntimeException)cause).getException();
293       else if(cause instanceof SAXException JavaDoc)
294         cause = ((SAXException JavaDoc)cause).getException();
295       else
296         cause = null;
297     }
298     while(null != cause);
299         
300     if(null != locator)
301     {
302       // m_pw.println("Parser fatal error: "+exception.getMessage());
303
String JavaDoc id = (null != locator.getPublicId() )
304                   ? locator.getPublicId()
305                     : (null != locator.getSystemId())
306                       ? locator.getSystemId() : XMLMessages.createXMLMessage(XMLErrorResources.ER_SYSTEMID_UNKNOWN, null); //"SystemId Unknown";
307

308       pw.print(id + "; " +XMLMessages.createXMLMessage("line", null) + locator.getLineNumber()
309                          + "; " +XMLMessages.createXMLMessage("column", null) + locator.getColumnNumber()+"; ");
310     }
311     else
312       pw.print("("+XMLMessages.createXMLMessage(XMLErrorResources.ER_LOCATION_UNKNOWN, null)+")");
313   }
314 }
315
Popular Tags