KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > solo > UnassignTask


1 /**
2  * $Id: UnassignTask.java 186 2007-03-16 13:42:35Z ssmc $
3  * Copyright 2004-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.solo;
30
31 import org.apache.tools.ant.BuildException;
32 import org.apache.tools.ant.Project;
33 import org.apache.tools.ant.PropertyHelper;
34
35 import com.idaremedia.antx.AntX;
36 import com.idaremedia.antx.AssertableTask;
37 import com.idaremedia.antx.ExportedProperties;
38 import com.idaremedia.antx.apis.AntLibFriendly;
39 import com.idaremedia.antx.parameters.FeedbackLevel;
40 import com.idaremedia.antx.parameters.FlexValueSupport;
41 import com.idaremedia.antx.parameters.IsA;
42
43 /**
44  * Helper task that removes fixture elements like variables, references, and even
45  * properties. You cannot remove command properties (like system and command-line
46  * directives).
47  * <p/>
48  * <b>Example Usage:</b><pre>
49  * &lt;unassign variable="__bn"/&gt;
50  * &lt;unassign reference="ant.PropertyHelper" feedback="quiet"/&gt;
51  * &lt;unassign property="loopcount"/&gt;
52  * </pre>
53  *
54  * @since JWare/AntX 0.5
55  * @author ssmc, &copy;2004-2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
56  * @version 0.5
57  * @.safety single
58  * @.group api,helper
59  * @see ExportTask
60  **/

61
62 public class UnassignTask extends AssertableTask
63     implements FlexValueSupport, AntLibFriendly
64 {
65     private static final int _NOTYPE= -1;//NB: must be invalid array index!
66

67
68     /**
69      * Initializes a new unassign task instance. This task's
70      * source must be defined before it is executed.
71      **/

72     public UnassignTask()
73     {
74         super(AntX.fixture+"UnassignTask:");
75     }
76
77
78 // ---------------------------------------------------------------------------------------
79
// Script-facing Parameters:
80
// ---------------------------------------------------------------------------------------
81

82     /**
83      * Sets the property which this task will unassign. The property is
84      * checked against the project's current list of user properties. If
85      * there is a match, a build exception is thrown.
86      * @param property the property to unassign (non-null)
87      * @throws BuildException if property is an immutable property.
88      */

89     public void setProperty(String JavaDoc property)
90     {
91         require_(property!=null,"setProperty- nonzro name");
92         verifyNotNamed();
93         if (getProject().getUserProperty(property)!=null) {
94             String JavaDoc error = getAntXMsg("task.cant.repl.userprop",property);
95             log(error,Project.MSG_ERR);
96             throw new BuildException(error, getLocation());
97         }
98         m_name = property;
99         m_type = IsA.PROPERTY_INDEX;
100     }
101
102
103
104     /**
105      * Sets the variable which this task will unassign. The variable
106      * is removed unconditionally.
107      * @param variable name of variable to unassign (non-null)
108      */

109     public void setVariable(String JavaDoc variable)
110     {
111         require_(variable!=null,"setVariable- nonzro name");
112         verifyNotNamed();
113         m_name = variable;
114         m_type = IsA.VARIABLE_INDEX;
115     }
116
117
118
119     /**
120      * Synonym for {@linkplain #setVariable setVariable}.
121      **/

122     public final void setVar(String JavaDoc variable)
123     {
124         setVariable(variable);
125     }
126
127
128
129     /**
130      * Sets the reference which this task will unassign. The reference
131      * is removed unconditionally; however, references that begin with
132      * "<span class="src">ant.</span>" will cause a warning to be
133      * registered unless the {@linkplain #setFeedback quiet option} has
134      * been set.
135      * @param refid name of reference to unassign (non-null)
136      */

137     public void setReference(String JavaDoc refid)
138     {
139         require_(refid!=null,"setReference- nonzro name");
140         verifyNotNamed();
141         m_name = refid;
142         m_type = IsA.REFERENCE_INDEX;
143     }
144
145
146
147     /**
148      * Instructs this unassigner whether to issue warnings when
149      * told to unassign seemingly reserved fixture bits.
150      * @param level feedback level (non-null).
151      * @throws BuildException if unrecognized feedback level.
152      **/

153     public void setFeedback(String JavaDoc level)
154     {
155         require_(level!=null,"setFeedback- nonzro level");
156         FeedbackLevel fb = FeedbackLevel.from(level);
157         if (fb==null) {
158             String JavaDoc e = getAntXMsg("task.illegal.param.value",
159                             getTaskName(), level,"feedbacklevel");
160             log(e, Project.MSG_ERR);
161             throw new BuildException(e, getLocation());
162         }
163         m_fbLevel = fb;
164     }
165
166
167 // ---------------------------------------------------------------------------------------
168
// Execution:
169
// ---------------------------------------------------------------------------------------
170

171     /**
172      * Verifies that a source element has been specified.
173      * @param calr calling method (non-null)
174      * @throws BuildException if neither a variable, reference,
175      * nor property has been specified.
176      */

177     protected void verifyCanExecute(String JavaDoc calr)
178     {
179         super.verifyCanExecute_(calr);
180         if (m_name==null) {
181             String JavaDoc error = getAntXMsg("task.needs.this.attr",
182                 getTaskName(), "property|reference|variable");
183             log(error, Project.MSG_ERR);
184             throw new BuildException(error, getLocation());
185         }
186     }
187
188
189
190     /**
191      * Removes the indicated fixture element. Note that properties
192      * cannot be completely removed (no way via APIs); instead, their
193      * values are (re)set to the empty string.
194      */

195     public void execute()
196     {
197         verifyCanExecute_("exec");
198
199         final Project myproject = getProject();
200         switch(m_type) {
201             case IsA.PROPERTY_INDEX: {
202                 PropertyHelper ph = PropertyHelper.getPropertyHelper(myproject);
203                 synchronized(ph) {
204                     if (ph.getProperty(null,m_name)!=null) {
205                         ph.setProperty(null,m_name,"",false);
206                     }
207                 }
208                 break;
209             }
210             case IsA.VARIABLE_INDEX: {
211                 ExportedProperties.delete(m_name);
212                 break;
213             }
214             case IsA.REFERENCE_INDEX: {
215                 if (m_name.startsWith("ant.") &&
216                     !FeedbackLevel.isQuietish(m_fbLevel,true)) {
217                     String JavaDoc warning = getAntXMsg("unassign.warn.ant.runtime.ref",m_name);
218                     log(warning, Project.MSG_VERBOSE);
219                 }
220                 myproject.getReferences().remove(m_name);
221                 break;
222             }
223         }
224     }
225
226
227
228     /**
229      * Ensures this task has not been assigned a fixture data
230      * source already.
231      * @param errid [optiona] id of error message to use
232      **/

233     protected final void verifyNotNamed(String JavaDoc errid)
234     {
235         if (m_name!=null) {
236             if (errid==null) {
237                 errid = "task.too.many.flex.attrs";
238             }
239             String JavaDoc error = getAntXMsg(errid);
240             log(error, Project.MSG_ERR);
241             throw new BuildException(error, getLocation());
242         }
243     }
244
245
246     private void verifyNotNamed()
247     {
248         verifyNotNamed(null);
249     }
250
251
252     private String JavaDoc m_name;
253     private int m_type=_NOTYPE;
254     private FeedbackLevel m_fbLevel=FeedbackLevel.NORMAL;
255 }
256
257 /* end-of-UnassignTask.java */
Popular Tags