1 | import java.util.*;
|
---|
2 | import org.apache.tools.ant.*;
|
---|
3 | import org.apache.tools.ant.taskdefs.*;
|
---|
4 | import org.apache.tools.ant.taskdefs.condition.*;
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * takes node addresses as arguments. E.g., 1.4.3.2
|
---|
8 | *
|
---|
9 | * node address x is a descendant of node address y iff, where y has n places,
|
---|
10 | * x has n or more places, and the values in the first n places of x are equal
|
---|
11 | * to the values in their corresponding places in y
|
---|
12 | */
|
---|
13 |
|
---|
14 | public class IsDescendantOf implements Condition {
|
---|
15 |
|
---|
16 | private String x = "";
|
---|
17 | private String y = "";
|
---|
18 |
|
---|
19 | public boolean eval() {
|
---|
20 |
|
---|
21 | System.out.println( "x: " + x );
|
---|
22 | System.out.println( "y: " + y );
|
---|
23 |
|
---|
24 | if ( x.equals("") || y.equals("") ) {
|
---|
25 | throw new BuildException("Error - x or y not specified !!");
|
---|
26 | }
|
---|
27 |
|
---|
28 | //check that both are in the form int.int.int
|
---|
29 |
|
---|
30 | //break into chambers
|
---|
31 | String[] xs = x.split("\\.");
|
---|
32 | String[] ys = y.split("\\.");
|
---|
33 |
|
---|
34 | //make sure x is at least as deep as y
|
---|
35 | if ( xs.length < ys.length ) {
|
---|
36 | System.out.println("x not descendant of y");
|
---|
37 | return false;
|
---|
38 | }
|
---|
39 |
|
---|
40 | //figure out n
|
---|
41 | int n = ys.length;
|
---|
42 |
|
---|
43 | //step through, making sure x values are equal to y values
|
---|
44 | for ( int i=0; i<n; i++ ) {
|
---|
45 | int xValue = Integer.parseInt( xs[i] );
|
---|
46 | int yValue = Integer.parseInt( ys[i] );
|
---|
47 |
|
---|
48 | System.out.println( xValue + " " + yValue );
|
---|
49 | if( xValue != yValue ) {
|
---|
50 | System.out.println("x not descendant of y");
|
---|
51 | return false;
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | // if execution reaches here, first n places are equal
|
---|
56 | System.out.println("x is descendant of y");
|
---|
57 | return true;
|
---|
58 | }
|
---|
59 |
|
---|
60 |
|
---|
61 | public void setX(String x) {
|
---|
62 | this.x=x;
|
---|
63 | }
|
---|
64 |
|
---|
65 | public void setY(String y) {
|
---|
66 | this.y=y;
|
---|
67 | }
|
---|
68 |
|
---|
69 | }
|
---|