KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejen > EjenErrors


1 //
2
// Ejen (code generation system)
3
// Copyright (C) 2001, 2002 François Wolff (ejen@noos.fr).
4
//
5
// This file is part of Ejen.
6
//
7
// Ejen is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License as published by
9
// the Free Software Foundation; either version 2 of the License, or
10
// (at your option) any later version.
11
//
12
// Ejen is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License for more details.
16
//
17
// You should have received a copy of the GNU General Public License
18
// along with Ejen; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
//
21
package org.ejen;
22
23 import java.lang.reflect.InvocationTargetException JavaDoc;
24 import java.util.Properties JavaDoc;
25 import java.util.Vector JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.io.StringWriter JavaDoc;
29 import javax.xml.transform.TransformerException JavaDoc;
30 import javax.xml.transform.SourceLocator JavaDoc;
31 import org.apache.xml.utils.WrappedRuntimeException;
32 import org.xml.sax.SAXException JavaDoc;
33 import org.xml.sax.SAXParseException JavaDoc;
34
35 /**
36  * Ejen errors class utility.
37  * @author F. Wolff
38  * @version 1.0
39  */

40 public class EjenErrors {
41     public static final String JavaDoc EJEN_INFORMATION = "Ejen information";
42     public static final String JavaDoc STACK_TRACE = "stack-trace";
43     public static final String JavaDoc ID_FILE = "file";
44     public static final String JavaDoc ID_NOTE = "note";
45     public static final String JavaDoc ID_MESSAGE = "message";
46     public static final String JavaDoc ID_PUBLIC_ID = "public-id";
47     public static final String JavaDoc ID_SYSTEM_ID = "system-id";
48     public static final String JavaDoc ID_LINE = "line";
49     public static final String JavaDoc ID_COLUMN = "column";
50     public static final String JavaDoc LINE_SEPARATOR = System.getProperty("line.separator",
51             "\n");
52     private EjenErrors() {}
53
54     /**
55      * Returns an EjenError array (no stack trace).
56      * See {@link #get(String file, String note, Throwable t, boolean printStackTrace)}.
57      * @param file name of the file where the error occured.
58      * @param note additional message.
59      * @param t the exception to be analysed in order to retrieve all
60      * embedded exceptions information.
61      * @return the EjenError array.
62      */

63     public static EjenError[] get(String JavaDoc file, String JavaDoc note, Throwable JavaDoc t) {
64         return get(file, note, t, false);
65     }
66
67     /**
68      * Returns an EjenError array.
69      * <p>
70      * If the file or the note argument is not null, the first EjenError will be
71      * an "Ejen information" with the properties "file" or "note" filled in (at least
72      * one those properties, depending on nullity).
73      * <p>
74      * If the t argument is not null, the following EjenError(s) will have the name
75      * of the exception as name and (if not null) the property "message" set to
76      * the exception message. While there is an embedded exception in the
77      * current exception, a new EjenError is appended to the array (with specific
78      * information from the embedded exception) and the embedded exception becomes
79      * the current exception. If one of those exception is a SAXParseException or
80      * a TransformerException, the properties "column", "line", "public-id" and
81      * "system-id" may be included as well (depending on nullity).
82      * <p>
83      * If printStackTrace is true, an EjenError is finally appended to the array with
84      * name set to the last embedded exception name and a "stack-trace" property
85      * is added (whose value is the stack trace).
86      * @param file name of the file where the error occured.
87      * @param note additional message.
88      * @param t the exception to be analysed in order to retrieve all
89      * embedded exceptions information.
90      * @param printStackTrace should stack trace be provided or not.
91      * @return the EjenError array.
92      */

93     public static EjenError[] get(String JavaDoc file, String JavaDoc note, Throwable JavaDoc t, boolean printStackTrace) {
94         
95         Vector JavaDoc errors = new Vector JavaDoc();
96         Throwable JavaDoc lastNonNullThrowable = null;
97
98         if (file != null || note != null) {
99             EjenError ee = new EjenError(EJEN_INFORMATION);
100
101             if (file != null) {
102                 ee.putMessage(ID_FILE, file);
103             }
104             if (note != null) {
105                 ee.putMessage(ID_NOTE, note);
106             }
107             errors.add(ee);
108         }
109         int line, column;
110         String JavaDoc publicId, systemId, eMsg;
111
112         while (t != null) {
113             lastNonNullThrowable = t;
114             EjenError ee = new EjenError(t.getClass().getName());
115
116             if (t.getMessage() != null) {
117                 ee.putMessage(ID_MESSAGE, t.getMessage());
118             }
119             line = column = -1;
120             publicId = systemId = null;
121             if (t instanceof EjenException) {
122                 t = ((EjenException) t).getEmbeddedThrowable();
123             } else if (t instanceof WrappedRuntimeException) {
124                 t = ((WrappedRuntimeException) t).getException();
125             } else if (t instanceof SAXException JavaDoc) {
126                 SAXException JavaDoc se = (SAXException JavaDoc) t;
127
128                 if (se instanceof SAXParseException JavaDoc) {
129                     SAXParseException JavaDoc spe = (SAXParseException JavaDoc) se;
130
131                     column = spe.getColumnNumber();
132                     line = spe.getLineNumber();
133                     publicId = spe.getPublicId();
134                     systemId = spe.getSystemId();
135                 }
136                 t = se.getException();
137             } else if (t instanceof TransformerException JavaDoc) {
138                 TransformerException JavaDoc te = (TransformerException JavaDoc) t;
139                 SourceLocator JavaDoc sl = te.getLocator();
140
141                 if (sl != null) {
142                     column = sl.getColumnNumber();
143                     line = sl.getLineNumber();
144                     publicId = sl.getPublicId();
145                     systemId = sl.getSystemId();
146                 }
147                 t = ((TransformerException JavaDoc) t).getCause();
148             } else if (t instanceof InvocationTargetException JavaDoc) {
149                 t = ((InvocationTargetException JavaDoc) t).getTargetException();
150             } else {
151                 t = null;
152             }
153             if (publicId != null) {
154                 ee.putMessage(ID_PUBLIC_ID, publicId);
155             }
156             if (systemId != null) {
157                 ee.putMessage(ID_SYSTEM_ID, systemId);
158             }
159             if (line != -1) {
160                 ee.putMessage(ID_LINE, Integer.toString(line));
161             }
162             if (column != -1) {
163                 ee.putMessage(ID_COLUMN, Integer.toString(column));
164             }
165             errors.add(ee);
166         }
167         if (printStackTrace && lastNonNullThrowable != null) {
168             PrintWriter JavaDoc pw = null;
169             EjenError ee = new EjenError(lastNonNullThrowable.getClass().getName());
170
171             try {
172                 StringWriter JavaDoc sw = new StringWriter JavaDoc();
173
174                 pw = new PrintWriter JavaDoc(sw);
175                 lastNonNullThrowable.printStackTrace(pw);
176                 ee.putMessage(STACK_TRACE, sw.toString());
177             } catch (Exception JavaDoc e) {
178                 ee.putMessage(STACK_TRACE, "(failed)");
179             }
180             finally {
181                 errors.add(ee);
182                 pw.close();
183             }
184         }
185         return (EjenError[]) errors.toArray(new EjenError[0]);
186     }
187
188     /**
189      * Returns a String representation of the errors argument.
190      * <p>
191      * If the errors argument is null, "errors.null" is returned.
192      * <p>
193      * Otherwise, for each EjenError in the array, a String with the
194      * following structure is appended:<pre><code>[&lt;name of the EjenError&gt;] {
195      * &lt;name of property&gt;: &lt;value of property&gt;
196      * &lt;name of property&gt;: &lt;value of property&gt;
197      * ...
198      *}<code></pre>
199      * @param errors an EjenError array.
200      * @return the String representation.
201      */

202     public static String JavaDoc toString(EjenError[] errors) {
203         if (errors == null) {
204             return "errors.null";
205         }
206         
207         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
208
209         for (int i = 0; i < errors.length; i++) {
210             if (i > 0) {
211                 sb.append(LINE_SEPARATOR);
212             }
213             sb.append('[').append(errors[i].getName()).append("] {").append(LINE_SEPARATOR);
214             Properties JavaDoc msgs = errors[i].getMessages();
215
216             for (Enumeration JavaDoc e = msgs.propertyNames(); e.hasMoreElements();) {
217                 String JavaDoc name = (String JavaDoc) (e.nextElement());
218
219                 sb.append(" ").append(name).append(": ").append(msgs.getProperty(name)).append(LINE_SEPARATOR);
220             }
221             sb.append("}");
222         }
223         return sb.toString();
224     }
225
226     /**
227      * Identical to <code>toString(get(file, note, t, false))</code>.
228      * @param file name of the file where the error occured.
229      * @param note additional message.
230      * @param t the exception to be analysed in order to retrieve all
231      * embedded exceptions information.
232      * @return the String representation.
233      */

234     public static String JavaDoc toString(String JavaDoc file, String JavaDoc note, Throwable JavaDoc t) {
235         return toString(get(file, note, t, false));
236     }
237
238     /**
239      * Identical to <code>toString(get(file, note, t, printStackTrace))</code>.
240      * @param file name of the file where the error occured.
241      * @param note additional message.
242      * @param t the exception to be analysed in order to retrieve all
243      * embedded exceptions information.
244      * @param printStackTrace should stack trace be provided or not.
245      * @return the String representation.
246      */

247     public static String JavaDoc toString(String JavaDoc file, String JavaDoc note, Throwable JavaDoc t, boolean printStackTrace) {
248         return toString(get(file, note, t, printStackTrace));
249     }
250     
251     /**
252      * Class that represents one error.
253      * @author F. Wolff.
254      * @version 1.0.
255      */

256     public static class EjenError {
257         private String JavaDoc _name;
258         private Properties JavaDoc _messages;
259
260         /**
261          * Constructs an EjenError whose name is 'name'.
262          * @param name name of this EjenError.
263          */

264         public EjenError(String JavaDoc name) {
265             _name = name;
266             _messages = new Properties JavaDoc();
267         }
268
269         /**
270          * Returns the name of this EjenError.
271          * @return the name of this EjenError.
272          */

273         public String JavaDoc getName() {
274             return _name;
275         }
276
277         /**
278          * Returns the messages of this EjenError as Properties.
279          * @return the messages.
280          */

281         public Properties JavaDoc getMessages() {
282             return _messages;
283         }
284
285         /**
286          * Adds a message into this EjenError with a name set to id.
287          * @param id name of the new message.
288          * @param the new message.
289          */

290         public void putMessage(String JavaDoc id, String JavaDoc message) {
291             _messages.setProperty(id, message);
292         }
293     }
294 }
295
Popular Tags