KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: EchoItemsTask.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 import java.io.PrintStream JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Properties JavaDoc;
37
38 import org.apache.tools.ant.BuildException;
39 import org.apache.tools.ant.Project;
40 import org.apache.tools.ant.taskdefs.condition.Condition;
41
42 import com.idaremedia.antx.AntX;
43 import com.idaremedia.antx.ExportedProperties;
44 import com.idaremedia.antx.FixtureExaminer;
45 import com.idaremedia.antx.helpers.Strings;
46 import com.idaremedia.antx.helpers.Tk;
47 import com.idaremedia.antx.parameters.FlexValueSupport;
48 import com.idaremedia.antx.parameters.PropertySource;
49 import com.idaremedia.antx.starters.EchoThingTask;
50
51 /**
52  * Test aid that echos properties, variables, etc. Also acts as stub 'true' condition
53  * to facilitate debugging of complex rules. Example usage:<pre>
54  *
55  * &lt;printenv property="ant.version"/&gt;
56  * &lt;printenv reference="my.classpath"/&gt;
57  * &lt;printenv variables="all" properties="all" if="build.debug"/&gt;
58  *
59  * OR (Nested as a dud-condition for debugging):
60  *
61  * &lt;tally trueproperty="blastoff"&gt;
62  * &lt;tally ruleid="..." trueproperty="a.present"/&gt;
63  * &lt;print properties="all" if="tally.debug"/&gt;
64  * &lt;tally trueproperty="a.present"&gt;
65  * &lt;.../&gt;
66  * &lt;/tally&gt;
67  * &lt;print property="a.present" if="tally.debug"/&gt;
68  * &lt;.../&gt;
69  * &lt;/tally&gt;
70  * </pre>
71  *
72  * @since JWare/AntX 0.2
73  * @author ssmc, &copy;2002-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
74  * @version 0.5
75  * @.safety multiple (once configured)
76  * @.group impl,test,helper
77  **/

78
79 public class EchoItemsTask extends EchoThingTask implements Condition, FlexValueSupport
80 {
81     /**
82      * Initializes a new EchoItemsTask instance.
83      **/

84     public EchoItemsTask()
85     {
86         super(AntX.starters);
87     }
88
89
90     /**
91      * Initializes a new CV-labeled EchoItemsTask instance.
92      * @param iam CV-label (non-null)
93      **/

94     protected EchoItemsTask(String JavaDoc iam)
95     {
96         super(iam);
97     }
98
99
100     /**
101      * Initialize this task's project. Also auto-sets this task's
102      * referenced thing to the name of the project if it has one.
103      **/

104     public void setProject(Project project)
105     {
106         super.setProject(project);
107
108         if (project!=null && getThingRefId()==null) {
109             String JavaDoc name = project.getName();
110             if (name==null) {
111                 name = project.getProperty("ant.project.name");
112             }
113             if (name!=null) {
114                 setThingRefId(name);
115             }
116         }
117     }
118
119
120     /**
121      * Sets the list of project properties to include in the output.
122      * Use '<i>all</i>' to display all project properties. Use '<i>user</i>'
123      * to display only user properties. Use a comma-delimited list for
124      * specific properties.
125      * @param nameList comma-delimited list of property names (non-null)
126      **/

127     public void setProperties(String JavaDoc nameList)
128     {
129         require_(nameList!=null,"setProps- nonzro list");
130         m_propertiesList= nameList;
131     }
132
133
134     /**
135      * Shortcut for setting a single property.
136      **/

137     public final void setProperty(String JavaDoc property)
138     {
139         setProperties(property);
140     }
141
142
143     /**
144      * Returns list of project properties to be output. Returns
145      * <i>null</i> if never set (nothing displayed).
146      **/

147     public final String JavaDoc getPropertiesNameList()
148     {
149         return m_propertiesList;
150     }
151
152
153     /**
154      * Sets the by-prefix filter for echoed properties or variables.
155      **/

156     public void setPrefix(String JavaDoc prefix)
157     {
158         require_(prefix!=null && prefix.length()>0,"setPfx- valid str");
159         m_prefix = prefix;
160     }
161
162
163     /**
164      * Returns the by-prefix filter for echoed properties or variables.
165      * Returns <i>null</i> if never set.
166      **/

167     public final String JavaDoc getPrefix()
168     {
169         return m_prefix;
170     }
171
172
173     /**
174      * Sets the list of exported properties to include in the output.
175      * Use '<i>all</i>' to display all exported properties. Use a
176      * comma-delimited list for specific exported properties.
177      * @param nameList comma-delimited list of variable names (non-null)
178      **/

179     public void setVariables(String JavaDoc nameList)
180     {
181         require_(nameList!=null,"setVars- nonzro list");
182         m_variablesList= nameList;
183     }
184
185
186     /**
187      * Shortcut for setting a single exported property.
188      **/

189     public final void setVariable(String JavaDoc variable)
190     {
191         setVariables(variable);
192     }
193
194
195     /**
196      * Shortcut for setting a single exported property.
197      **/

198     public final void setVar(String JavaDoc variable)
199     {
200         setVariables(variable);
201     }
202
203
204     /**
205      * Returns list of exported properties to be output. Returns
206      * '<i>null</i>' if never set (nothing displayed).
207      **/

208     public final String JavaDoc getVariablesNameList()
209     {
210         return m_variablesList;
211     }
212
213
214     /**
215      * Sets the list of project references to include in the output.
216      * Use a comma-delimited list for specific references.
217      * @param nameList comma-delimited list of references names (non-null)
218      **/

219     public void setReferences(String JavaDoc nameList)
220     {
221         require_(nameList!=null,"setRefs- nonzro list");
222         m_refsList= nameList;
223     }
224
225
226     /**
227      * Shortcut for setting a single reference.
228      **/

229     public final void setReference(String JavaDoc refid)
230     {
231         require_(refid!=null,"setRef- nonzro ref");
232         setReferences(refid);
233     }
234
235
236     /**
237      * Returns list of project references to be output. Returns
238      * '<i>null</i>' if never set (nothing displayed).
239      **/

240     public final String JavaDoc getReferencesNameList()
241     {
242         return m_refsList;
243     }
244
245
246     /**
247      * Sets the by-class filter for echoed references.
248      **/

249     public void setKindOf(Class JavaDoc claz)
250     {
251         require_(claz!=null,"setKindOf- nonzro claz");
252         m_kindOfClass = claz;
253     }
254
255
256     /**
257      * Returns the by-class filter for echoed references. Returns
258      * <i>Object.class</i> if never set.
259      **/

260     public final Class JavaDoc getKindOfFilterClass()
261     {
262         return m_kindOfClass==null ? Object JavaDoc.class : m_kindOfClass;
263     }
264
265
266     /**
267      * Sets whether reference printing will exclude out-of-scope
268      * reference identifiers. Defaults <i>true</i> for backward
269      * compatibility.
270      * @since JWare/AntX 0.4
271      **/

272     public void setUnknowns(boolean yn)
273     {
274         m_includeUnknowns= yn;
275     }
276
277
278     /**
279      * Returns <i>true</i> if this task will include to-be-determined
280      * references in listings.
281      * @since JWare/AntX 0.4
282      **/

283     public final boolean willIncludeUnknowns()
284     {
285         return m_includeUnknowns;
286     }
287
288
289
290     /**
291      * Returns this task's project as the referenced thing.
292      **/

293     protected Object JavaDoc getReferencedThing(Class JavaDoc ofKind, String JavaDoc msgid)
294     {
295         return getProject();//that's-all-she-wrote
296
}
297
298
299     /**
300      * Write the appropriate set of project properties to output stream.
301      * @param P project from which properties read (non-null)
302      * @param os output stream (non-null)
303      * @see #setProperties
304      * @return <i>true</i> if properties written
305      * @throws IOException if any I/O error occurs on stream
306      **/

307     protected boolean echoProperties(final Project P, OutputStream JavaDoc os)
308         throws IOException JavaDoc
309     {
310         String JavaDoc list= getPropertiesNameList();
311         if (!Tk.isWhitespace(list)) {
312
313             Properties JavaDoc allP= new Properties JavaDoc();
314
315             PropertySource domain= PropertySource.from(list);
316             if (domain!=null) {
317                 allP.putAll(FixtureExaminer.copyOfProperties(domain,P));
318                 if (getPrefix()!=null) {//oye...
319
Iterator JavaDoc itr= allP.keySet().iterator();
320                     while (itr.hasNext()) {
321                         String JavaDoc key = itr.next().toString();
322                         if (!key.startsWith(m_prefix)) {
323                             itr.remove();
324                         }
325                     }
326                 }
327             } else {
328                 List JavaDoc wanted = Tk.splitList(list);
329                 for (int i=0,N=wanted.size();i<N;i++) {
330                     String JavaDoc key = (String JavaDoc)wanted.get(i);
331                     allP.setProperty(key, String.valueOf(P.getProperty(key)));
332                 }
333                 wanted=null;
334             }
335
336             String JavaDoc header= getMsg(newMsgGetter(uistrs().get("echo.label.properties")));
337             allP.store(os,header);
338
339             allP.clear();
340             allP=null;
341             return true;
342         }
343         return false;
344     }
345
346
347     /**
348      * Write the appropriate set of exported properties to output stream.
349      * @param P project information
350      * @param os output stream (non-null)
351      * @see #setVariables
352      * @return <i>true</i> if properties written
353      * @throws IOException if any I/O error occurs on stream
354      **/

355     protected boolean echoVariables(final Project P, OutputStream JavaDoc os)
356         throws IOException JavaDoc
357     {
358         String JavaDoc list= getVariablesNameList();
359         if (!Tk.isWhitespace(list)) {
360             Properties JavaDoc allP;
361             String JavaDoc ll= Tk.lowercaseFrom(list);
362
363             if (Strings.ALL.equals(ll)) {
364                 allP= ExportedProperties.copy(null);
365                 if (getPrefix()!=null) {//oye...
366
Iterator JavaDoc itr= allP.keySet().iterator();
367                     while (itr.hasNext()) {
368                         String JavaDoc key = itr.next().toString();
369                         if (!key.startsWith(m_prefix)) {
370                             itr.remove();
371                         }
372                     }
373                 }
374             } else {
375                 allP= ExportedProperties.copy(null);
376                 List JavaDoc wanted = Tk.splitList(list);
377                 allP.keySet().retainAll(wanted);
378                 if (wanted.size()>allP.size()) {//insert "null" strings
379
for (int i=0,N=wanted.size();i<N;i++) {
380                         String JavaDoc key = (String JavaDoc)wanted.get(i);
381                         if (allP.getProperty(key)==null) {
382                             allP.setProperty(key,Strings.NULL);
383                         }
384                     }
385                 }
386                 wanted=null;
387             }
388
389             String JavaDoc header= getMsg(newMsgGetter(uistrs().get("echo.label.variables")));
390             allP.store(os,header);
391
392             allP.clear();
393             allP=null;
394             return true;
395         }
396         return false;
397     }
398
399
400
401     /**
402      * Returns a marker string for unresolved references (still unknown).
403      **/

404     protected final String JavaDoc unresolvedString(Project P, String JavaDoc key)
405     {
406         return P.getName()+":UnresolvedReference@"+key;
407     }
408
409
410
411     /**
412      * Write the appropriate set of project references to output stream.
413      * Uses each targetted reference's 'toString' method to generate output.
414      * @param P project information
415      * @param os output stream (non-null)
416      * @see #setReferences
417      * @return <i>true</i> if properties written
418      * @throws IOException if any I/O error occurs on stream
419      **/

420     protected boolean echoReferences(final Project P, OutputStream JavaDoc os)
421         throws IOException JavaDoc
422     {
423         String JavaDoc list= getReferencesNameList();
424         if (!Tk.isWhitespace(list)) {
425
426             Properties JavaDoc allP= new Properties JavaDoc();
427
428             Iterator JavaDoc keysitr;
429             if (Strings.ALL.equals(Tk.lowercaseFrom(list))) {
430                 keysitr= P.getReferences().keySet().iterator(); //?MT-safe..copy?
431
} else {
432                 keysitr= Tk.splitList(list).iterator();
433             }
434             while (keysitr.hasNext()) {
435                 String JavaDoc key = (String JavaDoc)keysitr.next();
436
437                 Object JavaDoc object = FixtureExaminer.trueReference(P,key);//@since AntX 0.4
438
if (object==FixtureExaminer.IGNORED_REFERENCE) {
439                     if (!willIncludeUnknowns()) {
440                         continue;
441                     }
442                     object = unresolvedString(P,key);
443                 }
444                 if (m_kindOfClass==null/*allowAll*/ || object==null/*passthru*/ ||
445                     (m_kindOfClass.isInstance(object))) {
446                     allP.setProperty(key, Tk.stringFrom(object,P));
447                 }
448             }
449
450             String JavaDoc header= getMsg(newMsgGetter(uistrs().get("echo.label.references")));
451             allP.store(os,header);
452
453             allP.clear();
454             allP=null;
455             return true;
456         }
457         return false;
458     }
459
460
461     /**
462      * Echoes the project items to this task's output stream.
463      **/

464     protected void echoThing()
465     {
466         OutputStream JavaDoc os = getOutputStream();
467         try {
468             boolean p= echoProperties(getProject(),os);
469             boolean v= echoVariables(getProject(),os);
470             boolean r= echoReferences(getProject(),os);
471
472             if (!p && !v && !r) {
473                 if (getMsgId()!=null || getDefaultMsg()!=null) {
474                     String JavaDoc s = getMsg();
475                     new PrintStream JavaDoc(os).println(s);
476                 }
477             }
478             if (tryAntLog(os)) {
479                 log(getAntLogString(os),getMsgLevel().getNativeIndex());
480             }
481
482         } catch(IOException JavaDoc ioX) {
483             String JavaDoc ermsg = uistrs().get("task.echo.unable");
484             log(ermsg,Project.MSG_ERR);
485             throw new BuildException(ermsg,getLocation());
486
487         } finally {
488             try { os.close(); } catch(Exception JavaDoc X){/*burp*/}
489             os=null;
490         }
491     }
492
493
494     /**
495      * Echoes the list of items then always returns <i>true</i>.
496      **/

497     public final boolean eval()
498     {
499         execute();
500         return true;
501     }
502
503
504     /**
505      * Should always be able to execute if in legitimate project.
506      **/

507     protected final void verifyCanExecute_(String JavaDoc calr)
508     {
509         verifyInProject_(calr);
510     }
511
512
513     private String JavaDoc m_propertiesList;
514     private String JavaDoc m_variablesList;
515     private String JavaDoc m_refsList;
516     private Class JavaDoc m_kindOfClass = Object JavaDoc.class;//NB:allow 'em all!
517
private String JavaDoc m_prefix;//NB:allow 'em all!
518
private boolean m_includeUnknowns=true;//NB:show 'em all!
519
}
520
521 /* end-of-EchoItemsTask.java */
522
Popular Tags