KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: CopyMsgTask.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 org.apache.tools.ant.BuildException;
32 import org.apache.tools.ant.Project;
33
34 import com.idaremedia.antx.AntX;
35 import com.idaremedia.antx.ExportScope;
36 import com.idaremedia.antx.FlexString;
37 import com.idaremedia.antx.apis.AntLibFriendly;
38 import com.idaremedia.antx.parameters.FlexValueSupport;
39 import com.idaremedia.antx.starters.MsgTask;
40
41 /**
42  * Helper task that copies a resource bundle based message to a regular Ant property
43  * or an AntX exported property. Optionally applies runtime arguments to templated
44  * strings.
45  * <p>
46  * Examples: <pre>
47  * &lt;copymsg msgid="msg.helloworld" property="default.greeting" msgarg1="${TODAY}"/&gt;
48  * &lt;copymsg msgid="defaults.copyleft" variable="copyleft"/&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 api,helper
56  **/

57
58 public final class CopyMsgTask extends MsgTask
59     implements FlexValueSupport, AntLibFriendly
60 {
61     /**
62      * The default scope used by all CopyMsgTasks.
63      **/

64     public static final ExportScope DEFAULT_SCOPE= ExportScope.THREAD;
65
66
67     /**
68      * Initializes a new CopyMsgTask.
69      **/

70     public CopyMsgTask()
71     {
72         super(AntX.uism);
73     }
74
75
76     /**
77      * Setup this copy task to update the specified property
78      * with generated message.
79      * @param property name of property (non-null)
80      **/

81     public void setProperty(String JavaDoc property)
82     {
83         require_(property!=null, "setProp- nonzro name");
84         checkNewCopyTarget();
85         m_value.set(property);
86         m_value.setIsProperty(true);
87     }
88
89
90     /**
91      * Setup this copy task to update the specified exported
92      * property with generated message.
93      * @param property name of exported property (non-null)
94      **/

95     public void setVariable(String JavaDoc property)
96     {
97         require_(property!=null, "setVar- nonzro name");
98         checkNewCopyTarget();
99         m_value.set(property);
100         m_value.setIsExported(true);
101     }
102
103
104     /**
105      * Sets a variable property's scope. Defaults to the
106      * <i>THREAD</i> scope if undefined.
107      **/

108     public void setScope(ExportScope scope)
109     {
110         require_(scope!=null,"setScop- nonzro");
111         m_scope = scope;
112     }
113
114
115     /**
116      * Returns this task's to-be-created variable's scope.
117      * Never returns <i>null</i>; defaults to <i>THREAD</i>.
118      **/

119     public final ExportScope getScope()
120     {
121         return m_scope;
122     }
123
124
125     /**
126      * Setup this copy task to update the specified project
127      * reference with generated message.
128      * @param refid name of (new) reference (non-null)
129      **/

130     public void setReference(String JavaDoc refid)
131     {
132         require_(refid!=null, "setRef- nonzro refid");
133         checkNewCopyTarget();
134         m_value.set(refid);
135         m_value.setIsReference(true);
136     }
137
138
139     /**
140      * Copies this item's generated message to its target.
141      **/

142     public void execute()
143     {
144         verifyCanExecute_("execute");
145
146         if (testIfCondition() && testUnlessCondition()) {
147             String JavaDoc what = getMsg();
148             String JavaDoc key = m_value.get();
149             Project P = getProject();
150
151             if (m_value.isProperty()) {
152                 if (ExportScope.ALL.equals(getScope())) {
153                     P.setInheritedProperty(key,what);
154                 } else {
155                     P.setNewProperty(key,what);
156                 }
157             }
158             else if (m_value.isExported()) {
159                 ExportScope.setTheProperty(getScope(),P, key,what,false);
160             }
161             else if (m_value.isReference()) {
162                 P.addReference(key,what);
163             }
164         }
165     }
166
167
168     /**
169      * Verifies that one of the three target holders has been
170      * specified.
171      * @throws BuildException if a target has not been specified
172      **/

173     protected void verifyCanExecute_(String JavaDoc calr)
174     {
175         super.verifyCanExecute_(calr);
176
177         String JavaDoc ermsg = null;
178         if (m_value.isLiteral()) {
179             ermsg = uistrs().get("task.needs.this.attr", getTaskName(),
180                                  "property|reference|variable");
181         } else if (getMsgId()==null && getDefaultMsg().length()==0) {
182             ermsg = uistrs().get("task.needs.this.attr", getTaskName(),
183                                  "msgid|message");
184         }
185         if (ermsg!=null) {
186             log(ermsg,Project.MSG_ERR);
187             throw new BuildException(ermsg,getLocation());
188         }
189     }
190
191
192     /**
193      * Ensures that a new 'set' isn't going to overwrite a previously
194      * set copy target (only one permitted).
195      * @throws BuildException if a copy target has already been specified
196      **/

197     private void checkNewCopyTarget()
198     {
199         if (m_copytargets>0) {
200             String JavaDoc ermsg = uistrs().get("task.too.many.flex.attrs");
201             log(ermsg,Project.MSG_ERR);
202             throw new BuildException(ermsg,getLocation());
203         }
204         m_copytargets++;
205     }
206
207
208     private FlexString m_value = new FlexString();
209     private ExportScope m_scope = ExportScope.DEFAULT_SCOPE;
210     private int m_copytargets;
211 }
212
213 /* end-of-CopyMsgTask.java */
214
Popular Tags