KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > construct > AlterTaskTask


1 /**
2  * $Id: AlterTaskTask.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2005 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 (LGPL) as published
8  * by the Free Software Foundation; either version 2.1 of the License, or (at your option)
9  * any 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 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 GNU 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.construct;
30
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import org.apache.tools.ant.BuildException;
35 import org.apache.tools.ant.DynamicAttribute;
36 import org.apache.tools.ant.Project;
37 import org.apache.tools.ant.Task;
38 import org.apache.tools.ant.TaskContainer;
39 import org.apache.tools.ant.UnknownElement;
40
41 import com.idaremedia.antx.AntX;
42 import com.idaremedia.antx.AntXFixture;
43 import com.idaremedia.antx.AssertableTask;
44 import com.idaremedia.antx.FixtureExaminer;
45 import com.idaremedia.antx.apis.Requester;
46 import com.idaremedia.antx.helpers.Tk;
47 import com.idaremedia.antx.ownhelpers.TaskExaminer;
48 import com.idaremedia.antx.ownhelpers.UEContainerProxy;
49
50 /**
51  * Modifies a previously cached task instance. Note that you can only <em>add</em>
52  * attributes and sub-components to an existing item; you cannot remove previously
53  * installed bits. The "<span class="src">name</span>" attribute is reserved by
54  * the AlterTask instance; all other parameters are treated as input to the task
55  * under construction. You can prefix each parameter's name with a "addparam-"
56  * string to mark it as a parameter (this allows you set a parameter called "name"
57  * also).
58  * <p/>
59  * If you use the &lt;altertask&gt; task within an AntX looping construct, you need
60  * to be careful about using the loop cursor as an input value to any added parameter
61  * or nested element. If the cursor property is not evaluated <em>before</em> the
62  * loop exists, the property cannot be determined (because it lives only as long
63  * as the executing loop block does).
64  * <p/>
65  * <b>Example Usage:</b><pre>
66  * &lt;createtask name="${$var:_junitinstance}"&gt;
67  * &lt;junit printsummary="yes" haltonfailure="yes"&gt;
68  * &#8230;
69  * &lt;/junit&gt;
70  * &lt;/createtask&gt;
71  * &lt;do true="${$notwhitespace:@{classpathref}}"&gt;
72  * &lt;<b>altertask</b> name="${$var:_junitinstance}"&gt;
73  * &lt;classpath refid="@{classpathref}"/&gt;
74  * &lt;/altertask&gt;
75  * &lt;/do&gt;
76  * </pre>
77  *
78  * @since JWare/AntX 0.5
79  * @author ssmc, &copy;2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
80  * @version 0.5
81  * @.safety single
82  * @.group api,helper
83  * @see CreateTaskTask
84  **/

85
86 public final class AlterTaskTask extends AssertableTask
87     implements TaskContainer, DynamicAttribute
88 {
89     /**
90      * Initializes a new altertask task instance.
91      **/

92     public AlterTaskTask()
93     {
94         super(AntX.utilities+"AlterTaskTask:");
95     }
96
97
98     
99     /**
100      * Tells this editor the reference id of the task under construction.
101      * This task should've been installed by the AntX task creator method.
102      * @param refId reference id of task under construction (non-null)
103      **/

104     public void setName(String JavaDoc refId)
105     {
106         require_(refId!=null,"setName- nonzro refid");
107         m_itemUC = (UEContainerProxy)FixtureExaminer.getReferencedObject(getProject(),
108             new Requester.ForComponent(this), refId, UEContainerProxy.class);
109     }
110
111
112
113     /**
114      * Tells this editor to immediately resolve properties in
115      * proxy objects.
116      * @param inLoop <i>true</i> to resolve.
117      **/

118     public void setResolveProperties(boolean inLoop)
119     {
120         m_tryResolveProperties = inLoop;
121     }
122
123
124
125     /**
126      * Tells this editor its modifications apply to a nested element
127      * one level inside the target component. Only a single level (to
128      * grandchild) is supported.
129      * @param childElementName local name of nested element
130      **/

131     public void setChild(String JavaDoc childElementName)
132     {
133         m_childElementName = childElementName;
134     }
135
136
137
138     /**
139      * Gives this wrapper an override for the named parameter. Only
140      * allowed if this task's "fixed" flag was cleared.
141      * @param param ARM parameter (non-null)
142      * @param value ARM parameter value (non-null)
143      * @throws BuildException the named attribute is invalid
144      */

145     public void setDynamicAttribute(String JavaDoc param, String JavaDoc value)
146     {
147         require_(param!=null,"setDynAttr- nonzro param");
148         if (param.startsWith("addparam-")) {
149             param = param.substring("addparam-".length());
150             if (param.length()==0) {
151                 String JavaDoc err = getAntXMsg("task.unsupported.attr",getTaskName(),"");
152                 log(err,Project.MSG_ERR);
153                 throw new BuildException(err,getLocation());
154             }
155         }
156         if (m_tryResolveProperties) {
157             value = tryResolve(getProject(),value);
158         }
159         m_itemAtrs.put(param,value);
160     }
161
162
163
164     /**
165      * Stashes the nested element's UE proxy for subsequent installation
166      * inside the main item under construction.
167      * @param task the unknown element task proxy (non-null)
168      **/

169     public void addTask(Task task)
170     {
171         require_(task instanceof UnknownElement, "addTask- is UE proxy");
172         m_nestedElems.add(task);
173     }
174
175
176
177     /**
178      * Installs all named attributes and nested elements into main item
179      * under construction.
180      */

181     public void execute()
182     {
183         verifyCanExecute_("exec");
184         final boolean grandchild = m_childElementName!=null;
185
186         if (!m_nestedElems.isEmpty()) {
187             for (int i=0,N=m_nestedElems.size();i<N;i++) {
188                 UnknownElement ue = (UnknownElement)m_nestedElems.get(i);
189                 if (m_tryResolveProperties) {
190                     ue = TaskExaminer.copyUEProxy
191                             (ue,ue.getProject(),ue.getOwningTarget(),true);
192                 }
193                 if (grandchild) {
194                     m_itemUC.addGrandchildTask(ue,m_childElementName);
195                 } else {
196                     m_itemUC.addTask(ue);
197                 }
198             }
199         }
200
201         if (!m_itemAtrs.isEmpty()) {
202             if (grandchild) {
203                 m_itemUC.setGrandchildAttributes(m_itemAtrs,m_childElementName);
204             } else {
205                 m_itemUC.setAttributes(m_itemAtrs);
206             }
207         }
208
209         m_itemUC = null;
210         m_nestedElems = null;
211         m_itemAtrs = null;
212     }
213
214
215
216     /**
217      * Ensures we've been given a reference to a task to perform.
218      */

219     protected void verifyCanExecute_(String JavaDoc calr)
220     {
221         verifyInProject_(calr);
222
223         if (m_itemUC==null) {
224             String JavaDoc ermsg = uistrs().get("task.needs.this.attr", getTaskName(), "name");
225             log(ermsg,Project.MSG_ERR);
226             throw new BuildException(ermsg,getLocation());
227         }
228     }
229
230
231     /**
232      * Tries to immediately determine the value of an attribute resolving
233      * any context-dependent properties.
234      * @param P project (non-null)
235      * @param string string to resolve (non-null)
236      * @return resolved string or original if could not resolve now.
237      **/

238     private static String JavaDoc tryResolve(final Project P, final String JavaDoc string)
239     {
240         try {
241             return Tk.resolveString(P,string);
242         } catch(BuildException tooSoonX) {
243             return string;
244         }
245     }
246
247
248
249     private UEContainerProxy m_itemUC;//item under construction (non-null after name known)
250
private List JavaDoc m_nestedElems = AntXFixture.newList();
251     private Map JavaDoc m_itemAtrs = AntXFixture.newMap();
252     private boolean m_tryResolveProperties;//NB:off-by-default (only in loops)
253
private String JavaDoc m_childElementName;
254 }
255
256 /* end-of-AlterTaskTask.java */
Popular Tags