mbenson     2005/04/08 12:41:58

  Modified:    docs/manual/CoreTypes filterchain.html
               src/main/org/apache/tools/ant/filters LineContains.java
                        LineContainsRegExp.java
               src/etc/testcases/filters build.xml
               src/testcases/org/apache/tools/ant/filters
                        LineContainsTest.java
  Added:       src/etc/testcases/filters/expected negatelinecontains.test
  Log:
  Add negate attribute to linecontains and linecontainsregexp filterreaders.
  PR: 34374
  
  Revision  Changes    Path
  1.26      +55 -0     ant/docs/manual/CoreTypes/filterchain.html
  
  Index: filterchain.html
  ===================================================================
  RCS file: /home/cvs/ant/docs/manual/CoreTypes/filterchain.html,v
  retrieving revision 1.25
  retrieving revision 1.26
  diff -u -r1.25 -r1.26
  --- filterchain.html  8 Apr 2005 19:14:34 -0000       1.25
  +++ filterchain.html  8 Apr 2005 19:41:58 -0000       1.26
  @@ -308,6 +308,12 @@
       <td vAlign=top align="center">Substring to be searched for.</td>
       <td vAlign=top align="center">Yes</td>
     </tr>
  +  <tr>
  +    <td vAlign=top>negate</td>
  +    <td vAlign=top align="center">Whether to select
  +      <i>non-</i>matching lines only. <b>Since Ant 1.7</b></td>
  +    <td vAlign=top align="center">No</td>
  +  </tr>
   </table>
   <p>
   <h4>Example:</h4>
  @@ -329,11 +335,46 @@
   &lt;/linecontains&gt;
   </pre></blockquote>
   
  +Negation:
  +<blockquote><pre>
  +&lt;filterreader 
classname=&quot;org.apache.tools.ant.filters.LineContains&quot;&gt;
  +  &lt;param type=&quot;negate&quot; value=&quot;true&quot;/&gt;
  +  &lt;param type=&quot;contains&quot; value=&quot;foo&quot;/&gt;
  +  &lt;param type=&quot;contains&quot; value=&quot;bar&quot;/&gt;
  +&lt;/filterreader&gt;
  +</pre></blockquote>
  +<i>or</i>
  +<blockquote><pre>
  +&lt;linecontains negate=&quot;true&quot;&gt;
  +  &lt;contains value=&quot;foo&quot;/&gt;
  +  &lt;contains value=&quot;bar&quot;/&gt;
  +&lt;/linecontains&gt;
  +</pre></blockquote>
  +
   <h3><a name="linecontainsregexp">LineContainsRegExp</a></h3>
   
   Filter which includes only those lines that contain the user-specified
   regular expression matching strings.
   
  +<table cellSpacing=0 cellPadding=2 border=1>
  +  <tr>
  +    <td vAlign=top><b>Parameter Type</b></td>
  +    <td vAlign=top><b>Parameter Value</b></td>
  +    <td vAlign=top align="center"><b>Required</b></td>
  +  </tr>
  +  <tr>
  +    <td vAlign=top>regexp</td>
  +    <td vAlign=top align="center">Regular expression to be searched for.</td>
  +    <td vAlign=top align="center">Yes</td>
  +  </tr>
  +  <tr>
  +    <td vAlign=top>negate</td>
  +    <td vAlign=top align="center">Whether to select
  +      <i>non-</i>matching lines only. <b>Since Ant 1.7</b></td>
  +    <td vAlign=top align="center">No</td>
  +  </tr>
  +</table>
  +
   See <a href="../CoreTypes/regexp.html">Regexp Type</a> for the description 
of the nested element regexp and of
   the choice of regular expression implementation.
   <h4>Example:</h4>
  @@ -352,6 +393,20 @@
   &lt;/linecontainsregexp&gt;
   </pre></blockquote>
   
  +Negation:
  +<blockquote><pre>
  +&lt;filterreader 
classname=&quot;org.apache.tools.ant.filters.LineContainsRegExp&quot;&gt;
  +  &lt;param type=&quot;negate&quot; value=&quot;true&quot;/&gt;
  +  &lt;param type=&quot;regexp&quot; value=&quot;foo*&quot;/&gt;
  +&lt;/filterreader&gt;
  +</pre></blockquote>
  +<i>or</i>
  +<blockquote><pre>
  +&lt;linecontainsregexp negate=&quot;true&quot;&gt;
  +  &lt;regexp pattern=&quot;foo*&quot;/&gt;
  +&lt;/linecontainsregexp&gt;
  +</pre></blockquote>
  +
   <h3><a name="prefixlines">PrefixLines</a></h3>
   
   Attaches a prefix to every line.
  
  
  
  1.18      +30 -15    
ant/src/main/org/apache/tools/ant/filters/LineContains.java
  
  Index: LineContains.java
  ===================================================================
  RCS file: 
/home/cvs/ant/src/main/org/apache/tools/ant/filters/LineContains.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- LineContains.java 25 Jan 2005 15:33:00 -0000      1.17
  +++ LineContains.java 8 Apr 2005 19:41:58 -0000       1.18
  @@ -19,6 +19,7 @@
   import java.io.IOException;
   import java.io.Reader;
   import java.util.Vector;
  +import org.apache.tools.ant.Project;
   import org.apache.tools.ant.types.Parameter;
   
   /**
  @@ -49,6 +50,9 @@
       /** Parameter name for the words to filter on. */
       private static final String CONTAINS_KEY = "contains";
   
  +    /** Parameter name for the words to filter on. */
  +    private static final String NEGATE_KEY = "negate";
  +
       /** Vector that holds the strings that input lines must contain. */
       private Vector contains = new Vector();
   
  @@ -59,6 +63,8 @@
        */
       private String line = null;
   
  +    private boolean negate = false;
  +
       /**
        * Constructor for "dummy" instances.
        *
  @@ -104,31 +110,22 @@
                   line = line.substring(1);
               }
           } else {
  -            line = readLine();
               final int containsSize = contains.size();
   
  -            while (line != null) {
  -                for (int i = 0; i < containsSize; i++) {
  +            for (line = readLine(); line != null; line = readLine()) {
  +                boolean matches = true;
  +                for (int i = 0; matches && i < containsSize; i++) {
                       String containsStr = (String) contains.elementAt(i);
  -                    if (line.indexOf(containsStr) == -1) {
  -                        line = null;
  -                        break;
  -                    }
  +                    matches = line.indexOf(containsStr) >= 0;
                   }
  -
  -                if (line == null) {
  -                    // line didn't match
  -                    line = readLine();
  -                } else {
  +                if (matches ^ isNegated()) {
                       break;
                   }
               }
  -
               if (line != null) {
                   return read();
               }
           }
  -
           return ch;
       }
   
  @@ -143,6 +140,22 @@
       }
   
       /**
  +     * Set the negation mode.  Default false (no negation).
  +     * @param b the boolean negation mode to set.
  +     */
  +    public void setNegate(boolean b) {
  +        negate = b;
  +    }
  +
  +    /**
  +     * Find out whether we have been negated.
  +     * @return boolean negation flag.
  +     */
  +    public boolean isNegated() {
  +        return negate;
  +    }
  +
  +    /**
        * Sets the vector of words which must be contained within a line read
        * from the original stream in order for it to match this filter.
        *
  @@ -179,7 +192,7 @@
       public Reader chain(final Reader rdr) {
           LineContains newFilter = new LineContains(rdr);
           newFilter.setContains(getContains());
  -        newFilter.setInitialized(true);
  +        newFilter.setNegate(isNegated());
           return newFilter;
       }
   
  @@ -192,6 +205,8 @@
               for (int i = 0; i < params.length; i++) {
                   if (CONTAINS_KEY.equals(params[i].getType())) {
                       contains.addElement(params[i].getValue());
  +                } else if (NEGATE_KEY.equals(params[i].getType())) {
  +                    setNegate(Project.toBoolean(params[i].getValue()));
                   }
               }
           }
  
  
  
  1.15      +32 -18    
ant/src/main/org/apache/tools/ant/filters/LineContainsRegExp.java
  
  Index: LineContainsRegExp.java
  ===================================================================
  RCS file: 
/home/cvs/ant/src/main/org/apache/tools/ant/filters/LineContainsRegExp.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- LineContainsRegExp.java   25 Jan 2005 15:33:00 -0000      1.14
  +++ LineContainsRegExp.java   8 Apr 2005 19:41:58 -0000       1.15
  @@ -19,6 +19,7 @@
   import java.io.IOException;
   import java.io.Reader;
   import java.util.Vector;
  +import org.apache.tools.ant.Project;
   import org.apache.tools.ant.types.Parameter;
   import org.apache.tools.ant.types.RegularExpression;
   import org.apache.tools.ant.util.regexp.Regexp;
  @@ -47,6 +48,9 @@
       /** Parameter name for the regular expression to filter on. */
       private static final String REGEXP_KEY = "regexp";
   
  +    /** Parameter name for the words to filter on. */
  +    private static final String NEGATE_KEY = "negate";
  +
       /** Vector that holds the expressions that input lines must contain. */
       private Vector regexps = new Vector();
   
  @@ -57,6 +61,8 @@
        */
       private String line = null;
   
  +    private boolean negate = false;
  +
       /**
        * Constructor for "dummy" instances.
        *
  @@ -103,34 +109,24 @@
                   line = line.substring(1);
               }
           } else {
  -            line = readLine();
               final int regexpsSize = regexps.size();
   
  -            while (line != null) {
  -                for (int i = 0; i < regexpsSize; i++) {
  -                    RegularExpression regexp = (RegularExpression)
  -                                                        regexps.elementAt(i);
  +            for (line = readLine(); line != null; line = readLine()) {
  +                boolean matches = true;
  +                for (int i = 0; matches && i < regexpsSize; i++) {
  +                    RegularExpression regexp
  +                        = (RegularExpression) regexps.elementAt(i);
                       Regexp re = regexp.getRegexp(getProject());
  -                    boolean matches = re.matches(line);
  -                    if (!matches) {
  -                        line = null;
  -                        break;
  -                    }
  +                    matches = re.matches(line);
                   }
  -
  -                if (line == null) {
  -                    // line didn't match
  -                    line = readLine();
  -                } else {
  +                if (matches ^ isNegated()) {
                       break;
                   }
               }
  -
               if (line != null) {
                   return read();
               }
           }
  -
           return ch;
       }
   
  @@ -184,11 +180,27 @@
       public Reader chain(final Reader rdr) {
           LineContainsRegExp newFilter = new LineContainsRegExp(rdr);
           newFilter.setRegexps(getRegexps());
  -        newFilter.setInitialized(true);
  +        newFilter.setNegate(isNegated());
           return newFilter;
       }
   
       /**
  +     * Set the negation mode.  Default false (no negation).
  +     * @param b the boolean negation mode to set.
  +     */
  +    public void setNegate(boolean b) {
  +        negate = b;
  +    }
  +
  +    /**
  +     * Find out whether we have been negated.
  +     * @return boolean negation flag.
  +     */
  +    public boolean isNegated() {
  +        return negate;
  +    }
  +
  +    /**
        * Parses parameters to add user defined regular expressions.
        */
       private void initialize() {
  @@ -200,6 +212,8 @@
                       RegularExpression regexp = new RegularExpression();
                       regexp.setPattern(pattern);
                       regexps.addElement(regexp);
  +                } else if (NEGATE_KEY.equals(params[i].getType())) {
  +                    setNegate(Project.toBoolean(params[i].getValue()));
                   }
               }
           }
  
  
  
  1.6       +20 -0     ant/src/etc/testcases/filters/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/ant/src/etc/testcases/filters/build.xml,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- build.xml 22 Apr 2003 18:23:54 -0000      1.5
  +++ build.xml 8 Apr 2005 19:41:58 -0000       1.6
  @@ -25,6 +25,26 @@
       </fixcrlf>-->
     </target>
     
  +  <target name="testNegateLineContains" depends="init">
  +    <copy file="input/linecontains.test"
  +          tofile="result/negatelinecontains.test">
  +      <filterchain>
  +        <filterreader classname="org.apache.tools.ant.filters.LineContains">
  +          <param type="negate" value="true"/>
  +          <param type="contains" value="beta"/>
  +        </filterreader>
  +      </filterchain>
  +    </copy>
  +    <fail>
  +      <condition>
  +        <not>
  +          <filesmatch file1="result/negatelinecontains.test"
  +                      file2="expected/negatelinecontains.test" />
  +        </not>
  +      </condition>
  +    </fail>
  +  </target>
  +  
     <target name="testEscapeUnicode" depends="init">
       <copy todir="result" encoding="UTF-8">
         <fileset dir="input">
  
  
  
  1.10      +4 -0      
ant/src/testcases/org/apache/tools/ant/filters/LineContainsTest.java
  
  Index: LineContainsTest.java
  ===================================================================
  RCS file: 
/home/cvs/ant/src/testcases/org/apache/tools/ant/filters/LineContainsTest.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- LineContainsTest.java     6 Jan 2005 12:05:09 -0000       1.9
  +++ LineContainsTest.java     8 Apr 2005 19:41:58 -0000       1.10
  @@ -48,4 +48,8 @@
           assertTrue(FILE_UTILS.contentEquals(expected, result));
       }
   
  +    public void testNegateLineContains() throws IOException {
  +        executeTarget("testNegateLineContains");
  +    }
  +
   }
  
  
  
  1.1                  
ant/src/etc/testcases/filters/expected/negatelinecontains.test
  
  Index: negatelinecontains.test
  ===================================================================
  This is line 1 with alpha.
  This is line 4 with gamma.
  This is line 6 with delta.
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to