KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > fixture > helpers > DataDefTask


1 /**
2  * $Id: DataDefTask.java 180 2007-03-15 12:56:38Z 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.fixture.helpers;
30
31 import java.util.Map JavaDoc;
32
33 import org.apache.tools.ant.BuildException;
34 import org.apache.tools.ant.ComponentHelper;
35 import org.apache.tools.ant.DynamicElementNS;
36 import org.apache.tools.ant.Project;
37 import org.apache.tools.ant.ProjectHelper;
38 import org.apache.tools.ant.Task;
39
40 import com.idaremedia.antx.AntX;
41 import com.idaremedia.antx.AssertableTask;
42 import com.idaremedia.antx.FixtureExaminer;
43 import com.idaremedia.antx.helpers.Tk;
44 import com.idaremedia.antx.parameters.FeedbackLevel;
45
46
47 /**
48  * Fixture configuration instruction that creates a new data item and installs it as
49  * a top-level project reference. This task ensures that a data item is not installed
50  * into the project's reference table before it should be. Unless told otherwise, a
51  * &lt;datadef&gt; will overwrite and existing reference with its nested data object's
52  * definition.
53  * <p/>
54  * <b>Example Usage:</b><pre>
55  * &lt;target name="Might-Never-Be-Called"&gt;
56  * &lt;datadef name="antlibs.in" tellproperty="antlibs.merged"&gt;
57  * &lt;strings suffix="/antlib.xml"&gt;
58  * &lt;string value="${antxtras.src}"/&gt;
59  * &lt;string value="${sams.src}"/&gt;
60  * &lt;string value="${antunit.src}"/&gt;
61  * &lt;/strings&gt;
62  * &lt;/datadef&gt;
63  * &#8230;
64  * &lt;/target&gt;
65  * </pre>
66  *
67  * @since JWare/AntX 0.5
68  * @author ssmc, &copy;2004-2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
69  * @version 0.5
70  * @.safety single
71  * @.group api,helper
72  **/

73
74 public final class DataDefTask extends AssertableTask implements DynamicElementNS
75 {
76     /**
77      * Initializes a new DataDefTask instance.
78      **/

79     public DataDefTask()
80     {
81         super(AntX.nopackage);
82     }
83
84
85 // ---------------------------------------------------------------------------------------
86
// Script-facing Parameters:
87
// ---------------------------------------------------------------------------------------
88

89     /**
90      * Set the target reference's identifier (non-null)
91      * @param dstRefId reference id to install data under (non-null)
92      * @see #setHaltIfExists setHaltIfExists(&#8230;)
93      **/

94     public void setName(String JavaDoc dstRefId)
95     {
96         require_(dstRefId!=null, "setName- nonzro refid");
97         m_dstRefId = dstRefId;
98     }
99
100
101
102     /**
103      * Synonym for {@linkplain #setName setName(&#8230;)}.
104      * @param dstRefId reference id to install data under (non-null)
105      **/

106     public final void setToId(String JavaDoc dstRefId)
107     {
108         setName(dstRefId);
109     }
110
111
112
113     /**
114      * Returns the (new) target reference's identifer.
115      * Returns <i>null</i> if never set and source never
116      * set.
117      **/

118     public String JavaDoc getToRefId()
119     {
120         return m_dstRefId;
121     }
122
123
124
125     /**
126      * Tells this datadef whether to try to set the new object's
127      * 'id' attribute explicitly.
128      * @param setIt <i>true</i> to try.
129      **/

130     public void setSetId(boolean setIt)
131     {
132         m_setImplId = setIt;
133     }
134
135
136
137     /**
138      * Sets this task's announcement property's name. This project
139      * property will be set to "<span class="src">true</span>" before
140      * this tasks' execute method returns.
141      * @param property name of property (non-null)
142      **/

143     public void setTellProperty(String JavaDoc property)
144     {
145         require_(property!=null,"setProperty- nonzro property");
146         m_announceProperty = property;
147     }
148
149
150
151     /**
152      * Returns this task's announcement property. Will return
153      * <i>null</i> if never set explicitly.
154      **/

155     public final String JavaDoc getTellProperty()
156     {
157         return m_announceProperty;
158     }
159
160
161
162     /**
163      * Sets if this task will generate a build error if the
164      * destination reference id already exists within its project.
165      * Trumps the 'overwrite' option.
166      * @param halt <i>true</i> to throw build exception
167      * @see #setToId setToId
168      * @see #setOverwrite setOverwrite
169      **/

170     public void setHaltIfExists(boolean halt)
171     {
172         m_haltIfExists = halt;
173     }
174
175
176
177     /**
178      * Returns <i>true</i> if this task will generate a build error
179      * if the destination reference id already exists within its
180      * project. Defaults <i>false</i> (supports overwrites).
181      **/

182     public boolean isHaltIfExists()
183     {
184         return m_haltIfExists;
185     }
186
187
188
189     /**
190      * Sets if this task will overwrite an existing reference.
191      * This option is ignored if the 'haltiferror' option
192      * is turned on.
193      * @see #setHaltIfExists
194      * @param allowOverwrite <i>true</i> if can overwrite old reference
195      **/

196     public void setOverwrite(boolean allowOverwrite)
197     {
198         m_allowOverwrite = allowOverwrite;
199     }
200
201
202
203     /**
204      * Returns <i>true</i> if this task will overwrite an existing
205      * reference. Defaults <i>true</i>. Ignored if 'haltiferror'
206      * option is turned on.
207      **/

208     public boolean willAllowOverwrite()
209     {
210         return m_allowOverwrite;
211     }
212
213
214
215     /**
216      * Tells this task how much non-diagnostic feedback to generate.
217      * Really only has "loud" vs. "quiet-ish" interpretation. If
218      * set quiet, this task will not issue a warning if it overwrites
219      * an existing reference.
220      * @param level feedback level (non-null)
221      **/

222     public void setFeedback(String JavaDoc level)
223     {
224         require_(level!=null,"setFeedback- nonzro level");
225         FeedbackLevel fbl = FeedbackLevel.from(level);
226         if (fbl==null) {
227             String JavaDoc e = getAntXMsg("task.illegal.param.value",
228                            getTaskName(), level, "feedback");
229             log(e, Project.MSG_ERR);
230             throw new BuildException(e, getLocation());
231         }
232         m_fbLevel = fbl;
233     }
234
235
236
237     /**
238      * Returns this task's assigned feedback level. Will return
239      * <i>null</i> by default.
240      **/

241     public final FeedbackLevel getFeedbackLevel()
242     {
243         return m_fbLevel;
244     }
245
246
247 // ---------------------------------------------------------------------------------------
248
// Script-facing Nested Elements:
249
// ---------------------------------------------------------------------------------------
250

251
252     /**
253      * Returns a new instance of the named object type.
254      * @throws BuildException if already have a data item defined.
255      **/

256     public final Object JavaDoc createDynamicElement(String JavaDoc uri, String JavaDoc localName,
257         String JavaDoc qName)
258     {
259         if (m_dataImpl!=null) {
260             String JavaDoc ermsg = uistrs().get("taskset.only.one.specialtask",
261                                 qName, m_dataQName);
262             log(ermsg,Project.MSG_ERR);
263             throw new BuildException(ermsg,getLocation());
264         }
265
266         Project P= getProject();
267         ComponentHelper ch = ComponentHelper.getComponentHelper(P);
268         String JavaDoc fullname = localName;
269         if (!localName.equals(qName)) {
270             fullname = ProjectHelper.genComponentName(uri,localName);
271         }
272
273         Object JavaDoc o= ch.createComponent(fullname);
274         if (o!=null) {
275             P.setProjectReference(o);
276             if (o instanceof Task) {
277                 Task t = (Task)o;
278                 t.setOwningTarget(getOwningTarget());
279                 t.setTaskName(qName);
280                 t.setTaskType(fullname);
281                 t.init();
282             }
283             m_dataQName = qName;
284             m_dataImpl = o;
285         }
286
287         return m_dataImpl;
288     }
289
290
291 // ---------------------------------------------------------------------------------------
292
// Execution:
293
// ---------------------------------------------------------------------------------------
294

295
296     /**
297      * Installs the nested data item declaration as a top-level reference
298      * in this task's parent project.
299      */

300     public void execute()
301     {
302         verifyCanExecute_("exec");
303         
304         final Project P = getProject();
305         boolean quiet = FeedbackLevel.isQuietish(m_fbLevel,true,true);
306
307         Object JavaDoc there = FixtureExaminer.trueReference(P,m_dstRefId);
308         if (there==FixtureExaminer.PENDING_REFERENCE) {
309             quiet = true;
310             there = null;
311         }
312         if (there!=null) {
313             String JavaDoc msg = uistrs().get("task.warn.refid.exists",m_dstRefId);
314             if (isHaltIfExists()) {
315                 log(msg,Project.MSG_ERR);
316                 throw new BuildException(msg,getLocation());
317             }
318             if (!willAllowOverwrite()) {
319                 if (!quiet) {
320                     log(msg,Project.MSG_VERBOSE);
321                 }
322                 return;
323             }
324             if (!quiet) {
325                 log(msg,Project.MSG_WARN);
326             }
327         }
328
329         if (m_setImplId) {
330             tryToSetId(m_dataImpl);
331         }
332
333         if (m_dataImpl instanceof Task) {
334             ((Task)m_dataImpl).perform();
335         }
336
337         Map JavaDoc refs = P.getReferences();
338         if (quiet) {
339             refs.put(m_dstRefId,m_dataImpl);
340         } else {
341             P.addReference(m_dstRefId,m_dataImpl);
342         }
343
344         if (m_announceProperty!=null) {
345             checkIfProperty_(m_announceProperty,true);
346             P.setNewProperty(m_announceProperty,"true");
347         }
348     }
349
350
351
352     /**
353      * Verifies that this task has at least its source reference
354      * identifier set.
355      * @throws BuildException if missing any required bits.
356      **/

357     public void verifyIsDefined()
358     {
359         if (getToRefId()==null) {
360             String JavaDoc ermsg = uistrs().get("task.needs.this.attr",
361                 getTaskName(),"name");
362             log(ermsg,Project.MSG_ERR);
363             throw new BuildException(ermsg,getLocation());
364         }
365         if (m_dataImpl==null) {
366             String JavaDoc ermsg = uistrs().get("task.needs.oneof.these.nested",
367                 getTaskName(),"<any datatype>");
368             log(ermsg,Project.MSG_ERR);
369             throw new BuildException(ermsg,getLocation());
370         }
371     }
372
373
374     /**
375      * Verifies this task is completely defined with a target refid
376      * and data definition sub-element.
377      * @throws BuildException if not in project or not defined properly.
378      **/

379     protected void verifyCanExecute_(String JavaDoc calr)
380     {
381         verifyInProject_(calr);
382         verifyIsDefined();
383     }
384
385
386
387     /**
388      * Tries to call a 'setId' method on the new instance. If no
389      * such method exists or is inaccessible we silently ignore request.
390      * @param instance object being constructed.
391      **/

392     private void tryToSetId(Object JavaDoc instance)
393     {
394         try {
395             Tk.invokeit(instance, "setId", new Class JavaDoc[]{String JavaDoc.class},
396                 new String JavaDoc[]{getToRefId()});
397         } catch(Exception JavaDoc reflectX) {
398             /*burp*/
399         }
400     }
401
402
403     private String JavaDoc m_dstRefId;
404     private String JavaDoc m_announceProperty;
405     private boolean m_haltIfExists;//NB:false allow overwrites!
406
private boolean m_allowOverwrite=true;//NB:true allow overwrites!
407
private boolean m_setImplId;//NB:no unless asked-to explicitly
408
private FeedbackLevel m_fbLevel=FeedbackLevel.NORMAL;//NB:=>overwrite noise
409
private Object JavaDoc m_dataImpl;
410     private String JavaDoc m_dataQName;
411
412 }
413
414 /* end-of-InstallRefTask.java */
Popular Tags