KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > init > EchoBundleTask


1 /**
2  * $Id: EchoBundleTask.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.init;
30
31 import java.io.IOException JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.util.Properties JavaDoc;
34
35 import org.apache.tools.ant.BuildException;
36 import org.apache.tools.ant.Project;
37
38 import com.idaremedia.antx.AntX;
39 import com.idaremedia.antx.apis.TestScriptComponent;
40 import com.idaremedia.antx.starters.EchoThingTask;
41
42 /**
43  * Helper task to display contents of a {@linkplain UISMBundle}. Mostly used for debugging
44  * and during unit tests. Example usage:<pre>
45  * &lt;printbundle message="default-msgs" bundleid="default-msgs"/&gt;
46  *
47  * &lt;printbundle message="default-msgs" bundleid="default-msgs"
48  * tofile="/tmp/default-msgs.properties" if="debug.msg.bundles"/&gt;
49  * </pre>
50  *
51  * @since JWare/AntX 0.2
52  * @author ssmc, &copy;2002-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
53  * @version 0.5
54  * @.safety single
55  * @.group impl,helper
56  * @see UISMBundle
57  **/

58
59 public final class EchoBundleTask extends EchoThingTask
60     implements TestScriptComponent
61 {
62     /**
63      * Initializes new EchoBundleTask.
64      **/

65     public EchoBundleTask()
66     {
67         super(AntX.print);
68     }
69
70
71
72     /**
73      * Initializes enclosed CV-labeled EchoBundleTask.
74      **/

75     public EchoBundleTask(String JavaDoc iam)
76     {
77         super(iam);
78     }
79
80
81
82     /**
83      * Initializes the UISMBundle reference id for this task.
84      **/

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

96     public final String JavaDoc getBundleRefId()
97     {
98         return getThingRefId();
99     }
100
101
102
103     /**
104      * Returns the actual {@linkplain UISMBundle} echoed by this
105      * task. Never returns <i>null</i>.
106      * @throws BuildException if bundle refid undefined or not a UISMBundle
107      **/

108     public final UISMBundle getReferencedBundle()
109         throws BuildException
110     {
111         return (UISMBundle)getReferencedThing
112             (UISMBundle.class,"task.uism.bad.refid");
113     }
114
115
116
117     /**
118      * Ensures we're in a valid target/project and have a valid bundle
119      * reference id.
120      * @throws BuildException if verify fails
121      **/

122     protected void verifyCanExecute_(String JavaDoc calr)
123     {
124         super.verifyCanExecute_(calr);
125         getReferencedBundle();//NB: ensures refid -> UISMBundle
126
}
127
128
129
130     /**
131      * Writes the referenced bundle as a properties object to this task's
132      * specified output stream (file|stdout).
133      * @throws BuildException if file I/O occurs
134      **/

135     protected void echoThing() throws BuildException
136     {
137         OutputStream JavaDoc os = getOutputStream();
138         Properties JavaDoc msgs = getReferencedBundle().newProperties(null);
139
140         try {
141             msgs.store(os,getMsg());
142
143             if (tryAntLog(os)) {
144                 log(getAntLogString(os),getMsgLevel().getNativeIndex());
145             }
146
147         } catch(IOException JavaDoc ioX) {
148             String JavaDoc ermsg = uistrs().get("task.echo.unable");
149             log(ermsg,Project.MSG_ERR);
150             throw new BuildException(ermsg,getLocation());
151
152         } finally {
153             try { os.close(); } catch(Exception JavaDoc X){/*burp*/}
154             os=null;
155             msgs.clear();
156             msgs=null;
157         }
158     }
159 }
160
161 /* end-of-EchoBundleTask.java */
162
Popular Tags