KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > CallTarget


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs;
20
21 import java.io.IOException JavaDoc;
22
23 import org.apache.tools.ant.Task;
24 import org.apache.tools.ant.BuildException;
25 import org.apache.tools.ant.types.PropertySet;
26
27 /**
28  * Call another target in the same project.
29  *
30  * <pre>
31  * &lt;target name="foo"&gt;
32  * &lt;antcall target="bar"&gt;
33  * &lt;param name="property1" value="aaaaa" /&gt;
34  * &lt;param name="foo" value="baz" /&gt;
35  * &lt;/antcall&gt;
36  * &lt;/target&gt;
37  *
38  * &lt;target name="bar" depends="init"&gt;
39  * &lt;echo message="prop is ${property1} ${foo}" /&gt;
40  * &lt;/target&gt;
41  * </pre>
42  *
43  * <p>This only works as expected if neither property1 nor foo are
44  * defined in the project itself.
45  *
46  *
47  * @since Ant 1.2
48  *
49  * @ant.task name="antcall" category="control"
50  */

51 public class CallTarget extends Task {
52
53     private Ant callee;
54     // must match the default value of Ant#inheritAll
55
private boolean inheritAll = true;
56     // must match the default value of Ant#inheritRefs
57
private boolean inheritRefs = false;
58
59     private boolean targetSet = false;
60
61     /**
62      * If true, pass all properties to the new Ant project.
63      * Defaults to true.
64      * @param inherit <code>boolean</code> flag.
65      */

66     public void setInheritAll(boolean inherit) {
67        inheritAll = inherit;
68     }
69
70     /**
71      * If true, pass all references to the new Ant project.
72      * Defaults to false.
73      * @param inheritRefs <code>boolean</code> flag.
74      */

75     public void setInheritRefs(boolean inheritRefs) {
76         this.inheritRefs = inheritRefs;
77     }
78
79     /**
80      * Initialize this task by creating new instance of the ant task and
81      * configuring it by calling its own init method.
82      */

83     public void init() {
84         callee = new Ant(this);
85         callee.init();
86     }
87
88     /**
89      * Delegate the work to the ant task instance, after setting it up.
90      * @throws BuildException on validation failure or if the target didn't
91      * execute.
92      */

93     public void execute() throws BuildException {
94         if (callee == null) {
95             init();
96         }
97         if (!targetSet) {
98             throw new BuildException(
99                 "Attribute target or at least one nested target is required.",
100                  getLocation());
101         }
102         callee.setAntfile(getProject().getProperty("ant.file"));
103         callee.setInheritAll(inheritAll);
104         callee.setInheritRefs(inheritRefs);
105         callee.execute();
106     }
107
108     /**
109      * Create a new Property to pass to the invoked target(s).
110      * @return a <code>Property</code> object.
111      */

112     public Property createParam() {
113         if (callee == null) {
114             init();
115         }
116         return callee.createProperty();
117     }
118
119     /**
120      * Reference element identifying a data type to carry
121      * over to the invoked target.
122      * @param r the specified <code>Ant.Reference</code>.
123      * @since Ant 1.5
124      */

125     public void addReference(Ant.Reference r) {
126         if (callee == null) {
127             init();
128         }
129         callee.addReference(r);
130     }
131
132     /**
133      * Set of properties to pass to the new project.
134      * @param ps the <code>PropertySet</code> to pass.
135      * @since Ant 1.6
136      */

137     public void addPropertyset(PropertySet ps) {
138         if (callee == null) {
139             init();
140         }
141         callee.addPropertyset(ps);
142     }
143
144     /**
145      * Set target to execute.
146      * @param target the name of the target to execute.
147      */

148     public void setTarget(String JavaDoc target) {
149         if (callee == null) {
150             init();
151         }
152         callee.setTarget(target);
153         targetSet = true;
154     }
155
156     /**
157      * Add a target to the list of targets to invoke.
158      * @param t <code>Ant.TargetElement</code> representing the target.
159      * @since Ant 1.6.3
160      */

161     public void addConfiguredTarget(Ant.TargetElement t) {
162         if (callee == null) {
163             init();
164         }
165         callee.addConfiguredTarget(t);
166         targetSet = true;
167     }
168
169     /**
170      * Handles output.
171      * Send it the the new project if is present, otherwise
172      * call the super class.
173      * @param output The string output to output.
174      * @see Task#handleOutput(String)
175      * @since Ant 1.5
176      */

177     public void handleOutput(String JavaDoc output) {
178         if (callee != null) {
179             callee.handleOutput(output);
180         } else {
181             super.handleOutput(output);
182         }
183     }
184
185     /**
186      * Handles input.
187      * Deleate to the created project, if present, otherwise
188      * call the super class.
189      * @param buffer the buffer into which data is to be read.
190      * @param offset the offset into the buffer at which data is stored.
191      * @param length the amount of data to read.
192      *
193      * @return the number of bytes read.
194      *
195      * @exception IOException if the data cannot be read.
196      * @see Task#handleInput(byte[], int, int)
197      * @since Ant 1.6
198      */

199     public int handleInput(byte[] buffer, int offset, int length)
200         throws IOException JavaDoc {
201         if (callee != null) {
202             return callee.handleInput(buffer, offset, length);
203         }
204         return super.handleInput(buffer, offset, length);
205     }
206
207     /**
208      * Handles output.
209      * Send it the the new project if is present, otherwise
210      * call the super class.
211      * @param output The string to output.
212      * @see Task#handleFlush(String)
213      * @since Ant 1.5.2
214      */

215     public void handleFlush(String JavaDoc output) {
216         if (callee != null) {
217             callee.handleFlush(output);
218         } else {
219             super.handleFlush(output);
220         }
221     }
222
223     /**
224      * Handle error output.
225      * Send it the the new project if is present, otherwise
226      * call the super class.
227      * @param output The string to output.
228      *
229      * @see Task#handleErrorOutput(String)
230      * @since Ant 1.5
231      */

232     public void handleErrorOutput(String JavaDoc output) {
233         if (callee != null) {
234             callee.handleErrorOutput(output);
235         } else {
236             super.handleErrorOutput(output);
237         }
238     }
239
240     /**
241      * Handle error output.
242      * Send it the the new project if is present, otherwise
243      * call the super class.
244      * @param output The string to output.
245      * @see Task#handleErrorFlush(String)
246      * @since Ant 1.5.2
247      */

248     public void handleErrorFlush(String JavaDoc output) {
249         if (callee != null) {
250             callee.handleErrorFlush(output);
251         } else {
252             super.handleErrorFlush(output);
253         }
254     }
255 }
256
Popular Tags