KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > feedback > Emit


1 /**
2  * $Id: Emit.java 186 2007-03-16 13:42:35Z ssmc $
3  * Copyright 2003-2004 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or (at your option) any
9  * later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL (GNU Lesser General Public License) for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html<p>
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.feedback;
30
31 import java.io.ByteArrayOutputStream JavaDoc;
32 import java.io.IOException JavaDoc;
33
34 import com.idaremedia.apis.DiagnosticsEmitter;
35 import com.idaremedia.antx.ErrorSnapshot;
36 import com.idaremedia.antx.NoiseLevel;
37 import com.idaremedia.antx.helpers.Tk;
38 import com.idaremedia.antx.print.DisplayRequest;
39 import com.idaremedia.antx.print.ErrorPrinter;
40
41 /**
42  * Helper utilities to translate AntX NoiseLevel and ErrorSnapshots to a
43  * DiagnosticsEmitter's implementation APIs.
44  *
45  * @since JWare/AntX 0.3 (Moved out of EmitTask)
46  * @author ssmc, &copy;2003-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
47  * @version 0.5
48  * @.safety multiple
49  * @.group impl,helper
50  **/

51
52 public final class Emit
53 {
54     /**
55      * Route an emitted message to the diagnostics emitter's
56      * (typically a log4j Logger) appropriate API.
57      * @param broadcaster the diagnostics emitter (non-null)
58      * @param msg what's to be set or emitted (<em>in final form</em>)(non-null)
59      * @param t [optional] exception that caused this broadcast (can be null)
60      * @param nl the associated priority of this message (non-null)
61      * @see ErrorSnapshotRenderer
62      **/

63     public static final void broadcast(DiagnosticsEmitter broadcaster,
64                                Object JavaDoc msg, Throwable JavaDoc t, NoiseLevel nl)
65     {
66         switch (nl.getIndex()) {
67             case NoiseLevel.FATAL_INDEX:
68                 broadcaster.failure(t,msg); break;
69             case NoiseLevel.ERROR_INDEX:
70                 broadcaster.error(t,msg); break;
71             case NoiseLevel.WARNING_INDEX:
72                 broadcaster.warning(t,msg); break;
73             case NoiseLevel.INFO_INDEX:
74                 broadcaster.note(msg); break;
75             case NoiseLevel.VERBOSE_INDEX:
76                 broadcaster.note(msg); break;
77             default: //NoiseLevel.DEBUG_INDEX
78
broadcaster.finetrace(t,msg);
79         }
80     }
81
82
83     /**
84      * Returns a standard string description of given object (usually
85      * an error snapshot). The returned string is formatted like that
86      * returned by the AntX {@linkplain ErrorPrinter ErrorPrinter}
87      * display helper.
88      * @return formatted string (non-null)
89      **/

90     public static final String JavaDoc stringify(Object JavaDoc object)
91     {
92         if (object instanceof ErrorSnapshot) {
93             ErrorSnapshot es = (ErrorSnapshot)object;
94             try {
95                 ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc(256);
96                 DisplayRequest dr = new DisplayRequest(es);
97                 new ErrorPrinter().print(dr,bos);
98                 String JavaDoc s = bos.toString();
99                 bos.close();
100                 bos = null;
101                 return s;
102             } catch(IOException JavaDoc ioX) {
103                 return es.toString();
104             }
105         }
106         return Tk.stringFrom(object,null);
107     }
108
109
110     /**
111      * Not-allowed.
112      **/

113     private Emit()
114     { }
115 }
116
117 /* end-of-Emit.java */
118
Popular Tags