KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > CascadingIOException


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 package org.apache.cocoon;
17
18 import java.io.IOException JavaDoc;
19 import java.io.PrintStream JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21
22 import org.apache.avalon.framework.CascadingThrowable;
23 import org.xml.sax.SAXParseException JavaDoc;
24 import javax.xml.transform.TransformerException JavaDoc;
25 import javax.xml.transform.SourceLocator JavaDoc;
26
27 /**
28  * This is a wrapping IOException.
29  *
30  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
31  * @version CVS $Id: CascadingIOException.java 30932 2004-07-29 17:35:38Z vgritsenko $
32  */

33
34 public class CascadingIOException
35 extends IOException JavaDoc
36 implements CascadingThrowable {
37
38     /**
39      * The Throwable that caused this exception to be thrown.
40      */

41     private final Throwable JavaDoc m_throwable;
42
43
44     /**
45      * Construct a new <code>ProcessingException</code> instance.
46      */

47     public CascadingIOException(String JavaDoc message) {
48         this(message, null);
49     }
50     
51     /**
52      * Creates a new <code>ProcessingException</code> instance.
53      *
54      * @param ex an <code>Exception</code> value
55      */

56     public CascadingIOException(Exception JavaDoc ex) {
57         this(ex.getMessage(), ex);
58     }
59     
60     /**
61      * Construct a new <code>ProcessingException</code> that references
62      * a parent Exception.
63      */

64     public CascadingIOException(String JavaDoc message, Throwable JavaDoc t) {
65         super( message );
66         this.m_throwable = t;
67     }
68     
69     /**
70      * Retrieve root cause of the exception.
71      *
72      * @return the root cause
73      */

74     public final Throwable JavaDoc getCause()
75     {
76         return this.m_throwable;
77     }
78
79     public String JavaDoc toString() {
80         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
81         s.append(super.toString());
82         final Throwable JavaDoc t = getCause();
83         if(t!=null) {
84             s.append(": ");
85             // be more verbose try to get location info
86
s.append( extraInfo(t) );
87             s.append(t.toString());
88         }
89         return s.toString();
90     }
91     
92     /**
93      * Examine Throwable and try to figure out location information.
94      * <p>
95      * At the moment only SAXParseException, and TransformerException
96      * are considered.
97      * </p>
98      *
99      * @return String containing location information of the format
100      * <code>{file-name}:{line}:{column}:</code>, if no location info is
101      * available return empty string
102      */

103     private String JavaDoc extraInfo( Throwable JavaDoc t ) {
104         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
105         if (t instanceof SAXParseException JavaDoc) {
106             SAXParseException JavaDoc spe = (SAXParseException JavaDoc)t;
107             sb.append( String.valueOf(spe.getSystemId()));
108             sb.append( ":" );
109             sb.append( String.valueOf(spe.getLineNumber()));
110             sb.append( ":" );
111             sb.append( String.valueOf(spe.getColumnNumber()));
112             sb.append( ":" );
113         } else if (t instanceof TransformerException JavaDoc) {
114             TransformerException JavaDoc transformerException = (TransformerException JavaDoc) t;
115             SourceLocator JavaDoc sourceLocator = transformerException.getLocator();
116             
117             if( null != sourceLocator ) {
118                 sb.append( String.valueOf(sourceLocator.getSystemId()));
119                 sb.append( ":" );
120                 sb.append( String.valueOf(sourceLocator.getLineNumber()));
121                 sb.append( ":" );
122                 sb.append( String.valueOf(sourceLocator.getColumnNumber()));
123                 sb.append( ":" );
124             }
125         }
126         return sb.toString();
127     }
128     
129     public void printStackTrace() {
130         super.printStackTrace();
131         if(getCause()!=null)
132             getCause().printStackTrace();
133     }
134     
135     public void printStackTrace( PrintStream JavaDoc s ) {
136         super.printStackTrace(s);
137         if(getCause()!=null)
138             getCause().printStackTrace(s);
139     }
140     
141     public void printStackTrace( PrintWriter JavaDoc s ) {
142         super.printStackTrace(s);
143         if(getCause()!=null)
144             getCause().printStackTrace(s);
145     }
146
147 }
148
149
Popular Tags