001/*
002 * $Id: ExceptionConverter.java 4784 2011-03-15 08:33:00Z blowagie $
003 *
004 * This file is part of the iText (R) project.
005 * Copyright (c) 1998-2011 1T3XT BVBA
006 * Authors: Bruno Lowagie, Paulo Soares, et al.
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU Affero General Public License version 3
010 * as published by the Free Software Foundation with the addition of the
011 * following permission added to Section 15 as permitted in Section 7(a):
012 * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT,
013 * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
014 *
015 * This program is distributed in the hope that it will be useful, but
016 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
017 * or FITNESS FOR A PARTICULAR PURPOSE.
018 * See the GNU Affero General Public License for more details.
019 * You should have received a copy of the GNU Affero General Public License
020 * along with this program; if not, see http://www.gnu.org/licenses or write to
021 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
022 * Boston, MA, 02110-1301 USA, or download the license from the following URL:
023 * http://itextpdf.com/terms-of-use/
024 *
025 * The interactive user interfaces in modified source and object code versions
026 * of this program must display Appropriate Legal Notices, as required under
027 * Section 5 of the GNU Affero General Public License.
028 *
029 * In accordance with Section 7(b) of the GNU Affero General Public License,
030 * a covered work must retain the producer line in every PDF that is created
031 * or manipulated using iText.
032 *
033 * You can be released from the requirements of the license by purchasing
034 * a commercial license. Buying such a license is mandatory as soon as you
035 * develop commercial activities involving the iText software without
036 * disclosing the source code of your own applications.
037 * These activities include: offering paid services to customers as an ASP,
038 * serving PDFs on the fly in a web application, shipping iText with a closed
039 * source product.
040 *
041 * For more information, please contact iText Software Corp. at this
042 * address: sales@itextpdf.com
043 */
044
045/*
046 * The original version of this class was published in an article by professor Heinz Kabutz.
047 * Read http://www.javaspecialists.co.za/archive/newsletter.do?issue=033&print=yes&locale=en_US
048 * "This material from The Java(tm) Specialists' Newsletter by Maximum Solutions (South Africa).
049 * Please contact Maximum Solutions for more information."
050 * 
051 * Copyright (C) 2001 Dr. Heinz M. Kabutz
052 * Permission was granted by Dr. Kabutz to use this source code in iText.
053 */
054package com.itextpdf.text;
055
056/**
057 * The ExceptionConverter changes a checked exception into an
058 * unchecked exception.
059 */
060public class ExceptionConverter extends RuntimeException {
061    private static final long serialVersionUID = 8657630363395849399L;
062        /** we keep a handle to the wrapped exception */
063    private Exception ex;
064    /** prefix for the exception */
065    private String prefix;
066
067    /**
068     * Construct a RuntimeException based on another Exception
069     * @param ex the exception that has to be turned into a RuntimeException
070     */
071    public ExceptionConverter(Exception ex) {
072        super(ex);
073        this.ex = ex;
074        prefix = (ex instanceof RuntimeException) ? "" : "ExceptionConverter: ";
075    }
076
077    /**
078     * Convert an Exception into an unchecked exception. Return the exception if it is
079     * already an unchecked exception or return an ExceptionConverter wrapper otherwise
080     *
081     * @param ex the exception to convert
082     * @return an unchecked exception 
083     * @since 2.1.6
084     */
085    public static final RuntimeException convertException(Exception ex) {
086        if (ex instanceof RuntimeException) {
087            return (RuntimeException) ex;
088        }
089        return new ExceptionConverter(ex);
090    }
091
092    /**
093     * and allow the user of ExceptionConverter to get a handle to it. 
094     * @return the original exception
095     */
096    public Exception getException() {
097        return ex;
098    }
099
100    /**
101     * We print the message of the checked exception 
102     * @return message of the original exception
103     */
104    public String getMessage() {
105        return ex.getMessage();
106    }
107
108    /**
109     * and make sure we also produce a localized version
110     * @return localized version of the message
111     */
112    public String getLocalizedMessage() {
113        return ex.getLocalizedMessage();
114    }
115
116    /**
117     * The toString() is changed to be prefixed with ExceptionConverter 
118     * @return String version of the exception
119     */
120    public String toString() {
121        return prefix + ex;
122    }
123
124    /** we have to override this as well */
125    public void printStackTrace() {
126        printStackTrace(System.err);
127    }
128
129    /**
130     * here we prefix, with s.print(), not s.println(), the stack
131     * trace with "ExceptionConverter:" 
132     * @param s
133     */
134    public void printStackTrace(java.io.PrintStream s) {
135        synchronized (s) {
136            s.print(prefix);
137            ex.printStackTrace(s);
138        }
139    }
140
141    /**
142     * Again, we prefix the stack trace with "ExceptionConverter:" 
143     * @param s
144     */
145    public void printStackTrace(java.io.PrintWriter s) {
146        synchronized (s) {
147            s.print(prefix);
148            ex.printStackTrace(s);
149        }
150    }
151
152    /**
153     * requests to fill in the stack trace we will have to ignore.
154     * We can't throw an exception here, because this method
155     * is called by the constructor of Throwable 
156     * @return a Throwable
157     */
158    public Throwable fillInStackTrace() {
159        return this;
160    }
161}