KickJava   Java API By Example, From Geeks To Geeks.

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

224   public void fatalError(TransformerException JavaDoc exception) throws TransformerException JavaDoc
225   {
226     // printLocation(exception);
227
// ensureLocationSet(exception);
228

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

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