KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > xslt > TraxErrorListener


1 /*
2  * Copyright 2005 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 package org.apache.cocoon.components.xslt;
17
18 import javax.xml.transform.ErrorListener JavaDoc;
19 import javax.xml.transform.TransformerException JavaDoc;
20
21 import org.apache.avalon.framework.logger.Logger;
22 import org.apache.cocoon.util.location.Location;
23 import org.apache.cocoon.util.location.LocationUtils;
24
25 /**
26  * A smart error listener for <code>javax.xml.tranform</code> that does its best to provide
27  * useful error messages.
28  *
29  * @version $Id: TraxErrorListener.java 326716 2005-10-19 21:44:41Z sylvain $
30  * @since 2.1.8
31  */

32 public class TraxErrorListener implements ErrorListener JavaDoc{
33
34     private Logger logger;
35     private String JavaDoc uri;
36     
37     /** The exception we had from warning() */
38     private TransformerException JavaDoc warningEx;
39     
40     /** The exception we had from error() or fatalError() */
41     private TransformerException JavaDoc exception;
42
43     public TraxErrorListener(Logger logger, String JavaDoc uri) {
44         this.logger = logger;
45         this.uri = uri;
46     }
47
48     /**
49      * Get the exception that was catched by this listener, if any.
50      *
51      * @return the exception
52      */

53     public Throwable JavaDoc getThrowable() {
54         if (exception == null) {
55             return null;
56         }
57         
58         Location loc = LocationUtils.getLocation(exception);
59         if (LocationUtils.isKnown(loc)) {
60             // Has a location: don't loose this precious information!
61
return exception;
62         }
63         
64         // No location: if it's just a wrapper, consider only the wrapped exception
65
if (exception.getCause() != null) {
66             return exception.getCause();
67         }
68         
69         // That's the actual exception!
70
return exception;
71     }
72
73     public void warning(TransformerException JavaDoc ex) throws TransformerException JavaDoc {
74         // TODO: We may want here to allow some special formatting of the messages, such as
75
// "DEBUG:A debug message" or "INFO:Transforming <foo> in mode 'bar'" to use the different
76
// log levels. This can include also deprecation logs for system-defined stylesheets
77
// using "DEPRECATED:WARN:Styling 'foo' is replaced by 'bar'".
78

79         if (logger.isWarnEnabled()) {
80             Location loc = LocationUtils.getLocation(ex);
81             logger.warn(ex.getMessage() + " at "+ loc == null ? uri : loc.toString());
82         }
83         // Keep the warning (see below)
84
warningEx = ex;
85     }
86
87     public void error(TransformerException JavaDoc ex) throws TransformerException JavaDoc {
88
89         // If we had a warning previoulsy, and the current exception has no cause, then use the warning.
90
// This is how Xalan behaves on <xsl:message terminate="yes">: it first issues a warning with all
91
// the useful information, then a useless "stylesheed directed termination" error.
92
if (warningEx != null && ex.getCause() == null) {
93             ex = warningEx;
94         }
95         warningEx = null;
96
97         // Keep the exception for later use.
98
exception = ex;
99         // and rethrow it
100
throw ex;
101     }
102
103     public void fatalError(TransformerException JavaDoc ex) throws TransformerException JavaDoc {
104         if (warningEx != null && ex.getCause() == null) {
105             ex = warningEx;
106         }
107         warningEx = null;
108
109         exception = ex;
110         throw ex;
111     }
112 }
113
Popular Tags