KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > ExceptionConverter


1 /*
2  * The original version of this class was published in an article by professor Heinz Kabutz.
3  * Read http://www.javaspecialists.co.za/archive/newsletter.do?issue=033&print=yes&locale=en_US
4  * "This material from The Java(tm) Specialists' Newsletter by Maximum Solutions (South Africa).
5  * Please contact Maximum Solutions for more information."
6  */

7
8 /*
9  * The contents of this file are subject to the Mozilla Public License Version 1.1
10  * (the "License"); you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
12  *
13  * Software distributed under the License is distributed on an "AS IS" basis,
14  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
15  * for the specific language governing rights and limitations under the License.
16  *
17  * The Original Code is 'iText, a free JAVA-PDF library'.
18  *
19  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
20  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
21  * All Rights Reserved.
22  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
23  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
24  *
25  * Contributor(s): all the names of the contributors are added in the source code
26  * where applicable.
27  *
28  * Alternatively, the contents of this file may be used under the terms of the
29  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
30  * provisions of LGPL are applicable instead of those above. If you wish to
31  * allow use of your version of this file only under the terms of the LGPL
32  * License and not to allow others to use your version of this file under
33  * the MPL, indicate your decision by deleting the provisions above and
34  * replace them with the notice and other provisions required by the LGPL.
35  * If you do not delete the provisions above, a recipient may use your version
36  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
37  *
38  * This library is free software; you can redistribute it and/or modify it
39  * under the terms of the MPL as stated above or under the terms of the GNU
40  * Library General Public License as published by the Free Software Foundation;
41  * either version 2 of the License, or any later version.
42  *
43  * This library is distributed in the hope that it will be useful, but WITHOUT
44  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
45  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
46  * details.
47  *
48  * If you didn't download this code from the following link, you should check if
49  * you aren't using an obsolete version:
50  * http://www.lowagie.com/iText/
51  */

52 package com.lowagie.text;
53
54 /**
55  * The ExceptionConverter changes a checked exception into an
56  * unchecked exception.
57  */

58 public class ExceptionConverter extends RuntimeException JavaDoc {
59     private static final long serialVersionUID = 8657630363395849399L;
60     /** we keep a handle to the wrapped exception */
61     private Exception JavaDoc ex;
62     /** prefix for the exception */
63     private String JavaDoc prefix;
64
65     /**
66      * Construct a RuntimeException based on another Exception
67      * @param ex the exception that has to be turned into a RuntimeException
68      */

69     public ExceptionConverter(Exception JavaDoc ex) {
70         this.ex = ex;
71         prefix = (ex instanceof RuntimeException JavaDoc) ? "" : "ExceptionConverter: ";
72     }
73
74     /**
75      * and allow the user of ExceptionConverter to get a handle to it.
76      * @return the original exception
77      */

78     public Exception JavaDoc getException() {
79         return ex;
80     }
81
82     /**
83      * We print the message of the checked exception
84      * @return message of the original exception
85      */

86     public String JavaDoc getMessage() {
87         return ex.getMessage();
88     }
89
90     /**
91      * and make sure we also produce a localized version
92      * @return localized version of the message
93      */

94     public String JavaDoc getLocalizedMessage() {
95         return ex.getLocalizedMessage();
96     }
97
98     /**
99      * The toString() is changed to be prefixed with ExceptionConverter
100      * @return Stringversion of the exception
101      */

102     public String JavaDoc toString() {
103         return prefix + ex;
104     }
105
106     /** we have to override this as well */
107     public void printStackTrace() {
108         printStackTrace(System.err);
109     }
110
111     /**
112      * here we prefix, with s.print(), not s.println(), the stack
113      * trace with "ExceptionConverter:"
114      * @param s
115      */

116     public void printStackTrace(java.io.PrintStream JavaDoc s) {
117         synchronized (s) {
118             s.print(prefix);
119             ex.printStackTrace(s);
120         }
121     }
122
123     /**
124      * Again, we prefix the stack trace with "ExceptionConverter:"
125      * @param s
126      */

127     public void printStackTrace(java.io.PrintWriter JavaDoc s) {
128         synchronized (s) {
129             s.print(prefix);
130             ex.printStackTrace(s);
131         }
132     }
133
134     /**
135      * requests to fill in the stack trace we will have to ignore.
136      * We can't throw an exception here, because this method
137      * is called by the constructor of Throwable
138      * @return a Throwable
139      */

140     public Throwable JavaDoc fillInStackTrace() {
141         return this;
142     }
143 }
Popular Tags