KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > print > ErrorPrinter


1 /**
2  * $Id: ErrorPrinter.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2002-2003 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.print;
30
31 import java.io.IOException JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.io.PrintStream JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Properties JavaDoc;
36
37 import com.idaremedia.antx.AntX;
38 import com.idaremedia.antx.ErrorSnapshot;
39 import com.idaremedia.antx.helpers.Strings;
40 import com.idaremedia.antx.helpers.Tk;
41 import com.idaremedia.apis.UIStringManager;
42
43 /**
44  * Default strategy for printing an {@linkplain ErrorSnapshot ErrorSnapshot} to an
45  * output stream. Basically dumps a big ol' Properties object containing all snapshot's
46  * information as fields.
47  *
48  * @since JWare/AntX 0.2
49  * @author ssmc, &copy;2002-2003 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
50  * @version 0.5
51  * @.safety multiple
52  * @.group impl,helper
53  * @see EchoErrorTask
54  **/

55
56 public final class ErrorPrinter implements DisplayStrategy
57 {
58     /**
59      * Initializes a new ErrorPrinter.
60      **/

61     public ErrorPrinter()
62     {
63     }
64
65
66     /**
67      * Converts an error snapshot's header fields to several Properties
68      * entries.
69      * @param id the display name of the request (non-null)
70      * @param es the error snapshot (non-null)
71      **/

72     private Properties JavaDoc propertiesFromHeader(String JavaDoc id, ErrorSnapshot es)
73     {
74         /** Include Header information as Property=Value entries **/
75         Properties JavaDoc allP = new Properties JavaDoc();
76
77         Object JavaDoc thrown = es.getThrown();
78         if (thrown==null) { thrown = ""; }
79         allP.setProperty(uistrs().get("snapshot.print.thrown",id),thrown.toString());
80
81         allP.setProperty(uistrs().get("snapshot.print.location",id),es.getLocation().toString());
82         allP.setProperty(uistrs().get("snapshot.print.task",id),es.getTaskName());
83         allP.setProperty(uistrs().get("snapshot.print.target",id),es.getTargetName());
84         allP.setProperty(uistrs().get("snapshot.print.comment",id),es.getComment());
85
86         return allP;
87     }
88
89
90     /**
91      * Produces the appropriate set of captured properties. Will return
92      * <i>null</i> if no properties requested.
93      * @param id the display name of the request (non-null)
94      * @param es the error snapshot (non-null)
95      * @param list list of properties to display (non-null)
96      **/

97     private Properties JavaDoc propertiesFromEnv(String JavaDoc id, ErrorSnapshot es, String JavaDoc list)
98     {
99         String JavaDoc ll= Tk.lowercaseFrom(list);
100         if (ll.length()==0 || Strings.NONE.equals(ll)) {
101             return null;
102         }
103
104         /** Start with the entire universe captured by the snapshot **/
105         Properties JavaDoc allP = es.copyOfProperties();
106
107         /** Filter to only the properties requested (can be all) **/
108         if (!Strings.ALL.equals(ll)) {
109             List JavaDoc wanted = Tk.splitList(list,",");
110             allP.keySet().retainAll(wanted);
111
112             if (wanted.size()>allP.size()) {//insert "null" strings
113
for (int i=0,N=wanted.size();i<N;i++) {
114                     String JavaDoc key = (String JavaDoc)wanted.get(i);
115                     if (allP.getProperty(key)==null) {
116                         allP.setProperty(key,Strings.NULL);
117                     }
118                 }
119             }
120             wanted=null;
121         }
122
123         return allP;
124     }
125
126
127     /**
128      * Generates an ErrorSnapshot as a big ol' Properties listing
129      * to the given output stream.
130      * @param info display information (non-null)
131      * @param os the output stream (non-null)
132      * @throws IOException if any I/O error occurs
133      **/

134     public void print(final DisplayRequest info, OutputStream JavaDoc os)
135         throws IOException JavaDoc
136     {
137         Object JavaDoc target = info.getObjectToBeDisplayed();
138
139         /** Ignore nulls (do nada), only do ErrorSnapshots **/
140         if (target instanceof ErrorSnapshot) {
141
142             ErrorSnapshot es = (ErrorSnapshot)target;
143             String JavaDoc id = info.getName();
144             if (id==null) {
145                 id = es.getName();
146                 if (id==null) {
147                     id = Strings.NULL;
148                 }
149             }
150
151             Properties JavaDoc allP;
152
153             /** Always ensure header information is first. **/
154             new PrintStream JavaDoc(os).println("########");
155             allP = propertiesFromHeader(id,es);
156             allP.store(os,uistrs().get("snapshot.print.header",id));
157             allP.clear();
158             allP=null;
159
160             /** Write the requested environment stuff next. **/
161             String JavaDoc list = info.getFilter();
162             if (list==null) {
163                 list = Strings.ALL;
164             }
165             allP = propertiesFromEnv(id,es,list);
166             if (allP!=null) {
167                 allP.store(os,uistrs().get("snapshot.print.content",id));
168                 allP.clear();
169                 allP=null;
170             }
171         }
172         /** Ignore nulls (do nada), only do Throwables **/
173         else if (target instanceof Throwable JavaDoc ) {
174
175             Throwable JavaDoc thrown = (Throwable JavaDoc)target;
176             PrintStream JavaDoc ps = new PrintStream JavaDoc(os);
177
178             ps.println("########");
179             ps.print("#");
180             ps.println(uistrs().get("thrown.print.header",Tk.leafNameFrom(thrown.getClass())));
181             ps.print("#");
182             ps.println(new java.util.Date JavaDoc());
183             thrown.printStackTrace(ps);
184             ps.flush();
185             ps=null;
186         }
187     }
188
189
190     private UIStringManager uistrs()
191     {
192         return AntX.uistrs();
193     }
194 }
195
196 /* end-of-ErrorPrinter.java */
197
Popular Tags