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     * @serial
037     */
038    Throwable nested = null;
039
040    /**
041     * Constructs an <code>ISOException</code> with no detail message. 
042     */
043    public ISOException() {
044        super();
045    }
046
047    /**
048     * Constructs an <code>ISOException</code> with the specified detail 
049     * message. 
050     *
051     * @param   s   the detail message.
052     */
053    public ISOException(String s) {
054        super(s);
055    }
056
057    /**
058     * Constructs an <code>ISOException</code> with a nested
059     * exception
060     * @param nested another exception
061     */
062    public ISOException (Throwable nested) {
063        super(nested.toString());
064        this.nested = nested;
065    }
066
067    /**
068     * Constructs an <code>ISOException</code> with a detail Message nested
069     * exception
070     * @param   s   the detail message.
071     * @param nested another exception
072     */
073    public ISOException (String s, Throwable nested) {
074        super(s);
075        this.nested = nested;
076    }
077
078    /**
079     * @return nested exception (may be null)
080     */
081    public Throwable getNested() {
082        return nested;
083    }
084
085    protected String getTagName() {
086        return "iso-exception";
087    }
088    public void dump (PrintStream p, String indent) {
089        String inner = indent + "  ";
090        p.println (indent + "<"+getTagName()+">");
091        p.println (inner  + getMessage());
092        if (nested != null) {
093            if (nested instanceof ISOException) 
094                ((ISOException)nested).dump (p, inner);
095            else {
096                p.println (inner + "<nested-exception>");
097                p.print   (inner);
098                nested.printStackTrace (p);
099                p.println (inner + "</nested-exception>");
100            }
101        }
102        p.print (inner);
103        printStackTrace (p);
104        p.println (indent + "</"+getTagName()+">");
105    }
106    public String toString() {
107        StringBuilder buf = new StringBuilder (super.toString());
108        if (nested != null) {
109            buf.append (" (");
110            buf.append (nested.toString());
111            buf.append (")");
112        }
113        return buf.toString();
114    }
115
116    public void printStackTrace() {
117        super.printStackTrace();
118        if (nested != null) {
119            System.err.print("Nested:");
120            nested.printStackTrace();
121        }
122    }
123
124    public void printStackTrace(PrintStream ps) {
125        super.printStackTrace(ps);
126        if (nested != null) {
127            ps.print("Nested:");
128            nested.printStackTrace(ps);
129        }
130    }
131
132    public void printStackTrace(PrintWriter pw) {
133        super.printStackTrace(pw);
134        if (nested != null) {
135            pw.print("Nested:");
136            nested.printStackTrace(pw);
137        }
138    }
139}