Ignore:
Timestamp:
2008-08-08T13:11:30+12:00 (16 years ago)
Author:
oranfry
Message:

hacked ant now addresses all targets and supports -sim, -from, -to and -descend without need of a custom task

File:
1 edited

Legend:

Unmodified
Added
Removed
  • release-kits/shared/ant/src/main/org/apache/tools/ant/taskdefs/CallTarget.java

    r14982 r16685  
    1717
    1818package org.apache.tools.ant.taskdefs;
    19 
    2019import java.io.IOException;
    2120
    2221import org.apache.tools.ant.Task;
     22import org.apache.tools.ant.Target;
    2323import org.apache.tools.ant.BuildException;
    2424import org.apache.tools.ant.types.PropertySet;
     
    5757
    5858    private boolean targetSet = false;
     59    private String theTarget = null;
     60    private String address = null;
    5961
    6062    /**
     
    102104                 getLocation());
    103105        }
    104         callee.setAntfile(getProject().getProperty("ant.file"));
    105         callee.setInheritAll(inheritAll);
    106         callee.setInheritRefs(inheritRefs);
    107         callee.execute();
     106
     107            /* -- start target addressing code -- */
     108
     109            //figure out what the address of this instance should be
     110            String callingAddress = this.getOwningTarget().getAddress();
     111            int subAddress = this.getOwningTarget().getNextSubAddress();
     112            this.getOwningTarget().incrementNextSubAddress();
     113
     114            if ( callingAddress == null ) {
     115                address = "" + subAddress;
     116            } else {
     117                address = callingAddress + "." + subAddress;
     118            }
     119           
     120           
     121            //check that the target should be run given address, resume.descend, resume.from and resume.to
     122            String resumeFrom = this.getProject().getProperty("resume.from");
     123            String resumeDescend = this.getProject().getProperty("resume.descend");
     124            String resumeTo = this.getProject().getProperty("resume.to");
     125
     126            //given resumeFrom, resumeDescend and ResumeTo, might we execute?
     127            boolean shouldExecOnFrom = resumeFrom == null || isDescendantOf(resumeFrom,address) || isAfter(address,resumeFrom);
     128            boolean shouldExecuteOnDescend = resumeDescend == null || isDescendantOf(resumeDescend,address) || isDescendantOf(address,resumeDescend);
     129            boolean shouldExecOnTo = resumeTo == null || !isAfter(address,resumeTo);
     130
     131            //check that all are true
     132            boolean execute = shouldExecOnFrom && shouldExecuteOnDescend && shouldExecOnTo;
     133
     134            if ( !execute ) {
     135                return;
     136            }
     137
     138            /* -- end target addressing code -- */
     139
     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            callee.execute(theTarget,address);
     146
    108147    }
    109148
     
    153192        }
    154193        callee.setTarget(target);
     194        theTarget = target;
    155195        targetSet = true;
    156196    }
     
    234274        }
    235275    }
     276
     277
     278    /* --- node address relations for target addressing --- */
     279    public static boolean isAfter(String x, String y) {
     280
     281        System.out.println( "x: " + x );
     282        System.out.println( "y: " + y );
     283
     284        if ( x.equals("") || y.equals("") ) {
     285            throw new BuildException("Error - x or y not specified !!");
     286        }
     287
     288        //check that both are in the form int.int.int
     289
     290        //break into chambers
     291        String[] xs = x.split("\\.");
     292        String[] ys = y.split("\\.");
     293
     294        //figure out n
     295        int n = ys.length;
     296
     297        //step through, making sure x values are higher than or equal to y values
     298        for ( int i=0; i<n; i++ ) {
     299            int xValue = i<xs.length ? Integer.parseInt( xs[i] ) : 0;
     300            int yValue = Integer.parseInt( ys[i] );
     301
     302            System.out.println( xValue + " " + yValue );
     303            if(  xValue < yValue ) {
     304                System.out.println("x not after y");
     305                return false;
     306            }
     307
     308            if(  xValue > yValue ) {
     309                System.out.println("x is after y");
     310                return true;
     311            }
     312        }
     313
     314        // if execution reaches here, first n places are equal
     315        System.out.println("x is after y");
     316        return true;
     317    }
     318
     319
     320    public static boolean isDescendantOf(String x, String y) {
     321
     322        System.out.println( "x: " + x );
     323        System.out.println( "y: " + y );
     324
     325        if ( x.equals("") || y.equals("") ) {
     326            throw new BuildException("Error - x or y not specified !!");
     327        }
     328
     329        //check that both are in the form int.int.int
     330
     331        //break into chambers
     332        String[] xs = x.split("\\.");
     333        String[] ys = y.split("\\.");
     334
     335        //make sure x is at least as deep as y
     336        if ( xs.length < ys.length ) {
     337            System.out.println("x not descendant of y");
     338            return false;
     339        }
     340
     341        //figure out n
     342        int n = ys.length;
     343
     344        //step through, making sure x values are equal to y values
     345        for ( int i=0; i<n; i++ ) {
     346            int xValue = Integer.parseInt( xs[i] );
     347            int yValue = Integer.parseInt( ys[i] );
     348
     349            System.out.println( xValue + " " + yValue );
     350            if(  xValue != yValue ) {
     351                System.out.println("x not descendant of y");
     352                return false;
     353            }
     354        }
     355
     356        // if execution reaches here, first n places are equal
     357        System.out.println("x is descendant of y");
     358        return true;
     359    }
     360
     361    public static boolean isEqualTo(String x, String y) {
     362
     363        System.out.println( "x: " + x );
     364        System.out.println( "y: " + y );
     365
     366        if ( x.equals("") || y.equals("") ) {
     367            throw new BuildException("Error - x or y not specified !!");
     368        }
     369
     370        //check that both are in the form int.int.int
     371
     372        //break into chambers
     373        String[] xs = x.split("\\.");
     374        String[] ys = y.split("\\.");
     375
     376        //make sure x is exactly as deep as y
     377        if ( xs.length != ys.length ) {
     378            System.out.println("x not equal to y");
     379            return false;
     380        }
     381
     382        //figure out n
     383        int n = xs.length;
     384
     385        //step through, making sure x values are equal to y values
     386        for ( int i=0; i<n; i++ ) {
     387            int xValue = Integer.parseInt( xs[i] );
     388            int yValue = Integer.parseInt( ys[i] );
     389
     390            System.out.println( xValue + " " + yValue );
     391            if(  xValue != yValue ) {
     392                System.out.println("x not equal to y");
     393                return false;
     394            }
     395        }
     396
     397        // if execution reaches here, first all places
     398        System.out.println("x is equal to y");
     399        return true;
     400    }
     401
     402
    236403}
Note: See TracChangeset for help on using the changeset viewer.