KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: ImportTask.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.solo;
30
31 import java.util.Iterator JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Properties JavaDoc;
34
35 import org.apache.tools.ant.BuildException;
36 import org.apache.tools.ant.Project;
37 import org.apache.tools.ant.types.Mapper;
38 import org.apache.tools.ant.util.FileNameMapper;
39
40 import com.idaremedia.antx.AntX;
41 import com.idaremedia.antx.AssertableTask;
42 import com.idaremedia.antx.ExportedProperties;
43
44 /**
45  * Copies exported variables into a project's properties space. Useful after running
46  * sub-project, sub-targets or steps and want to bring generated variables back into the
47  * project where standard Ant tasks can get at them. The import task does not overwrite
48  * existing properties.
49  * <p>
50  * <b>Example Usage</b>:<pre>
51  * &lt;assignimport name="my.variable"/&gt;
52  * &lt;assignimport name="global.name" property="local.name"/&gt;
53  * &lt;assignimport name="my.variable" haltifmissing="yes"/&gt;
54  * &lt;assignimport prefix="defaults."/&gt;
55  * &lt;assignimport prefix="defaults." strip="yes"/&gt;
56  *
57  * &lt;assignimport prefix="outputs." ismove="yes"&gt;
58  * &lt;mapper type="glob" from="outputs.*" to="defaults.*"/&gt;
59  * &lt;/assignimport&gt;
60  * </pre>
61  *
62  * @since JWare/AntX 0.2
63  * @author ssmc, &copy;2002-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
64  * @version 0.5
65  * @.safety single
66  * @.group api,helper
67  * @see ExportTask
68  * @see ExportedProperties
69  **/

70
71 public final class ImportTask extends AssertableTask
72 {
73     /**
74      * Initializes a new ImportTask instance.
75      **/

76     public ImportTask()
77     {
78         super(AntX.nopackage);
79     }
80
81
82     /**
83      * Initializes a new CV-labeled ImportTask instance.
84      * @param iam CV-label (non-null)
85      **/

86     public ImportTask(String JavaDoc iam)
87     {
88         super(iam);
89     }
90
91
92     /**
93      * Sets the name of the single variable to import. Either this
94      * parameter or a prefix must be defined before this task is
95      * executed.
96      * @param name the variable's name (non-null)
97      * @throws BuildException if the 'prefix' parameter has already been defined
98      **/

99     public void setName(String JavaDoc name)
100     {
101         require_(name!=null,"setNam- nonzro nam");
102         if (getPrefix()!=null) {
103             String JavaDoc ermsg = getAntXMsg("import.one.from.attr");
104             log(ermsg,Project.MSG_ERR);
105             throw new BuildException(ermsg,getLocation());
106         }
107         m_name = name;
108     }
109
110
111     /**
112      * Returns the variable name to import. Returns <i>null</i>
113      * if never set.
114      **/

115     public final String JavaDoc getName()
116     {
117         return m_name;
118     }
119
120
121     /**
122      * Setup this task to import all thread-based variables that begin
123      * with the given prefix.
124      * @throws BuildException if the 'name' parameter has already been defined
125      **/

126     public void setPrefix(String JavaDoc prefix)
127     {
128         require_(prefix!=null && prefix.length()>0,"setPfx- nonzro pfx");
129         if (getName()!=null) {
130             String JavaDoc ermsg = getAntXMsg("import.one.from.attr");
131             log(ermsg,Project.MSG_ERR);
132             throw new BuildException(ermsg,getLocation());
133         }
134         m_prefix = prefix;
135     }
136
137
138     /**
139      * Returns the prefix of all variables to be imported by this task.
140      * Will return <i>null</i> if never set.
141      **/

142     public final String JavaDoc getPrefix()
143     {
144         return m_prefix;
145     }
146
147
148     /**
149      * Tells this task to remove the prefix from imported variables. Only
150      * used if a prefix is defined.
151      **/

152     public void setStrip(boolean remove)
153     {
154         m_stripPrefix = remove;
155     }
156
157
158     /**
159      * Returns <i>true</i> if prefix will be stripped from imported
160      * variables. Defaults <i>false</i>.
161      **/

162     public final boolean willStrip()//oooouuuee...
163
{
164         return m_stripPrefix;
165     }
166
167
168
169     /**
170      * Returns a new name mapper for this import task ensuring that
171      * only one is defined for task.
172      * @since JWare/AntX 0.4
173      **/

174     public Mapper createMapper()
175     {
176         if (m_nameMapper!=null) {
177             String JavaDoc error = uistrs().get("taskset.only.one.specialtask",
178                                         "mapper","mapper");
179             log(error,Project.MSG_ERR);
180             throw new BuildException(error,getLocation());
181         }
182         m_nameMapper = new Mapper(getProject());
183         return m_nameMapper;
184     }
185
186
187
188     /**
189      * Tells this tasks whether to throw a build exception if named
190      * variable doesn't exist within thread's scope. Defaults to
191      * <i>false</i>. If a prefix is specified, at least one matching
192      * variable must be found.
193      * @param stop <i>true</i> if variable must exist.
194      **/

195     public void setHaltIfMissing(boolean stop)
196     {
197         m_haltIfNone = stop;
198     }
199
200
201     /**
202      * Returns <i>true</i> if this task will stop if it cannot import
203      * at least one variable. Defaults <i>false</i>.
204      **/

205     public final boolean isHaltIfMissing()
206     {
207         return m_haltIfNone;
208     }
209
210
211     /**
212      * Sets the project-local name of the imported variable. If never
213      * defined the property is named same as the variable.
214      * @param property the local property's name (non-null)
215      **/

216     public void setProperty(String JavaDoc property)
217     {
218         require_(property!=null,"setProp- nonzro nam");
219         m_updateProperty = property;
220     }
221
222
223     /**
224      * Returns the project-local name set for this import task. Will
225      * return <i>null</i> if never set (the variable's name will be
226      * used).
227      **/

228     public final String JavaDoc getUpdateProperty()
229     {
230         return m_updateProperty;
231     }
232
233
234     /**
235      * Tells this task to unset source variables once they've been
236      * successfully copied to properties. This is a house-cleaning
237      * parameter used when steps/sub-targets create variables expressly
238      * to pass back to calling target.
239      * @param is <i>true</i> if import is really a move
240      **/

241     public void setIsMove(boolean is)
242     {
243         m_likeMove = is;
244     }
245
246
247     /**
248      * Returns <i>true</i> if this import should function like a
249      * move operation. Defaults to <i>false</i>; the originating
250      * variable is not removed after the copy operation.
251      **/

252     public final boolean isMove()
253     {
254         return m_likeMove;
255     }
256
257
258     /**
259      * Imports one or all specified variables into this task's project's
260      * properties. If the property already exists, it is not overwritten.
261      * @throws BuildException if incomplete definition or variable not
262      * defined and 'haltIfMissing' flag is set <i>true</i>
263      **/

264     public void execute() throws BuildException
265     {
266         verifyCanExecute_("execute");
267
268         Project P= getProject();
269         int N=0;
270         boolean zapVar= isMove();
271
272         if (getPrefix()!=null) {
273             final String JavaDoc pfx = getPrefix();
274             final boolean strip = willStrip();
275             final int PFXLEN= pfx.length();
276
277             FileNameMapper mapper= null;
278             if (m_nameMapper!=null) {
279                 mapper = m_nameMapper.getImplementation();
280             }
281
282             Properties JavaDoc allP = ExportedProperties.copy(null);
283             Iterator JavaDoc itr= allP.entrySet().iterator();
284
285             log("Import trying to convert properties starting with '"+pfx+
286                 "' to project; "+allP.size()+" candidates", Project.MSG_DEBUG);
287
288             while (itr.hasNext()) {
289                 Map.Entry JavaDoc mE = (Map.Entry JavaDoc)itr.next();
290                 String JavaDoc key = mE.getKey().toString();
291                 if (key.startsWith(pfx)) {
292                     if (strip) {
293                         key = key.substring(PFXLEN);
294                         verify_(key.length()>0,"import valid key aftr strip");
295                     }
296                     if (mapper!=null) {
297                         String JavaDoc[] rslt = mapper.mapFileName(key);
298                         if (rslt!=null) {
299                             key = rslt[0];
300                         }
301                     }
302                     P.setNewProperty(key, mE.getValue().toString());
303                     N++;
304                     if (zapVar) {
305                         ExportedProperties.unset(mE.getKey().toString());
306                     }
307                 }
308             }
309             log("Import tried to suck in ("+N+") variables into project",
310                 Project.MSG_DEBUG);
311             allP.clear();
312
313         } else {
314             String JavaDoc varkey = getName();
315             String JavaDoc value = ExportedProperties.readstring(varkey);
316             if (value!=null) {
317                 String JavaDoc property = getUpdateProperty();
318                 if (property==null) {
319                     property = varkey;
320                 }
321                 if (m_nameMapper!=null) {
322                     FileNameMapper mapper = m_nameMapper.getImplementation();
323                     String JavaDoc[] rslt = mapper.mapFileName(property);
324                     if (rslt!=null) {
325                         property = rslt[0];
326                     }
327                 }
328                 log("Import trying to convert variable '"+varkey+
329                     "' to property '"+property+"'",Project.MSG_DEBUG);
330
331                 P.setNewProperty(property, value);
332                 N++;
333                 if (zapVar) {
334                     ExportedProperties.unset(varkey);
335                 }
336             }
337         }
338
339         if (N==0 && isHaltIfMissing()) {
340             String JavaDoc ermsg;
341             if (getMsgId()!=null) {
342                 ermsg = getMsg();
343             } else {
344                 ermsg= getAntXMsg("import.none",getName());
345             }
346             log(ermsg,Project.MSG_ERR);
347             throw new BuildException(ermsg,getLocation());
348         }
349     }
350
351
352
353     /**
354      * Verifies that this task is in a valid project and has either the
355      * variable's (single) name or a prefix defined.
356      * @throws BuildException if not in project or neither the prefix nor
357      * the name has been defined
358      **/

359     protected void verifyCanExecute_(String JavaDoc calr)
360     {
361         super.verifyCanExecute_(calr);
362
363         if (getPrefix()==null && getName()==null) {
364             String JavaDoc ermsg = getAntXMsg("task.needs.this.attr",getTaskName(),"name|prefix");
365             log(ermsg,Project.MSG_ERR);
366             throw new BuildException(ermsg, getLocation());
367         }
368     }
369
370
371     private boolean m_haltIfNone;//NB:nope,keep goin'
372
private String JavaDoc m_prefix;
373     private String JavaDoc m_name;
374     private String JavaDoc m_updateProperty;
375     private boolean m_stripPrefix, m_likeMove;
376     private Mapper m_nameMapper;
377 }
378
379 /* end-of-ImportTask.java */
380
Popular Tags