source: other-projects/trunk/gs3-release-maker/tasks/orans/TreeCallTarget.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

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