ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject cvs commit: jakarta-ant/src/testcases/org/apache/tools/ant/types FilterSetTest.java
Date Wed, 12 Jun 2002 14:54:41 GMT
bodewig     2002/06/12 07:54:41

  Modified:    .        WHATSNEW
               docs/manual/CoreTypes filterset.html
               src/main/org/apache/tools/ant/types FilterSet.java
               src/testcases/org/apache/tools/ant/types FilterSetTest.java
  Log:
  recursively expand filter tokens.
  
  PR: 9378
  
  Revision  Changes    Path
  1.267     +2 -0      jakarta-ant/WHATSNEW
  
  Index: WHATSNEW
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/WHATSNEW,v
  retrieving revision 1.266
  retrieving revision 1.267
  diff -u -r1.266 -r1.267
  --- WHATSNEW	1 Jun 2002 12:26:31 -0000	1.266
  +++ WHATSNEW	12 Jun 2002 14:54:40 -0000	1.267
  @@ -9,6 +9,8 @@
   * <echoproperties> has a new srcfile attribute that can make it read
   properties files and output them instead of Ant's properties.
   
  +* <filterset> will now resolve filters recursively.
  +
   Changes from Ant 1.4.1 to Ant 1.5
   =================================
   
  
  
  
  1.7       +1 -0      jakarta-ant/docs/manual/CoreTypes/filterset.html
  
  Index: filterset.html
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/docs/manual/CoreTypes/filterset.html,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- filterset.html	25 Mar 2002 20:21:11 -0000	1.6
  +++ filterset.html	12 Jun 2002 14:54:40 -0000	1.7
  @@ -19,6 +19,7 @@
   <code>endtoken</code> attributes to define what to match.</p>
   <p>Filtersets are used for doing 
   replacements in tasks such as <code>&lt;copy&gt;</code>, etc.</p>
  +<p>Nested filters are possible and a message will be given in case a value in a filter
chain is called for a second time and thus causing an infinite loop. The originating token
will be passed back when an infinite loop occurs.
   
   <H2>Filterset</H2>
   
  
  
  
  1.14      +53 -0     jakarta-ant/src/main/org/apache/tools/ant/types/FilterSet.java
  
  Index: FilterSet.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/types/FilterSet.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- FilterSet.java	15 Apr 2002 14:56:34 -0000	1.13
  +++ FilterSet.java	12 Jun 2002 14:54:41 -0000	1.14
  @@ -75,6 +75,7 @@
    * A filter set may have begintoken and endtokens defined.
    *
    * @author     <A href="mailto:gholam@xtra.co.nz">  Michael McCallum  </A>
  + * @author     <A href="mailto:martin@mvdb.net">  Martin van den Bemt </A>
    */
   public class FilterSet extends DataType implements Cloneable {
       
  @@ -354,6 +355,10 @@
                       b.append(line.substring(i, index));
                       if (tokens.containsKey(token)) {
                           value = (String) tokens.get(token);
  +                        if (!value.equals(token)) {
  +                            // we have another token, let's parse it.
  +                            value = replaceTokens(value, token);
  +                        }
                           log("Replacing: " + beginToken + token + endToken 
                               + " -> " + value, Project.MSG_VERBOSE);
                           b.append(value);
  @@ -374,6 +379,54 @@
           } else {
              return line;
           }
  +    }
  +    
  +    /** Contains a list of parsed tokens */
  +    private Vector passedTokens;
  +    /** if a ducplicate token is found, this is set to true */
  +    private boolean duplicateToken = false;
  +    
  +    /**
  +     * This parses tokens which point to tokens.
  +     * It also maintains a list of currently used tokens, so we cannot
  +     * get into an infinite loop
  +     * @param value the value / token to parse
  +     * @param parent the parant token (= the token it was parsed from)
  +     */
  +    private String replaceTokens(String line, String parent)
  +    throws BuildException
  +    {
  +        if (passedTokens == null) {
  +            passedTokens = new Vector();
  +        }
  +        if (passedTokens.contains(parent) && !duplicateToken) {
  +            duplicateToken = true;
  +            StringBuffer sb = new StringBuffer();
  +            sb.append("Inifinite loop in tokens. Currently known tokens : ");
  +            sb.append(passedTokens);
  +            sb.append("\nProblem token : "+getBeginToken()+parent+getEndToken());
  +            sb.append(" called from "+getBeginToken()+passedTokens.lastElement());
  +            sb.append(getEndToken());
  +            System.out.println(sb.toString());
  +            return parent;
  +        }
  +        passedTokens.addElement(parent);
  +        String value = this.replaceTokens(line);
  +        if (value.indexOf(getBeginToken()) == -1 && !duplicateToken) {
  +            duplicateToken = false;
  +            passedTokens = null;
  +        } else if(duplicateToken) {
  +            // should always be the case...
  +            if (passedTokens.size() > 0) {
  +                value = (String) passedTokens.lastElement();
  +                passedTokens.removeElementAt(passedTokens.size()-1);
  +                if (passedTokens.size() == 0) {
  +                    value = getBeginToken()+value+getEndToken();
  +                    duplicateToken = false;
  +                }
  +            }
  +        }
  +        return value;
       }
       
       /**
  
  
  
  1.7       +37 -0     jakarta-ant/src/testcases/org/apache/tools/ant/types/FilterSetTest.java
  
  Index: FilterSetTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/testcases/org/apache/tools/ant/types/FilterSetTest.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- FilterSetTest.java	19 Mar 2002 07:02:12 -0000	1.6
  +++ FilterSetTest.java	12 Jun 2002 14:54:41 -0000	1.7
  @@ -67,6 +67,7 @@
    * FilterSet testing
    *
    * @author Conor MacNeill
  + * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
    */
   public class FilterSetTest extends BuildFileTest {
   
  @@ -100,6 +101,42 @@
           executeTarget("test3");
           assertTrue("Filterset 3 failed", compareFiles("src/etc/testcases/types/gold/filterset3.txt",
                                                         "src/etc/testcases/types/dest3.txt"));
  +    }
  +
  +    /**
  +     * This will test the recursive FilterSet.  Which means that if
  +     * the filter value @test@ contains another filter value, it will
  +     * actually resolve.  
  +     */
  +    public void testRecursive() {
  +        System.out.println("testRecursive");
  +        String result = "it works line";
  +        String line="@test@ line";
  +        FilterSet fs = new FilterSet();
  +        fs.addFilter("test", "@test1@");
  +        fs.addFilter("test1","@test2@");
  +        fs.addFilter("test2", "it works");
  +        fs.setBeginToken("@");
  +        fs.setEndToken("@");
  +        assertEquals(result, fs.replaceTokens(line));
  +    }
  +
  +    /**
  +     * Test to see what happens when the resolving occurs in an
  +     * infinite loop.
  +     */
  +    public void testInfinite() {
  +        System.out.println("testInfinite");
  +        String result = "@test@ line testvalue";
  +        String line = "@test@ line @test3@";
  +        FilterSet fs = new FilterSet();
  +        fs.addFilter("test", "@test1@");
  +        fs.addFilter("test1","@test2@");
  +        fs.addFilter("test2", "@test@");
  +        fs.addFilter("test3", "testvalue");
  +        fs.setBeginToken("@");
  +        fs.setEndToken("@");
  +        assertEquals(result, fs.replaceTokens(line));
       }
   
       private boolean compareFiles(String name1, String name2) {
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message