source: release-kits/lirk3/ant-scripts/tasks/orans/old/AddressedCallTarget.java@ 14982

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

initial import of LiRK3

File size: 11.3 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.BuildException;
23import org.apache.tools.ant.types.PropertySet;
24
25/**
26 * Call another target in the same project.
27 *
28 * <pre>
29 * &lt;target name="foo"&gt;
30 * &lt;antcall target="bar"&gt;
31 * &lt;param name="property1" value="aaaaa" /&gt;
32 * &lt;param name="foo" value="baz" /&gt;
33 * &lt;/antcall&gt;
34 * &lt;/target&gt;
35 *
36 * &lt;target name="bar" depends="init"&gt;
37 * &lt;echo message="prop is ${property1} ${foo}" /&gt;
38 * &lt;/target&gt;
39 * </pre>
40 *
41 * <p>This only works as expected if neither property1 nor foo are
42 * defined in the project itself.
43 *
44 *
45 * @since Ant 1.2
46 *
47 * @ant.task name="antcall" category="control"
48 */
49public class AddressedCallTarget extends Task {
50
51 private Ant callee;
52 // must match the default value of Ant#inheritAll
53 private boolean inheritAll = true;
54 // must match the default value of Ant#inheritRefs
55 private boolean inheritRefs = false;
56
57 //the address of the target
58 private String address = "";
59
60 private boolean targetSet = false;
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
102 if (!targetSet) {
103 throw new BuildException(
104 "Attribute target or at least one nested target is required.",
105 getLocation());
106 }
107
108 //check that the target should be run given its address, the resume mode and the rusumefrom property
109 String resumeFrom = this.getProject().getProperty("resume.from");
110 String resumeTo = this.getProject().getProperty("resume.to");
111 String resumeMode = this.getProject().getProperty("resume.mode");
112
113 boolean shouldExecOnFrom = false, shouldExecOnTo = false, shouldExecOnMode = false;
114
115 //given resumeFrom, might we execute?
116 System.out.println("checking if we should execute given that resumeFrom=" + resumeFrom);
117 if ( resumeFrom == null ) {
118 shouldExecOnFrom = true;
119 } else {
120 if ( isAfter(address,resumeFrom) || isDescendantOf(resumeFrom,address) ) {
121 shouldExecOnFrom = true;
122 } else {
123 shouldExecOnFrom = false;
124 }
125 }
126
127 System.out.println("checking if we should execute given that resumeTo=" + resumeTo);
128 //given resumeTo, might we execute?
129 if ( resumeTo == null ) {
130 shouldExecOnTo = true;
131 } else {
132 if ( !isAfter(address,resumeTo ) ) {
133 shouldExecOnTo = true;
134 } else {
135 shouldExecOnTo = false;
136 }
137 }
138
139 System.out.println("checking if we should execute given that resumeMode=" + resumeMode);
140 //given resumeMode, might we execute?
141 if ( resumeMode == null ) {
142 shouldExecOnMode = true;
143 } else if ( resumeMode.equals("descend") ) {
144 if ( resumeFrom == null ) {
145 shouldExecOnMode = true;
146 } else if ( isDescendantOf(address,resumeFrom ) ) {
147 shouldExecOnMode = true;
148 } else {
149 shouldExecOnMode = false;
150 }
151 }
152
153 System.out.println("check that all three are true");
154 boolean execute = ( shouldExecOnFrom && shouldExecOnTo && shouldExecOnMode );
155
156
157 if ( execute ) {
158*/
159 callee.setAntfile(getProject().getProperty("ant.file"));
160 callee.setInheritAll(inheritAll);
161 callee.setInheritRefs(inheritRefs);
162 callee.execute();
163// }
164 }
165
166 /**
167 * Create a new Property to pass to the invoked target(s).
168 * @return a <code>Property</code> object.
169 */
170 public Property createParam() {
171 if (callee == null) {
172 init();
173 }
174 return callee.createProperty();
175 }
176
177 /**
178 * Reference element identifying a data type to carry
179 * over to the invoked target.
180 * @param r the specified <code>Ant.Reference</code>.
181 * @since Ant 1.5
182 */
183 public void addReference(Ant.Reference r) {
184 if (callee == null) {
185 init();
186 }
187 callee.addReference(r);
188 }
189
190 /**
191 * Set of properties to pass to the new project.
192 * @param ps the <code>PropertySet</code> to pass.
193 * @since Ant 1.6
194 */
195 public void addPropertyset(PropertySet ps) {
196 if (callee == null) {
197 init();
198 }
199 callee.addPropertyset(ps);
200 }
201
202 /**
203 * setter for address
204 */
205 public void setAddress(String address) {
206 this.address=address;
207 }
208
209 /**
210 * getter for address
211 */
212 public String getAddress() {
213 return address;
214 }
215
216
217 /**
218 * Set target to execute.
219 * @param target the name of the target to execute.
220 */
221 public void setTarget(String target) {
222 if (callee == null) {
223 init();
224 }
225 callee.setTarget(target);
226 targetSet = true;
227 }
228
229 /**
230 * Add a target to the list of targets to invoke.
231 * @param t <code>Ant.TargetElement</code> representing the target.
232 * @since Ant 1.6.3
233 */
234 public void addConfiguredTarget(Ant.TargetElement t) {
235 if (callee == null) {
236 init();
237 }
238 callee.addConfiguredTarget(t);
239 targetSet = true;
240 }
241
242 /**
243 * @see Task#handleOutput(String)
244 *
245 * @since Ant 1.5
246 */
247 public void handleOutput(String output) {
248 if (callee != null) {
249 callee.handleOutput(output);
250 } else {
251 super.handleOutput(output);
252 }
253 }
254
255 /**
256 * @see Task#handleInput(byte[], int, int)
257 *
258 * @since Ant 1.6
259 */
260 public int handleInput(byte[] buffer, int offset, int length)
261 throws IOException {
262 if (callee != null) {
263 return callee.handleInput(buffer, offset, length);
264 } else {
265 return super.handleInput(buffer, offset, length);
266 }
267 }
268
269 /**
270 * @see Task#handleFlush(String)
271 *
272 * @since Ant 1.5.2
273 */
274 public void handleFlush(String output) {
275 if (callee != null) {
276 callee.handleFlush(output);
277 } else {
278 super.handleFlush(output);
279 }
280 }
281
282 /**
283 * @see Task#handleErrorOutput(String)
284 *
285 * @since Ant 1.5
286 */
287 public void handleErrorOutput(String output) {
288 if (callee != null) {
289 callee.handleErrorOutput(output);
290 } else {
291 super.handleErrorOutput(output);
292 }
293 }
294
295 /**
296 * @see Task#handleErrorFlush(String)
297 *
298 * @since Ant 1.5.2
299 */
300 public void handleErrorFlush(String output) {
301 if (callee != null) {
302 callee.handleErrorFlush(output);
303 } else {
304 super.handleErrorFlush(output);
305 }
306 }
307
308
309
310
311
312 /* --- node address relations --- */
313 public boolean isAfter(String x, String y) {
314
315 System.out.println( "x: " + x );
316 System.out.println( "y: " + y );
317
318 if ( x.equals("") || y.equals("") ) {
319 throw new BuildException("Error - x or y not specified !!");
320 }
321
322 //check that both are in the form int.int.int
323
324 //break into chambers
325 String[] xs = x.split("\\.");
326 String[] ys = y.split("\\.");
327
328 //figure out n
329 int n = ys.length;
330
331 //step through, making sure x values are higher than or equal to y values
332 for ( int i=0; i<n; i++ ) {
333 int xValue = i<xs.length ? Integer.parseInt( xs[i] ) : 0;
334 int yValue = Integer.parseInt( ys[i] );
335
336 System.out.println( xValue + " " + yValue );
337 if( xValue < yValue ) {
338 System.out.println("x not after y");
339 return false;
340 }
341
342 if( xValue > yValue ) {
343 System.out.println("x is after y");
344 return true;
345 }
346 }
347
348 // if execution reaches here, first n places are equal
349 System.out.println("x is after y");
350 return true;
351 }
352
353
354 public boolean isDescendantOf(String x, String y) {
355
356 System.out.println( "x: " + x );
357 System.out.println( "y: " + y );
358
359 if ( x.equals("") || y.equals("") ) {
360 throw new BuildException("Error - x or y not specified !!");
361 }
362
363 //check that both are in the form int.int.int
364
365 //break into chambers
366 String[] xs = x.split("\\.");
367 String[] ys = y.split("\\.");
368
369 //make sure x is at least as deep as y
370 if ( xs.length < ys.length ) {
371 System.out.println("x not descendant of y");
372 return false;
373 }
374
375 //figure out n
376 int n = ys.length;
377
378 //step through, making sure x values are equal to y values
379 for ( int i=0; i<n; i++ ) {
380 int xValue = Integer.parseInt( xs[i] );
381 int yValue = Integer.parseInt( ys[i] );
382
383 System.out.println( xValue + " " + yValue );
384 if( xValue != yValue ) {
385 System.out.println("x not descendant of y");
386 return false;
387 }
388 }
389
390 // if execution reaches here, first n places are equal
391 System.out.println("x is descendant of y");
392 return true;
393 }
394
395
396 public boolean isBetweenYAndRoot(String x, String y) {
397
398 System.out.println( "x: " + x );
399 System.out.println( "y: " + y );
400
401 if ( x.equals("") || y.equals("") ) {
402 throw new BuildException("Error - x or y not specified !!");
403 }
404
405 //check that both are in the form int.int.int
406
407 //break into chambers
408 String[] xs = x.split("\\.");
409 String[] ys = y.split("\\.");
410
411 //make sure y is at least as deep as n
412 if ( ys.length < xs.length ) {
413 System.out.println("x not between y and root");
414 return false;
415 }
416
417 //figure out n
418 int n = xs.length;
419
420 //step through, making sure y values are equal to x values
421 for ( int i=0; i<n; i++ ) {
422 int xValue = Integer.parseInt( xs[i] );
423 int yValue = Integer.parseInt( ys[i] );
424
425 System.out.println( xValue + " " + yValue );
426 if( xValue != yValue ) {
427 System.out.println("x not between y and root");
428 return false;
429 }
430 }
431
432 // if execution reaches here, first n places are equal
433 System.out.println("x is between y and root");
434 return true;
435 }
436
437
438
439
440
441
442}
Note: See TracBrowser for help on using the repository browser.