KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: ErrorSnapshotRenderer.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2002-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 of the License, or (at your option) any later
9  * 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 org.apache.log4j.or.ObjectRenderer;
35
36 import com.idaremedia.apis.UIStringManager;
37
38 import com.idaremedia.antx.AntX;
39 import com.idaremedia.antx.ErrorSnapshot;
40 import com.idaremedia.antx.helpers.Tk;
41 import com.idaremedia.antx.print.DisplayRequest;
42 import com.idaremedia.antx.print.ErrorPrinter;
43
44 /**
45  * Object Renderer adapter for AntX {@linkplain ErrorSnapshot ErrorSnapshots}.
46  *
47  * @since JWare/AntX 0.1
48  * @author ssmc, &copy;2002-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
49  * @version 0.5
50  * @.safety multiple
51  * @.group impl,helper
52  * @see ErrorSnapshot
53  * @see EmitTask
54  **/

55
56 public class ErrorSnapshotRenderer implements ObjectRenderer
57 {
58     /**
59      * Creates new error snapshot object renderer.
60      **/

61     public ErrorSnapshotRenderer()
62     {
63         m_printWorker = new ErrorPrinter();
64     }
65
66
67     /**
68      * Returns the standard <i>AntX</i> resource bundle strings
69      * manager. Never returns <i>null</i>.
70      **/

71     protected final UIStringManager uistrs()
72     {
73         return AntX.uistrs();
74     }
75
76
77     /**
78      * Returns a standard string description of given error snapshot.
79      * The returned string is formatted like that returned by the
80      * AntX {@linkplain ErrorPrinter ErrorPrinter} display helper.
81      * @param es error snapshot (non-null)
82      * @return formatted string (non-null)
83      **/

84     protected final String JavaDoc defaultStringFrom(ErrorSnapshot es)
85     {
86         try {
87             ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc(256);
88             DisplayRequest dr = new DisplayRequest(es);
89             m_printWorker.print(dr,bos);
90             String JavaDoc s = bos.toString();
91             bos.close();
92             bos = null;
93             return s;
94         } catch(IOException JavaDoc ioX) {
95             return es.toString();
96         }
97     }
98
99
100     /**
101      * Returns a standard string description of error snapshot. Subclasses
102      * should override this method to produce XML, HTML or whatever.
103      **/

104     protected String JavaDoc stringFrom(ErrorSnapshot es)
105     {
106         return defaultStringFrom(es);
107     }
108
109
110     /**
111      * If incoming object is an ErrorSnapshot, returns a standard formatted
112      * string of its contents. Otherwise, defers to the object's 'toString'
113      * method.
114      **/

115     public String JavaDoc doRender(Object JavaDoc object)
116     {
117         if (object instanceof ErrorSnapshot) {
118             return stringFrom((ErrorSnapshot)object);
119         }
120         return Tk.stringFrom(object,null);
121     }
122
123     private final ErrorPrinter m_printWorker;
124 }
125
126 /* end-of-ErrorSnapshotRenderer.java */
127
Popular Tags