KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > susebox > java > lang > ThrowableMessageFormatter


1 /*
2  * ThrowableMessageFormatter.java: formatting a throwable message
3  *
4  * Copyright (C) 2001 Heiko Blau
5  *
6  * This file belongs to the Susebox Java Core Library (Susebox JCL).
7  * The Susebox JCL is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or (at your
10  * option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.
15  * See the GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License along
18  * with the Susebox JCL. If not, write to the
19  *
20  * Free Software Foundation, Inc.
21  * 59 Temple Place, Suite 330,
22  * Boston, MA 02111-1307
23  * USA
24  *
25  * or check the Internet: http://www.fsf.org
26  *
27  * Contact:
28  * email: heiko@susebox.de
29  */

30
31 package de.susebox.java.lang;
32
33 //------------------------------------------------------------------------------
34
// Imports
35
//
36
import java.lang.reflect.Method JavaDoc;
37 import java.text.MessageFormat JavaDoc;
38 import de.susebox.java.lang.ThrowableList;
39
40
41 //------------------------------------------------------------------------------
42
// Class ThrowableMessageFormatter
43
//
44

45 /**<p>
46  * This class is used by the implementations of the {@link ThrowableList} interface.
47  * Its method {@link #getMessage} formats the message that should be returned by
48  * the {@link java.lang.Throwable#getMessage} overridden by implementations of the
49  * {@link ThrowableList} interface.
50  *</p>
51  *
52  * @see ThrowableList
53  * @see java.lang.Throwable
54  * @see java.text.MessageFormat
55  * @author Heiko Blau
56  */

57 public final class ThrowableMessageFormatter {
58   
59   /**
60    * Message indentation for nested exceptions.
61    */

62   public static final String JavaDoc MSG_IDENTATION = " ";
63   
64   /**
65    * This method should be called by all implementations of the {@link ThrowableList}
66    * interface in their {@link java.lang.Throwable#getMessage} implementation. It
67    * ensures that the formatting of throwable lists, nested or wrapped exceptions
68    * is done in a consistent way.
69    *<br>
70    * The method returns an throwable message assembled in this way:
71    *<ol><li>
72    * If the calling throwable is a wrapper throwable (see {@link ThrowableList#isWrapper}),
73    * it delegates the call to the wrapped throwable (<code>wrappedEx.getMessage()</code>).
74    *</li><li>
75    * If there is a nested throwable (the calling throwable has a message of its own),
76    * the returned message starts with the string returned by the {@link java.lang.Object#toString}
77    * method, followed and separated by a end-of-line sequence by the formatted message
78    * of the calling throwable.
79    *</li><li>
80    * If the calling throwable has only a message without parameters, this message
81    * is either appended to the string produced by processing a nested throwable or
82    * is taken as the entire return value if there is no nested or subsequent throwable
83    * present.
84    *</li><li>
85    * If the calling throwable provides parameters along with a format string (the
86    * return value of <code>super.getMessage</code> is interpreted as a format string),
87    * a formatted message produced by {@link java.text.MessageFormat#format} is either
88    * appended to the string produced by processing a nested throwable or is taken as
89    * the entire return value if there is no nested or subsequent throwable present.
90    *</li></ol>
91    *
92    * @param ex the calling throwable
93    * @return the formatted throwable message
94    * @see java.text.MessageFormat
95    * @see ThrowableList
96    */

97   public static final String JavaDoc getMessage(ThrowableList ex) {
98     // wrapped exceptions return theri own message
99
if (ex.isWrapper()) {
100       return ex.getCause().getMessage();
101     }
102     
103     // prepare the formatting
104
StringBuffer JavaDoc msg = new StringBuffer JavaDoc();
105     String JavaDoc fmt = ex.getFormat();
106     Throwable JavaDoc next = ex.getCause();
107     
108     // message of the nested or next throwable first
109
if (next != null) {
110       msg.append(EOL_SEQUENCE);
111       msg.append(MSG_IDENTATION);
112       msg.append(next.toString());
113       if (fmt != null) {
114         msg.append(EOL_SEQUENCE);
115         msg.append(MSG_IDENTATION);
116       }
117     }
118
119     // and now our own message
120
if (fmt != null) {
121       Object JavaDoc[] args = ex.getArguments();
122       
123       if (args == null) {
124         msg.append(fmt);
125       } else {
126         try {
127           msg.append(MessageFormat.format(fmt, args));
128         } catch (IllegalArgumentException JavaDoc argEx) {
129           msg.append(argEx.getMessage());
130           msg.append(EOL_SEQUENCE);
131           msg.append("While formatting this message:");
132           msg.append(EOL_SEQUENCE);
133           msg.append(fmt);
134         }
135       }
136     }
137     return msg.toString();
138   }
139
140   //---------------------------------------------------------------------------
141
// class constructor
142
//
143
static {
144     try {
145       Method JavaDoc causeMethod = Throwable JavaDoc.class.getMethod("getCause", new Class JavaDoc[] {} );
146       GET_CAUSE_AVAILABLE = true;
147     } catch (NoSuchMethodException JavaDoc ex) {
148       GET_CAUSE_AVAILABLE = false;
149     }
150   }
151   
152   //---------------------------------------------------------------------------
153
// members
154
//
155
private static final String JavaDoc EOL_SEQUENCE = System.getProperty("line.separator");
156   private static boolean GET_CAUSE_AVAILABLE;
157 }
158
Popular Tags