source: release-kits/shared/ant-tasks/orans/TreeCallTarget.java@ 16537

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

fixed a little bug in tree target caller

File size: 11.1 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
18import 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 TreeCallTarget 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 BuildEximport org.apache.tools.ant.Task;ception 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 //figure out what the address of this instance should be
108 String callingAddress = this.getOwningTarget().getAddress();
109 int subAddress = this.getOwningTarget().getNextSubAddress();
110 this.getOwningTarget().incrementNextSubAddress();
111
112 if ( callingAddress == null ) {
113 address = "" + subAddress;
114 } else {
115 address = callingAddress + "." + subAddress;
116 }
117
118
119 //check that the target should be run given address,resume.mode, rusume.from and resume.to
120 String resumeFrom = this.getProject().getProperty("resume.from");
121 String resumeDescend = this.getProject().getProperty("resume.descend");
122 String resumeTo = this.getProject().getProperty("resume.to");
123
124 //given resumeFrom, might we execute?
125 boolean shouldExecOnFrom = resumeFrom == null || isDescendantOf(resumeFrom,address) || isAfter(address,resumeFrom);
126
127 //given resumeDescend, might we execute?
128 boolean shouldExecuteOnDescend = resumeDescend == null || isDescendantOf(address,resumeDescend);
129
130 //given resumeTo, might we execute?
131 boolean shouldExecOnTo = resumeTo == null || !isAfter(address,resumeTo);
132
133 //check that all are true
134 boolean execute = shouldExecOnFrom && shouldExecuteOnDescend && shouldExecOnTo;
135
136 if ( execute ) {
137 callee.setAntfile(getProject().getProperty("ant.file"));
138 callee.setInheritAll(inheritAll);
139 callee.setInheritRefs(inheritRefs);
140 Target calleeTarget = (Target)callee.getProject().getTargets().get(theTarget);
141 calleeTarget.setAddress(address);
142
143 if( false ) {
144 throw new BuildException(
145 "\n theTarget " + theTarget + "\n calleeTarget: " + calleeTarget + "\n address: " + calleeTarget.getAddress(),
146 getLocation()
147 );
148 }
149 callee.execute(theTarget,address);
150 }
151 }
152
153 /**
154 * Create a new Property to pass to the invoked target(s).
155 * @return a <code>Property</code> object.
156 */
157 public Property createParam() {
158 if (callee == null) {
159 init();
160 }
161 return callee.createProperty();
162 }
163
164 /**
165 * Reference element identifying a data type to carry
166 * over to the invoked target.
167 * @param r the specified <code>Ant.Reference</code>.
168 * @since Ant 1.5
169 */
170 public void addReference(Ant.Reference r) {
171 if (callee == null) {
172 init();
173 }
174 callee.addReference(r);
175 }
176
177 /**
178 * Set of properties to pass to the new project.
179 * @param ps the <code>PropertySet</code> to pass.
180 * @since Ant 1.6
181 */
182 public void addPropertyset(PropertySet ps) {
183 if (callee == null) {
184 init();
185 }
186 callee.addPropertyset(ps);
187 }
188
189
190 /**
191 * Set target to execute.
192 * @param target the name of the target to execute.
193 */
194 public void setTarget(String target) {
195 if (callee == null) {
196 init();
197 }
198 callee.setTarget(target);
199 theTarget = target;
200 targetSet = true;
201 }
202
203 /**
204 * Add a target to the list of targets to invoke.
205 * @param t <code>Ant.TargetElement</code> representing the target.
206 * @since Ant 1.6.3
207 */
208 public void addConfiguredTarget(Ant.TargetElement t) {
209 if (callee == null) {
210 init();
211 }
212 callee.addConfiguredTarget(t);
213 targetSet = true;
214 }
215
216 /**
217 * @see Task#handleOutput(String)
218 *
219 * @since Ant 1.5
220 */
221 public void handleOutput(String output) {
222 if (callee != null) {
223 callee.handleOutput(output);
224 } else {
225 super.handleOutput(output);
226 }
227 }
228
229 /**
230 * @see Task#handleInput(byte[], int, int)
231 *
232 * @since Ant 1.6
233 */
234 public int handleInput(byte[] buffer, int offset, int length)
235 throws IOException {
236 if (callee != null) {
237 return callee.handleInput(buffer, offset, length);
238 } else {
239 return super.handleInput(buffer, offset, length);
240 }
241 }
242
243 /**
244 * @see Task#handleFlush(String)
245 *
246 * @since Ant 1.5.2
247 */
248 public void handleFlush(String output) {
249 if (callee != null) {
250 callee.handleFlush(output);
251 } else {
252 super.handleFlush(output);
253 }
254 }
255
256 /**
257 * @see Task#handleErrorOutput(String)
258 *
259 * @since Ant 1.5
260 */
261 public void handleErrorOutput(String output) {
262 if (callee != null) {
263 callee.handleErrorOutput(output);
264 } else {
265 super.handleErrorOutput(output);
266 }
267 }
268
269 /**
270 * @see Task#handleErrorFlush(String)
271 *
272 * @since Ant 1.5.2
273 */
274 public void handleErrorFlush(String output) {
275 if (callee != null) {
276 callee.handleErrorFlush(output);
277 } else {
278 super.handleErrorFlush(output);
279 }
280 }
281
282
283
284
285
286
287 /* --- node address relations --- */
288 public static boolean isAfter(String x, String y) {
289
290 System.out.println( "x: " + x );
291 System.out.println( "y: " + y );
292
293 if ( x.equals("") || y.equals("") ) {
294 throw new BuildException("Error - x or y not specified !!");
295 }
296
297 //check that both are in the form int.int.int
298
299 //break into chambers
300 String[] xs = x.split("\\.");
301 String[] ys = y.split("\\.");
302
303 //figure out n
304 int n = ys.length;
305
306 //step through, making sure x values are higher than or equal to y values
307 for ( int i=0; i<n; i++ ) {
308 int xValue = i<xs.length ? Integer.parseInt( xs[i] ) : 0;
309 int yValue = Integer.parseInt( ys[i] );
310
311 System.out.println( xValue + " " + yValue );
312 if( xValue < yValue ) {
313 System.out.println("x not after y");
314 return false;
315 }
316
317 if( xValue > yValue ) {
318 System.out.println("x is after y");
319 return true;
320 }
321 }
322
323 // if execution reaches here, first n places are equal
324 System.out.println("x is after y");
325 return true;
326 }
327
328
329 public static boolean isDescendantOf(String x, String y) {
330
331 System.out.println( "x: " + x );
332 System.out.println( "y: " + y );
333
334 if ( x.equals("") || y.equals("") ) {
335 throw new BuildException("Error - x or y not specified !!");
336 }
337
338 //check that both are in the form int.int.int
339
340 //break into chambers
341 String[] xs = x.split("\\.");
342 String[] ys = y.split("\\.");
343
344 //make sure x is at least as deep as y
345 if ( xs.length < ys.length ) {
346 System.out.println("x not descendant of y");
347 return false;
348 }
349
350 //figure out n
351 int n = ys.length;
352
353 //step through, making sure x values are equal to y values
354 for ( int i=0; i<n; i++ ) {
355 int xValue = Integer.parseInt( xs[i] );
356 int yValue = Integer.parseInt( ys[i] );
357
358 System.out.println( xValue + " " + yValue );
359 if( xValue != yValue ) {
360 System.out.println("x not descendant of y");
361 return false;
362 }
363 }
364
365 // if execution reaches here, first n places are equal
366 System.out.println("x is descendant of y");
367 return true;
368 }
369
370 public static boolean isEqualTo(String x, String y) {
371
372 System.out.println( "x: " + x );
373 System.out.println( "y: " + y );
374
375 if ( x.equals("") || y.equals("") ) {
376 throw new BuildException("Error - x or y not specified !!");
377 }
378
379 //check that both are in the form int.int.int
380
381 //break into chambers
382 String[] xs = x.split("\\.");
383 String[] ys = y.split("\\.");
384
385 //make sure x is exactly as deep as y
386 if ( xs.length != ys.length ) {
387 System.out.println("x not equal to y");
388 return false;
389 }
390
391 //figure out n
392 int n = xs.length;
393
394 //step through, making sure x values are equal to y values
395 for ( int i=0; i<n; i++ ) {
396 int xValue = Integer.parseInt( xs[i] );
397 int yValue = Integer.parseInt( ys[i] );
398
399 System.out.println( xValue + " " + yValue );
400 if( xValue != yValue ) {
401 System.out.println("x not equal to y");
402 return false;
403 }
404 }
405
406 // if execution reaches here, first all places
407 System.out.println("x is equal to y");
408 return true;
409 }
410
411}
Note: See TracBrowser for help on using the repository browser.