KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > util > ChainedException


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2004 Claude Duguay
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/util/ChainedException.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/01/02 16:24:58 $
10
// $Revision: 1.44 $
11
//
12
// This library is free software; you can redistribute it and/or
13
// modify it under the terms of the GNU Lesser General Public
14
// License as published by the Free Software Foundation; either
15
// version 2.1 of the License, or (at your option) any later version.
16
//
17
// This library is distributed in the hope that it will be useful,
18
// but WITHOUT ANY WARRANTY; without even the implied warranty of
19
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
// Lesser General Public License for more details.
21
//
22
// You should have received a copy of the GNU Lesser General Public
23
// License along with this library; if not, write to the Free Software
24
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
//
26

27 package org.htmlparser.util;
28
29 /**
30  * Support for chained exceptions in code that predates Java 1.4.
31  * A chained exception can use a Throwable argument to reference
32  * a lower level exception. The chained exception provides a
33  * stack trace that includes the message and any throwable
34  * exception included as an argument in the chain.
35  *
36  * For example:
37  *
38  * ApplicationException: Application problem encountered;
39  * ProcessException: Unable to process document;
40  * java.io.IOException: Unable to open 'filename.ext'
41  * at ChainedExceptionTest.openFile(ChainedExceptionTest.java:19)
42  * at ChainedExceptionTest.processFile(ChainedExceptionTest.java:27)
43  * at ChainedExceptionTest.application(ChainedExceptionTest.java:40)
44  * at ChainedExceptionTest.main(ChainedExceptionTest.java:52)
45  *
46  * Represents the output from two nested exceptions. The outside
47  * exception is a subclass of ChainedException called
48  * ApplicationException, which includes a throwable reference.
49  * The throwable reference is also a subclass of ChainedException,
50  * called ProcessException, which in turn includes a reference to
51  * a standard IOException. In each case, the message is increasingly
52  * specific about the nature of the problem. The end user may only
53  * see the application exception, but debugging is greatly
54  * enhanced by having more details in the stack trace.
55  *
56  * @author Claude Duguay
57  **/

58
59 import java.io.PrintStream JavaDoc;
60 import java.io.PrintWriter JavaDoc;
61 import java.util.Vector JavaDoc;
62
63 public class ChainedException
64   extends Exception JavaDoc
65 {
66   protected Throwable JavaDoc throwable;
67
68   public ChainedException() {}
69
70   public ChainedException(String JavaDoc message)
71   {
72     super(message);
73   }
74
75   public ChainedException(Throwable JavaDoc throwable)
76   {
77     this.throwable = throwable;
78   }
79
80   public ChainedException(String JavaDoc message, Throwable JavaDoc throwable)
81   {
82     super(message);
83     this.throwable = throwable;
84   }
85
86   public String JavaDoc[] getMessageChain()
87   {
88     Vector JavaDoc list = getMessageList();
89     String JavaDoc[] chain = new String JavaDoc[list.size()];
90     list.copyInto (chain);
91     return chain;
92   }
93
94   public Vector JavaDoc getMessageList()
95   {
96     Vector JavaDoc list = new Vector JavaDoc();
97     list.addElement(getMessage());
98     if (throwable != null)
99     {
100       if (throwable instanceof ChainedException)
101       {
102         ChainedException chain = (ChainedException)throwable;
103         Vector JavaDoc sublist = chain.getMessageList ();
104         for (int i = 0; i < sublist.size (); i++)
105             list.addElement (sublist.elementAt (i));
106       }
107       else
108       {
109         String JavaDoc message = throwable.getMessage();
110         if (message != null && !message.equals(""))
111         {
112           list.addElement (message);
113         }
114       }
115     }
116     return list;
117   }
118
119   public Throwable JavaDoc getThrowable()
120   {
121     return throwable;
122   }
123
124   public void printStackTrace()
125   {
126     printStackTrace(System.err);
127   }
128
129   public void printStackTrace(PrintStream JavaDoc out)
130   {
131     synchronized (out)
132     {
133       if (throwable != null)
134       {
135         out.println(getClass().getName() +
136           ": " + getMessage() + ";");
137         throwable.printStackTrace(out);
138       }
139       else
140       {
141         super.printStackTrace(out);
142       }
143     }
144   }
145
146   public void printStackTrace(PrintWriter JavaDoc out)
147   {
148     synchronized (out)
149     {
150       if (throwable != null)
151       {
152         out.println(getClass().getName() +
153           ": " + getMessage() + ";");
154         throwable.printStackTrace(out);
155       }
156       else
157       {
158         super.printStackTrace(out);
159       }
160     }
161   }
162 }
163
164
Popular Tags