source: release-kits/shared/ant/src/main/org/apache/tools/ant/taskdefs/CallTarget.java@ 16691

Last change on this file since 16691 was 16691, checked in by oranfry, 16 years ago

some changes in the execute logic

File size: 11.0 KB
Line 
1/*
2 * Copyright 2000-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.apache.tools.ant.taskdefs;
19import java.io.IOException;
20
21import org.apache.tools.ant.Task;
22import org.apache.tools.ant.Target;
23import org.apache.tools.ant.BuildException;
24import org.apache.tools.ant.types.PropertySet;
25
26/**
27 * Call another target in the same project.
28 *
29 * <pre>
30 * &lt;target name="foo"&gt;
31 * &lt;antcall target="bar"&gt;
32 * &lt;param name="property1" value="aaaaa" /&gt;
33 * &lt;param name="foo" value="baz" /&gt;
34 * &lt;/antcall&gt;
35 * &lt;/target&gt;
36 *
37 * &lt;target name="bar" depends="init"&gt;
38 * &lt;echo message="prop is ${property1} ${foo}" /&gt;
39 * &lt;/target&gt;
40 * </pre>
41 *
42 * <p>This only works as expected if neither property1 nor foo are
43 * defined in the project itself.
44 *
45 *
46 * @since Ant 1.2
47 *
48 * @ant.task name="antcall" category="control"
49 */
50public class CallTarget extends Task {
51
52 private Ant callee;
53 // must match the default value of Ant#inheritAll
54 private boolean inheritAll = true;
55 // must match the default value of Ant#inheritRefs
56 private boolean inheritRefs = false;
57
58 private boolean targetSet = false;
59 private String theTarget = null;
60 private String address = null;
61
62 /**
63 * If true, pass all properties to the new Ant project.
64 * Defaults to true.
65 * @param inherit <code>boolean</code> flag.
66 */
67 public void setInheritAll(boolean inherit) {
68 inheritAll = inherit;
69 }
70
71 /**
72 * If true, pass all references to the new Ant project.
73 * Defaults to false.
74 * @param inheritRefs <code>boolean</code> flag.
75 */
76 public void setInheritRefs(boolean inheritRefs) {
77 this.inheritRefs = inheritRefs;
78 }
79
80 /**
81 * Initialize this task by creating new instance of the ant task and
82 * configuring it by calling its own init method.
83 */
84 public void init() {
85 callee = (Ant) getProject().createTask("ant");
86 callee.setOwningTarget(getOwningTarget());
87 callee.setTaskName(getTaskName());
88 callee.setLocation(getLocation());
89 callee.init();
90 }
91
92 /**
93 * Delegate the work to the ant task instance, after setting it up.
94 * @throws BuildException on validation failure or if the target didn't
95 * execute.
96 */
97 public void execute() throws BuildException {
98 if (callee == null) {
99 init();
100 }
101 if (!targetSet) {
102 throw new BuildException(
103 "Attribute target or at least one nested target is required.",
104 getLocation());
105 }
106
107 /* -- start target addressing code -- */
108
109 //figure out what the address of this instance should be
110 String callingAddress = this.getOwningTarget().getAddress();
111 int subAddress = this.getOwningTarget().getNextSubAddress();
112 this.getOwningTarget().incrementNextSubAddress();
113
114 if ( callingAddress == null ) {
115 address = "" + subAddress;
116 } else {
117 address = callingAddress + "." + subAddress;
118 }
119
120
121 //check that the target should be run given address, resume.descend, resume.from and resume.to
122 String resumeFrom = this.getProject().getProperty("resume.from");
123 String resumeDescend = this.getProject().getProperty("resume.descend");
124 String resumeTo = this.getProject().getProperty("resume.to");
125
126 //given resumeFrom, resumeDescend and ResumeTo, might we execute?
127 boolean shouldExecOnFrom = resumeFrom == null || isDescendantOf(resumeFrom,address) || isAfter(address,resumeFrom);
128 boolean shouldExecuteOnDescend = resumeDescend == null || isDescendantOf(resumeDescend,address) || isDescendantOf(address,resumeDescend);
129 boolean shouldExecOnTo = resumeTo == null || !isAfter(address,resumeTo);
130
131 //check that all are true
132 boolean execute = shouldExecOnFrom && shouldExecuteOnDescend && shouldExecOnTo;
133 //log( "oran: address: " + address );
134 //log( "oran: execute: " + execute);
135
136 if ( !execute ) {
137 return;
138 }
139
140 /* -- end target addressing code -- */
141
142 callee.setAntfile(getProject().getProperty("ant.file"));
143 callee.setInheritAll(inheritAll);
144 callee.setInheritRefs(inheritRefs);
145 Target calleeTarget = (Target)callee.getProject().getTargets().get(theTarget);
146 calleeTarget.setAddress(address);
147 callee.execute(theTarget,address);
148
149 }
150
151 /**
152 * Create a new Property to pass to the invoked target(s).
153 * @return a <code>Property</code> object.
154 */
155 public Property createParam() {
156 if (callee == null) {
157 init();
158 }
159 return callee.createProperty();
160 }
161
162 /**
163 * Reference element identifying a data type to carry
164 * over to the invoked target.
165 * @param r the specified <code>Ant.Reference</code>.
166 * @since Ant 1.5
167 */
168 public void addReference(Ant.Reference r) {
169 if (callee == null) {
170 init();
171 }
172 callee.addReference(r);
173 }
174
175 /**
176 * Set of properties to pass to the new project.
177 * @param ps the <code>PropertySet</code> to pass.
178 * @since Ant 1.6
179 */
180 public void addPropertyset(PropertySet ps) {
181 if (callee == null) {
182 init();
183 }
184 callee.addPropertyset(ps);
185 }
186
187 /**
188 * Set target to execute.
189 * @param target the name of the target to execute.
190 */
191 public void setTarget(String target) {
192 if (callee == null) {
193 init();
194 }
195 callee.setTarget(target);
196 theTarget = target;
197 targetSet = true;
198 }
199
200 /**
201 * Add a target to the list of targets to invoke.
202 * @param t <code>Ant.TargetElement</code> representing the target.
203 * @since Ant 1.6.3
204 */
205 public void addConfiguredTarget(Ant.TargetElement t) {
206 if (callee == null) {
207 init();
208 }
209 callee.addConfiguredTarget(t);
210 targetSet = true;
211 }
212
213 /**
214 * @see Task#handleOutput(String)
215 *
216 * @since Ant 1.5
217 */
218 public void handleOutput(String output) {
219 if (callee != null) {
220 callee.handleOutput(output);
221 } else {
222 super.handleOutput(output);
223 }
224 }
225
226 /**
227 * @see Task#handleInput(byte[], int, int)
228 *
229 * @since Ant 1.6
230 */
231 public int handleInput(byte[] buffer, int offset, int length)
232 throws IOException {
233 if (callee != null) {
234 return callee.handleInput(buffer, offset, length);
235 } else {
236 return super.handleInput(buffer, offset, length);
237 }
238 }
239
240 /**
241 * @see Task#handleFlush(String)
242 *
243 * @since Ant 1.5.2
244 */
245 public void handleFlush(String output) {
246 if (callee != null) {
247 callee.handleFlush(output);
248 } else {
249 super.handleFlush(output);
250 }
251 }
252
253 /**
254 * @see Task#handleErrorOutput(String)
255 *
256 * @since Ant 1.5
257 */
258 public void handleErrorOutput(String output) {
259 if (callee != null) {
260 callee.handleErrorOutput(output);
261 } else {
262 super.handleErrorOutput(output);
263 }
264 }
265
266 /**
267 * @see Task#handleErrorFlush(String)
268 *
269 * @since Ant 1.5.2
270 */
271 public void handleErrorFlush(String output) {
272 if (callee != null) {
273 callee.handleErrorFlush(output);
274 } else {
275 super.handleErrorFlush(output);
276 }
277 }
278
279
280 /* --- node address relations for target addressing --- */
281 public static boolean isAfter(String x, String y) {
282
283 System.out.println( "x: " + x );
284 System.out.println( "y: " + y );
285
286 if ( x.equals("") || y.equals("") ) {
287 throw new BuildException("Error - x or y not specified !!");
288 }
289
290
291 //everything is after root
292 if ( y.equals("root") ) {
293 return true;
294 }
295
296 //root is after nothing
297 //(except itself, but that's already caught)
298 if ( x.equals("root") ) {
299 return false;
300 }
301
302 //infinity is after everything
303 if ( x.equals("infinity") )
304 return true;
305
306 //nothing is after infinity
307 // (except itself, but that's already caught)
308 if ( y.equals("infinity") )
309 return false;
310
311 //check that both are in the form int.int.int
312
313 //break into chambers
314 String[] xs = x.split("\\.");
315 String[] ys = y.split("\\.");
316
317 //figure out n
318 int n = ys.length;
319
320 //step through, making sure x values are higher than or equal to y values
321 for ( int i=0; i<n; i++ ) {
322 int xValue = i<xs.length ? Integer.parseInt( xs[i] ) : 0;
323 int yValue = Integer.parseInt( ys[i] );
324
325 System.out.println( xValue + " " + yValue );
326 if( xValue < yValue ) {
327 System.out.println("x not after y");
328 return false;
329 }
330
331 if( xValue > yValue ) {
332 System.out.println("x is after y");
333 return true;
334 }
335 }
336
337 // if execution reaches here, first n places are equal
338 System.out.println("x is after y");
339 return true;
340 }
341
342
343 public static boolean isDescendantOf(String x, String y) {
344
345 System.out.println( "x: " + x );
346 System.out.println( "y: " + y );
347
348 if ( x.equals("") || y.equals("") ) {
349 throw new BuildException("Error - x or y not specified !!");
350 }
351
352
353 //everything decends from root
354 if ( y.equals("root") )
355 return true;
356
357 //root descends from nothing
358 //(except itself but that's already caught)
359 if ( x.equals("root") )
360 return false;
361
362 //infinity descends from itself
363 if ( x.equals("infinity") && y.equals("infinity") ) {
364 return true;
365 }
366
367 //nothing descends from infinity
368 //(except itself but that's already caught)
369 if ( y.equals("infinity") ) {
370 return false;
371 }
372
373 //infinity descends only from nothing
374 //(except root and itself, but that's already caught)
375 if ( x.equals("infinity") ) {
376 return false;
377 }
378
379
380
381
382 //check that both are in the form int.int.int
383
384 //break into chambers
385 String[] xs = x.split("\\.");
386 String[] ys = y.split("\\.");
387
388 //make sure x is at least as deep as y
389 if ( xs.length < ys.length ) {
390 System.out.println("x not descendant of y");
391 return false;
392 }
393
394 //figure out n
395 int n = ys.length;
396
397 //step through, making sure x values are equal to y values
398 for ( int i=0; i<n; i++ ) {
399 int xValue = Integer.parseInt( xs[i] );
400 int yValue = Integer.parseInt( ys[i] );
401
402 System.out.println( xValue + " " + yValue );
403 if( xValue != yValue ) {
404 System.out.println("x not descendant of y");
405 return false;
406 }
407 }
408
409 // if execution reaches here, first n places are equal
410 System.out.println("x is descendant of y");
411 return true;
412 }
413
414}
Note: See TracBrowser for help on using the repository browser.