KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > xml > xslt > TraxErrorHandler


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.excalibur.xml.xslt;
18
19 import javax.xml.transform.ErrorListener JavaDoc;
20 import javax.xml.transform.SourceLocator JavaDoc;
21 import javax.xml.transform.TransformerException JavaDoc;
22
23 import org.apache.avalon.framework.logger.Logger;
24
25 /**
26  * This ErrorListener simply logs the exception and in
27  * case of an fatal-error the exception is rethrown.
28  * Warnings and errors are ignored.
29  *
30  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
31  * @version CVS $Id: TraxErrorHandler.java,v 1.4 2004/02/28 11:47:16 cziegeler Exp $
32  */

33 class TraxErrorHandler
34     implements ErrorListener JavaDoc
35 {
36     private Logger m_logger;
37
38     TraxErrorHandler( final Logger logger )
39     {
40         if( null == logger )
41         {
42             throw new NullPointerException JavaDoc( "logger" );
43         }
44         m_logger = logger;
45     }
46
47     public void warning( final TransformerException JavaDoc te )
48         throws TransformerException JavaDoc
49     {
50         final String JavaDoc message = getMessage( te );
51         if( null != m_logger )
52         {
53             m_logger.warn( message, te );
54         }
55         else
56         {
57             System.out.println( "WARNING: " + message );
58         }
59     }
60
61     public void error( final TransformerException JavaDoc te )
62         throws TransformerException JavaDoc
63     {
64         final String JavaDoc message = getMessage( te );
65         if( null != m_logger )
66         {
67             m_logger.error( message, te );
68         }
69         else
70         {
71             System.out.println( "ERROR: " + message );
72         }
73     }
74
75     public void fatalError( final TransformerException JavaDoc te )
76         throws TransformerException JavaDoc
77     {
78         final String JavaDoc message = getMessage( te );
79         if( null != m_logger )
80         {
81             m_logger.fatalError( message, te );
82         }
83         else
84         {
85             System.out.println( "FATAL-ERROR: " + message );
86         }
87         throw te;
88     }
89
90     private String JavaDoc getMessage( final TransformerException JavaDoc te )
91     {
92         final SourceLocator JavaDoc locator = te.getLocator();
93         if( null != locator )
94         {
95             // System.out.println("Parser fatal error: "+exception.getMessage());
96
final String JavaDoc id =
97                 ( locator.getPublicId() != locator.getPublicId() )
98                 ? locator.getPublicId()
99                 : ( null != locator.getSystemId() )
100                 ? locator.getSystemId() : "SystemId Unknown";
101             return new StringBuffer JavaDoc( "Error in TraxTransformer: " )
102                 .append( id ).append( "; Line " ).append( locator.getLineNumber() )
103                 .append( "; Column " ).append( locator.getColumnNumber() )
104                 .append( "; " ).toString();
105         }
106         return "Error in TraxTransformer: " + te;
107     }
108 }
109
Popular Tags