KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: CopyPropertyTask.java 186 2007-03-16 13:42:35Z 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.solo;
30
31 import org.apache.tools.ant.BuildException;
32 import org.apache.tools.ant.Project;
33 import org.apache.tools.ant.taskdefs.Property;
34
35 import com.idaremedia.antx.AntXFixture;
36 import com.idaremedia.antx.ExportScope;
37 import com.idaremedia.antx.FlexString;
38 import com.idaremedia.antx.apis.AntLibFriendly;
39 import com.idaremedia.antx.go.Unless;
40 import com.idaremedia.antx.parameters.FlexValueSupport;
41 import com.idaremedia.antx.parameters.TransformEnabled;
42 import com.idaremedia.antx.parameters.TransformHelper;
43 import com.idaremedia.antx.parameters.TrimEnabled;
44 import com.idaremedia.antx.parameters.ValueTransform;
45
46 /**
47  * Brain-dead Property extension to support flexible value-source attributes
48  * while allowing all those "only-allow-nested-Properties" tasks to work properly. Unlike
49  * a regular property a <i>CopyPropertyTask</i> can initialize its property from an literal
50  * value (like standard), another property's value, a variable property's value, or
51  * a stringifiable project reference. Usually defined &lt;copyproperty&gt;.
52  * <p>
53  * This task pre-dates the &lt;assignimport&gt; task, so some of its variable-support
54  * functionality now overlaps with that task.
55  * <p>
56  * <b>Example Usage:</b><pre>
57  * &lt;copyproperty name="sourcefile" value="${ant.file}"/&gt;
58  * &lt;copyproperty name="someProperty" property="delayed.created.property"/&gt;
59  * &lt;copyproperty name="outdir" value="${tempdir}/${user.name}" transform="ospath"/&gt;
60  *
61  * &lt;assign var="theVariable" value="100"/&gt;...
62  * &lt;copyproperty name="regularProperty" variable="theVariable"/&gt;
63  *
64  * &lt;copyproperty name="val.lowercase" value="val.MixedCase" transform="lowercase"/&gt;
65  * &lt;copyproperty name="system.property" variable="theVariable" scope="all"/>
66  *
67  * &lt;call targets="a,b,c"&gt;
68  * &lt;property name="property1" variable="variable1"/&gt;
69  * &lt;property name="property2" property="property2"/&gt;
70  * &lt;property name="property3" reference="source.file"/&gt;
71  * &lt;/call&gt;
72  * </pre>
73  *
74  * @since JWare/AntX 0.1 (moved from antx since AntX 0.2)
75  * @author ssmc, &copy;2002-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
76  * @version 0.5
77  * @.safety single
78  * @.group api,helper
79  * @see CopyReferenceTask
80  **/

81
82 public final class CopyPropertyTask extends Property
83     implements FlexValueSupport, TrimEnabled, TransformEnabled, AntLibFriendly
84 {
85     /**
86      * Initializes new blank CopyPropertyTask.
87      **/

88     public CopyPropertyTask()
89     {
90         super();
91     }
92
93
94     /**
95      * Update this task's project and any dependent project component.
96      **/

97     public void setProject(Project P)
98     {
99         super.setProject(P);
100         m_XValue.setProject(P);
101     }
102
103
104     /**
105      * Adds an unless-condition to this property task.
106      * @since JWare/AntX 0.3
107      **/

108     public void setUnless(String JavaDoc property)
109     {
110         m_unlessProperty = (property==null) ? "" : property;
111     }
112
113
114     /**
115      * Returns this task's (raw) unless-condition if any.
116      * Returns empty string if condition never set.
117      * @since JWare/AntX 0.3
118      **/

119     public final String JavaDoc getUnlessProperty()
120     {
121         return m_unlessProperty;
122     }
123
124
125
126     /**
127      * Tells this task to trim the source value before
128      * saving to the target.
129      * @since JWare/AntX 0.3
130      **/

131     public void setTrim(boolean b)
132     {
133         m_XValue.setTrim(b);
134     }
135
136
137     /**
138      * Returns <i>true</i> if this task will trim the value
139      * before saving to the target.
140      * @since JWare/AntX 0.3
141      **/

142     public boolean willTrim()
143     {
144         return m_XValue.isTrimmed();
145     }
146
147
148     /**
149      * Sets the value transform for the value copied to
150      * the target.
151      * @since JWare/AntX 0.3
152      **/

153     public final void setTransform(ValueTransform t)
154     {
155         m_T = t==null ? ValueTransform.NONE
156             : ValueTransform.from(t.getValue())/*normalize*/;
157     }
158
159
160     /**
161      * Returns the value transformation this task will
162      * perform on the to-be-copied value. Will return
163      * {@linkplain ValueTransform#NONE} if never set.
164      * @since JWare/AntX 0.3
165      **/

166     public final ValueTransform getTransform()
167     {
168         return m_T;
169     }
170
171
172
173
174     /**
175      * Sets a literal value for this task to copy to a
176      * local property. This is the inherited behavior.
177      * @param value value of property (substitution done)
178      **/

179     public void setValue(String JavaDoc value)
180     {
181         super.setValue(value);
182         m_XValue.set(value);
183         m_XValue.setIsLiteral();//NB:clears all is-a flags
184
}
185
186
187     /**
188      * Sets name of variable property to copy.
189      * @param variable name of variable property (non-null)
190      * @since JWare/AntX 0.2
191      **/

192     public void setVariable(String JavaDoc variable)
193     {
194         setValue(variable);
195         m_XValue.setIsExported(true);
196     }
197
198
199     /**
200      * Synonym for {@linkplain #setVariable setVariable}.
201      **/

202     public final void setVar(String JavaDoc variable)
203     {
204         setVariable(variable);
205     }
206
207
208     /**
209      * Returns <i>true</i> if this task will copy the contents
210      * of a variable to a local property.
211      * @since JWare/AntX 0.2
212      **/

213     public final boolean isVariable()
214     {
215         return m_XValue.isExported();
216     }
217
218
219     /**
220      * Sets name of local property to copy.
221      * @param property name of property (non-null)
222      * @since JWare/AntX 0.2
223      **/

224     public void setProperty(String JavaDoc property)
225     {
226         setValue(property);
227         m_XValue.setIsProperty(true);
228     }
229
230
231     /**
232      * Returns <i>true</i> if this task will copy the contents
233      * of one local property to another local property.
234      * @since JWare/AntX 0.2
235      **/

236     public final boolean isProperty()
237     {
238         return m_XValue.isProperty();
239     }
240
241
242     /**
243      * Sets name of local reference to copy.
244      * @param refid identity of reference object (non-null)
245      * @since JWare/AntX 0.2
246      **/

247     public void setReference(String JavaDoc refid)
248     {
249         setValue(refid);
250         m_XValue.setIsReference(true);
251     }
252
253
254     /**
255      * Returns <i>true</i> if this task will copy the stringified
256      * version of a project reference to a local property.
257      * @since JWare/AntX 0.2
258      **/

259     public final boolean isReference()
260     {
261         return m_XValue.isReference();
262     }
263
264
265
266     /**
267      * Sets this new-property operation's scope to either the
268      * project level (normal) or system level.
269      * @param scope operation's scope ("system" or "project")
270      * @since JWare/AntX 0.5
271      **/

272     public void setScope(String JavaDoc scope)
273     {
274         m_scope = ExportScope.from(scope,ExportScope.PROJECT);
275     }
276
277
278
279     /**
280      * Returns <em>current</em> value of underlying thing being
281      * copied (literal, property, variable, or reference) <em>or</em>
282      * if source is undefined, returns the source's name. Will
283      * return <i>null</i> if a source has never been defined.
284      **/

285     public final String JavaDoc getValue()
286     {
287         if (m_XValue.isUndefined()) {
288             return null;
289         }
290         String JavaDoc xvalue = m_XValue.getValue(getProject());
291         if (xvalue==null) {
292             return m_XValue.get();//The *name* of the source as-is!
293
}
294
295         ValueTransform t= getTransform();
296         if (t!=ValueTransform.NONE) {//done!
297
return TransformHelper.apply(t,xvalue,getProject());
298         }
299         return xvalue;
300     }
301
302
303
304     /**
305      * Returns <i>true</i> if this property is <em>just</em> a simple
306      * a one(value)-to-one(property) definition.
307      **/

308     public final boolean isSimpleAssignment()
309     {
310         return (getFile()==null &&
311                 getResource()==null &&
312                 getUrl()==null && /*@since Ant1.6*/
313                 getRefid()==null &&
314                 !m_XValue.isUndefined());
315     }
316
317
318
319     /**
320      * Tries to initialize a project property based on this task's
321      * specified value source. If multiple property sources have been
322      * defined (like "file" <i>and</i> "resource") this method defaults
323      * to the inherited behavior.
324      * @throws BuildException if is a system property set and ant process
325      * does not have permission.
326      **/

327     public void execute()
328     {
329         /**
330          * Heavy-handed: check if is a simple value->property
331          * assignment. No combinations are supported.
332          **/

333         if (Unless.allowed(m_unlessProperty,getProject())) {
334             if (getName()!=null && isSimpleAssignment()) {
335                 if (!ExportScope.ALL.equals(m_scope)) {
336                     addProperty(getName(), getValue());
337                 }
338                 else {//@since AntX 0.5
339
String JavaDoc name1 = getName();
340                     String JavaDoc valu = getValue();
341                     try {
342                         System.setProperty(name1, valu);
343                     } catch(SecurityException JavaDoc secX) {
344                         throw new BuildException(secX,getLocation());
345                     }
346                     AntXFixture.unsetProperty(name1);
347                     getProject().setInheritedProperty(name1,valu);
348                 }
349             } else {
350                 super.execute();
351             }
352         }
353     }
354
355
356     private FlexString m_XValue = new FlexString();
357     private String JavaDoc m_unlessProperty="";
358     private ValueTransform m_T= ValueTransform.NONE;
359     private ExportScope m_scope = ExportScope.PROJECT;
360 }
361
362 /* end-of-CopyPropertyTask.java */
363
Popular Tags