KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antmod > tasks > ReplacePropertyTask


1 package org.antmod.tasks;
2
3 import org.apache.tools.ant.BuildException;
4 import org.apache.tools.ant.Task;
5
6 /**
7  * <p>
8  * Task to replace "${}" constructs more than one level deep.
9  * </p>
10  * <p>
11  * This task replaces the "${}" constructs in a "value" (see {@link #setValue}) and stores
12  * the new value with replacements in a property with the desired "name" (see {@link #setName}).
13  * </p>
14  * <p>
15  * This task can apply the replacement "depth" times (see {@link #setDepth}), which means
16  * that if after the first replacement new "${}" constructs pop-up,
17  * these can get replaced as well.
18  * </p>
19  *
20  * @author Klaas Waslander
21  */

22 public class ReplacePropertyTask extends Task {
23     private String JavaDoc name;
24     private String JavaDoc value;
25     private int depth = 0;
26
27     /**
28      * The depth represents the number of times "${}"
29      * constructs are replaced in the given {@link #setValue}.
30      * @param depth The desired depth, defaults to zero in which case Ant replaces the top-level "${}" constructs and this task does nothing
31      */

32     public void setDepth(int depth) {
33         this.depth = depth;
34     }
35
36     /**
37      * The name for the property that should hold the new
38      * value with replaced "${}" constructs.
39      * @param name Name of property in the project with new value
40      */

41     public void setName(String JavaDoc name) {
42         this.name = name;
43     }
44     
45     /**
46      * The value in which "${}" constructs are to be replaced
47      * and stored in the desired property name, see {@see #setName}.
48      * @param value The value with "${}" constructs that are to be replaced
49      */

50     public void setValue(String JavaDoc value) {
51         this.value = value;
52     }
53
54     /**
55      * Start replacing "${}" constructs in the given value
56      * "depth" times.
57      * @see #setValue(String)
58      * @see #setName(String)
59      * @see #setDepth(int)
60      */

61     public void execute() throws BuildException {
62         String JavaDoc newValue = this.value;
63         for (int i = depth; i-- > 0;) {
64             newValue = getProject().replaceProperties(newValue);
65         }
66         getProject().setProperty(this.name, newValue);
67     }
68 }
69
Popular Tags