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