KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > starters > MsgTask


1 /**
2  * $Id: MsgTask.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.starters;
30
31 import com.idaremedia.antx.AntX;
32 import com.idaremedia.antx.AssertableTask;
33 import com.idaremedia.antx.MsgGetter;
34 import com.idaremedia.antx.NoiseLevel;
35 import com.idaremedia.antx.go.Iff;
36 import com.idaremedia.antx.go.Unless;
37 import com.idaremedia.antx.helpers.InnerString;
38 import com.idaremedia.antx.helpers.Tk;
39 import com.idaremedia.antx.parameters.Conditional;
40
41 /**
42  * Simple task used to generate <i>msgid</i>-based messages for subclasses. Also useful
43  * to test that resource bundles are where they're supposed to be. By default behaves
44  * like the standard Ant '<i>echo</i>' task only using property-based resource bundles.
45  * If undefined, the messages noise-level is whatever the AntX's runtime default is
46  * defined to be.
47  * <p>
48  * Some examples of how to use MsgTask (assume MsgTask is mapped to the
49  * <code>&lt;show&gt;</code> element):<pre>
50  * &lt;show msgid="msg.helloworld"/&gt;
51  * &lt;show msgid="msg.byeworld" level="verbose"/&gt;
52  * &lt;show msgid="msg.youarehere" msgarg1="Compiling" level="debug"/&gt;
53  * &lt;show msgid="err.no.jdk15" unless="jdk15.present"&gt;
54  * &lt;defaultmsg&gt;Need J2SE 1.5 or later&lt;/defaultmsg&gt;
55  * &lt;/show&gt;
56  * </pre>
57  *
58  * @since JWare/AntX 0.1
59  * @author ssmc, &copy;2002-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
60  * @version 0.5
61  * @.safety single
62  * @.group impl,helper
63  **/

64
65 public class MsgTask extends AssertableTask implements Conditional
66 {
67     /**
68      * Initializes a new MsgTask.
69      **/

70     public MsgTask()
71     {
72         super(AntX.starters);
73     }
74
75
76     /**
77      * Initializes a new CV-tagged MsgTask.
78      * @param iam CV-label (non-null)
79      **/

80     public MsgTask(String JavaDoc iam)
81     {
82         super(iam);
83     }
84
85
86     /**
87      * Initializes a new CV-tagged MsgTask with a predefined
88      * default message.
89      * @param iam CV-label (non-null)
90      * @param defaultMsg default message (non-null)
91      **/

92     public MsgTask(String JavaDoc iam, String JavaDoc defaultMsg)
93     {
94         super(iam);
95         require_(defaultMsg!=null,"ctor- nonzro dfltMsg");
96         m_defaultMsgStr = defaultMsg;
97     }
98
99
100     /**
101      * Adds an if-condition to this echo task.
102      * @since JWare/AntX 0.2
103      **/

104     public void setIf(String JavaDoc property)
105     {
106         m_ifProperty = (property==null) ? "" : property;
107     }
108
109
110     /**
111      * Returns this task's (raw) if-condition if any. Returns
112      * empty string if condition never set.
113      * @since JWare/AntX 0.2
114      **/

115     public String JavaDoc getIfProperty()
116     {
117         return m_ifProperty;
118     }
119
120
121     /**
122      * Adds an unless-condition to this echo task.
123      * @since JWare/AntX 0.2
124      **/

125     public void setUnless(String JavaDoc property)
126     {
127         m_unlessProperty = (property==null) ? "" : property;
128     }
129
130
131     /**
132      * Returns this task's (raw) unless-condition if any. Returns
133      * empty string if condition never set.
134      * @since JWare/AntX 0.2
135      **/

136     public String JavaDoc getUnlessProperty()
137     {
138         return m_unlessProperty;
139     }
140
141
142     /**
143      * Sets this task's preferred message noise level.
144      * @param level new level (non-null)
145      * @see #getPreferredMsgLevel
146      **/

147     public void setMsgLevel(NoiseLevel level)
148     {
149         require_(level!=null,"setLvl- nonzro lvl");
150         m_preferredMsgLevel = level;
151     }
152
153
154     /**
155      * Synonym for {@linkplain #setMsgLevel setMsgLevel} that matches
156      * the standard '<i>echo</i>' task's parameter.
157      * @param nl the noise level (non-null)
158      * @since JWare/AntX 0.2
159      **/

160     public final void setLevel(NoiseLevel nl)
161     {
162         setMsgLevel(nl);
163     }
164
165
166     /**
167      * Returns this task's preferred message noise level. Returns
168      * <i>null</i> if never set. For the message level used when
169      * showing message, see {@linkplain #getMsgLevel}.
170      **/

171     public final NoiseLevel getPreferredMsgLevel()
172     {
173         return m_preferredMsgLevel;
174     }
175
176
177     /**
178      * Returns this task's <em>effective</em> message noise level. If
179      * this task's preferred msg level never set, this method returns
180      * the default message level set at either the project, system, or
181      * AntX-defaults levels. Never returns <i>null</i>.
182      * @see AssertableTask#getDefaultMsgLevel
183      **/

184     public final NoiseLevel getMsgLevel()
185     {
186         NoiseLevel nl= getPreferredMsgLevel();
187         if (nl==null) {
188             nl= getDefaultMsgLevel();
189         }
190         return nl;
191     }
192
193
194     /**
195      * <em>Replaces</em> the default message string associated with
196      * this task. Usually only specified if it is acceptable for resource
197      * bundles to be missing at build-time.
198      * @see #getDefaultMsg
199      **/

200     public void addConfiguredDefaultMsg(InnerString defaultMsgStr)
201     {
202         require_(defaultMsgStr!=null,"addDfltMsg- nonzro str");
203         m_defaultMsgStr= defaultMsgStr.toString(getProject());
204     }
205
206
207     /**
208      * Returns this task's default message string if any. Never
209      * returns <i>null</i> but will return the empty string if this
210      * element never defined.
211      **/

212     public final String JavaDoc getDefaultMsg()
213     {
214         return m_defaultMsgStr;
215     }
216
217
218     /**
219      * Sets the inlined string message emitted by this task. Same
220      * as nesting a default message.
221      * @param string the message (non-null)
222      **/

223     public void setMessage(String JavaDoc string)
224     {
225         require_(string!=null,"setMsg- nonzro string");
226         addConfiguredDefaultMsg(new InnerString(string));
227     }
228
229
230     /**
231      * Shortcut to dynamically define a single additional substition
232      * value in final message. This argument is mapped to the
233      * <i>{2}</i> template variable.
234      **/

235     public final void setMsgArg1(String JavaDoc arg1)
236     {
237         m_arg1= arg1;
238     }
239
240
241     /**
242      * Returns optional first argument to msgid's template string.
243      * This argument is mapped to the <i>{2}</i> template variable.
244      **/

245     public final String JavaDoc getMsgArg1()
246     {
247         return m_arg1;
248     }
249
250
251     /**
252      * Shortcut to dynamically define a second additional substition
253      * value in final message. This argument is mapped to the
254      * <i>{3}</i> template variable.
255      **/

256     public final void setMsgArg2(String JavaDoc arg2)
257     {
258         m_arg2= arg2;
259     }
260
261
262     /**
263      * Returns optional second argument to msgid's template string. This
264      * argument is mapped to the <i>{3}</i> template variable.
265      **/

266     public final String JavaDoc getMsgArg2()
267     {
268         return m_arg2;
269     }
270
271
272     /**
273      * Extension of inherited '<i>getMsg</i>' that will return this
274      * task's default message string if its msgid is undefined.
275      * @see #setMsgArg1
276      * @see #setMsgArg2
277      **/

278     public String JavaDoc getMsg()
279     {
280         String JavaDoc msg = null;
281         boolean chk = false;
282
283         if (m_arg1!=null) {
284             if (m_arg2!=null) {
285                 msg= getMsg(newMsgGetter(m_arg1,m_arg2));
286             } else {
287                 msg= getMsg(newMsgGetter(m_arg1));
288             }
289         } else if (m_arg2!=null) {
290             msg = getMsg(newMsgGetter("",m_arg2));
291         } else {
292             msg = super.getMsg();
293             chk = true;
294         }
295
296         if (chk) {
297             String JavaDoc dfltMsg = getDefaultMsg();
298             if (Tk.isWhitespace(msg)) {
299                 msg = dfltMsg;
300             } else if (msg.equals(getMsgId()) && !Tk.isWhitespace(dfltMsg)) {
301                 msg = dfltMsg;
302             }
303         }
304
305         return msg;
306     }
307
308
309     /**
310      * Extension of inherited '<i>getMsg(MsgGetter)</i>' that will
311      * return this task's default message string if its msgid is
312      * undefined.
313      **/

314     public String JavaDoc getMsg(MsgGetter getr)
315     {
316         String JavaDoc msg = super.getMsg(getr);
317
318         if (Tk.isWhitespace(msg)) {
319             msg = getDefaultMsg();
320         }
321
322         return msg;
323     }
324
325
326     /**
327      * Tests whether or not this task's "if" condition resolves
328      * to a an existing property.
329      * @return <i>true</i>if the "if" property is defined
330      * @since JWare/AntX 0.2
331      **/

332     protected final boolean testIfCondition()
333     {
334         return Iff.allowed(getIfProperty(), getProject());
335     }
336
337
338     /**
339      * Tests whether or not this task's "unless" condition resolves
340      * to an existing property.
341      * @return <i>true</i> if the "unless" property is not defined
342      * @since JWare/AntX 0.2
343      **/

344     protected final boolean testUnlessCondition()
345     {
346         return Unless.allowed(getUnlessProperty(),getProject());
347     }
348
349
350     /**
351      * Work like the default '<i>echo</i>' Ant task except using
352      * UIStringManager underneath if can.
353      **/

354     public void execute()
355     {
356         verifyCanExecute_("execute");
357
358         if (testIfCondition() && testUnlessCondition()) {
359             log(getMsg(), getMsgLevel().getNativeIndex());
360         }
361     }
362
363
364     private NoiseLevel m_preferredMsgLevel;
365     private String JavaDoc m_defaultMsgStr="";
366     private String JavaDoc m_arg1, m_arg2;//NB: additonal args to MessageFormat
367
private String JavaDoc m_ifProperty="";
368     private String JavaDoc m_unlessProperty="";
369 }
370
371
372 /* end-of-MsgTask.java */
373
Popular Tags