KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: EchoErrorTask.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.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
34 import org.apache.tools.ant.BuildException;
35 import org.apache.tools.ant.Project;
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.antx.starters.EchoThingTask;
42
43 /**
44  * Helper task to display contents of an {@linkplain ErrorSnapshot} or a BuildException.
45  * Mostly used for debugging or during unit testing. Also useful if the AntX print
46  * system is not being used.
47  * <p>
48  * <b>Examples:</b><pre>
49  * &lt;<b>printerror</b> message="javadoc-error" snapshotid="javadoc-error"/&gt;
50  *
51  * &lt;<b>printerror</b> message="cvs-error" thrownid="cvs-err"/&gt;
52  *
53  * &lt;<b>printerror</b> msgid="err.javadoc" snapshotid="javadoc-error"
54  * tofile="${logs.root}/docs/javadoc.err" append="true" if="log.errors"/&gt;
55  * </pre>
56  *
57  * @since JWare/AntX 0.1
58  * @author ssmc, &copy;2002-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
59  * @version 0.5
60  * @.safety single
61  * @.group impl,helper
62  **/

63
64 public final class EchoErrorTask extends EchoThingTask
65 {
66     /**
67      * Initializes new error echo task.
68      **/

69     public EchoErrorTask()
70     {
71         super(AntX.print);
72     }
73
74
75     /**
76      * Initializes enclosed CV-labeled echo task.
77      **/

78     public EchoErrorTask(String JavaDoc iam)
79     {
80         super(iam);
81     }
82
83
84     /**
85      * Sets the error snapshot reference id for this task. This reference
86      * is evaluated when the task is executed.
87      **/

88     public void setSnapshotId(String JavaDoc refId)
89     {
90         setThingRefId(refId);
91     }
92
93
94     /**
95      * Returns the error snapshot's reference id; can be <i>null</i>
96      * if never set. Required for task to execute properly.
97      **/

98     public final String JavaDoc getSnapshotRefId()
99     {
100         return getThingRefId();
101     }
102
103
104     /**
105      * Sets the exception reference id for this task. This reference
106      * is evaluated when the task is executed.
107      * @since JWare/AntX 0.3
108      **/

109     public void setThrownId(String JavaDoc refId)
110     {
111         setThingRefId(refId);
112     }
113
114
115     /**
116      * Returns the throwable's reference id; can be <i>null</i>
117      * if never set. Required for task to execute properly.
118      * @since JWare/AntX 0.3
119      **/

120     public final String JavaDoc getThrownRefId()
121     {
122         return getThingRefId();
123     }
124
125
126     /**
127      * Returns the names of our refid parameters.
128      * @since JWare/AntX 0.4
129      **/

130     protected String JavaDoc getThingRefParameterName()
131     {
132         return "thrownid|snapshotid";
133     }
134
135
136     /**
137      * Verifies that refered-to thing is either an exception or
138      * an {@linkplain ErrorSnapshot}. Returns the generic object
139      * reference if ok. Never return <i>null</i>.
140      * @throws BuildException if refid undefined or does not
141      * refer-to either an Exception or a ErrorSnapshot
142      * @.sideeffect Also verifies this task is in a valid project
143      **/

144     public final Object JavaDoc getReferencedErrorThing()
145         throws BuildException
146     {
147         verifyInProject_("getErrorRef");
148
149         String JavaDoc refid = getThingRefId();
150         String JavaDoc error = null;
151
152         Object JavaDoc o = getProject().getReference(refid);
153
154         if (o==null) {
155             error = uistrs().get("task.missing.refid", refid);
156         }
157         else if (!(o instanceof Exception JavaDoc) && !(o instanceof ErrorSnapshot)) {
158             error = uistrs().get("task.bad.refid", refid,
159                                  Exception JavaDoc.class.getName()+"|"+ErrorSnapshot.class.getName(),
160                                  o.getClass().getName());
161         }
162         if (error!=null) {
163             log(error,Project.MSG_ERR);
164             throw new BuildException(error,getLocation());
165         }
166         return o;
167     }
168
169
170     /**
171      * Sets the list of captured properties to include in the output.
172      * Use '<i>all</i>' to display all captured properties. Use '<i>none</i>'
173      * to skip outputing properties altogether. By default will dump all
174      * captured properties. Using the empty string is same as using '<i>none</i>.'
175      * @param nameList comma-delimited list of property names (non-null)
176      **/

177     public void setProperties(String JavaDoc nameList)
178     {
179         require_(nameList!=null,"setprops- nonzro list");
180         m_propertiesList= nameList;
181     }
182
183
184     /**
185      * Returns list of captured properties to be output. Returns
186      * "<i>all</i>" if all properties will be output. Returns
187      * "<i>none</i>" or the empty string if no properties will be output.
188      **/

189     public final String JavaDoc getPropertiesNameList()
190     {
191         return m_propertiesList;
192     }
193
194
195     /**
196      * Writes diagnostic contents of an ErrorSnapshot or Throwable
197      * to this task's specified output stream (file|Ant-log).
198      **/

199     protected void echoThing()
200     {
201         OutputStream JavaDoc out= getOutputStream();
202         try {
203             Object JavaDoc thing = getReferencedErrorThing();
204
205             String JavaDoc name;
206             if (thing instanceof ErrorSnapshot) {
207                 name = ((ErrorSnapshot)thing).getName();
208             } else {
209                 name = Tk.leafNameFrom(thing.getClass());
210             }
211
212             DisplayRequest dr= new DisplayRequest(getProject(),name,thing);
213             dr.setFilter(getPropertiesNameList());
214
215             m_displayWorker.print(dr,out);
216
217             if (tryAntLog(out)) {
218                 log(getAntLogString(out),getMsgLevel().getNativeIndex());
219             }
220
221         } catch(IOException JavaDoc ioX) {
222             String JavaDoc ermsg = uistrs().get("task.echo.unable");
223             log(ermsg,Project.MSG_ERR);
224             throw new BuildException(ermsg,getLocation());
225
226         } finally {
227             try { out.close(); } catch(Exception JavaDoc X){/*burp*/}
228             out=null;
229         }
230     }
231
232
233     /**
234      * Ensures we're in a valid target/project and have a valid snapshot
235      * reference id.
236      **/

237     protected void verifyCanExecute_(String JavaDoc calr)
238     {
239         super.verifyCanExecute_(calr);
240         getReferencedErrorThing();//NB: ensures refid -> ErrorSnapshot|Exception
241
}
242
243
244     private String JavaDoc m_propertiesList= Strings.ALL;
245     private final ErrorPrinter m_displayWorker = new ErrorPrinter();
246 }
247
248 /* end-of-EchoErrorTask.java */
249
Popular Tags