001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.iso;
020
021import org.jpos.util.Loggeable;
022
023import java.io.PrintStream;
024import java.io.PrintWriter;
025
026/**
027 * Signals that an ISO exception of some sort has occurred. 
028 *
029 * @author  <a href="mailto:apr@cs.com.uy">Alejandro P. Revilla</a>
030 * @version $Revision$ $Date$
031 */
032public class ISOException extends Exception implements Loggeable {
033
034    private static final long serialVersionUID = -777216335204861186L;
035    /**
036     * Nested exception wrapped by this ISOException, when present.
037     *
038     * @serial wrapped cause captured for legacy serialization
039     */
040    Throwable nested = null;
041
042    /**
043     * Constructs an <code>ISOException</code> with no detail message. 
044     */
045    public ISOException() {
046        super();
047    }
048
049    /**
050     * Constructs an <code>ISOException</code> with the specified detail 
051     * message. 
052     *
053     * @param   s   the detail message.
054     */
055    public ISOException(String s) {
056        super(s);
057    }
058
059    /**
060     * Constructs an <code>ISOException</code> with a nested
061     * exception
062     * @param nested another exception
063     */
064    public ISOException (Throwable nested) {
065        super(nested.toString());
066        this.nested = nested;
067    }
068
069    /**
070     * Constructs an <code>ISOException</code> with a detail Message nested
071     * exception
072     * @param   s   the detail message.
073     * @param nested another exception
074     */
075    public ISOException (String s, Throwable nested) {
076        super(s);
077        this.nested = nested;
078    }
079
080    /**
081     * Returns the nested (wrapped) exception, if any.
082     * @return nested exception (may be null)
083     */
084    public Throwable getNested() {
085        return nested;
086    }
087
088    /**
089     * Returns the XML tag name used when dumping this exception.
090     * @return the tag name string
091     */
092    protected String getTagName() {
093        return "iso-exception";
094    }
095    /**
096     * Dumps a human-readable representation of this exception to the print stream.
097     * @param p the output stream
098     * @param indent indentation prefix
099     */
100    public void dump (PrintStream p, String indent) {
101        String inner = indent + "  ";
102        p.println (indent + "<"+getTagName()+">");
103        p.println (inner  + getMessage());
104        if (nested != null) {
105            if (nested instanceof ISOException) 
106                ((ISOException)nested).dump (p, inner);
107            else {
108                p.println (inner + "<nested-exception>");
109                p.print   (inner);
110                nested.printStackTrace (p);
111                p.println (inner + "</nested-exception>");
112            }
113        }
114        p.print (inner);
115        printStackTrace (p);
116        p.println (indent + "</"+getTagName()+">");
117    }
118    public String toString() {
119        StringBuilder buf = new StringBuilder (super.toString());
120        if (nested != null) {
121            buf.append (" (");
122            buf.append (nested.toString());
123            buf.append (")");
124        }
125        return buf.toString();
126    }
127
128    public void printStackTrace() {
129        super.printStackTrace();
130        if (nested != null) {
131            System.err.print("Nested:");
132            nested.printStackTrace();
133        }
134    }
135
136    public void printStackTrace(PrintStream ps) {
137        super.printStackTrace(ps);
138        if (nested != null) {
139            ps.print("Nested:");
140            nested.printStackTrace(ps);
141        }
142    }
143
144    public void printStackTrace(PrintWriter pw) {
145        super.printStackTrace(pw);
146        if (nested != null) {
147            pw.print("Nested:");
148            nested.printStackTrace(pw);
149        }
150    }
151}