PR: 34374 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@278124 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -308,6 +308,12 @@ strings. | |||||
| <td vAlign=top align="center">Substring to be searched for.</td> | <td vAlign=top align="center">Substring to be searched for.</td> | ||||
| <td vAlign=top align="center">Yes</td> | <td vAlign=top align="center">Yes</td> | ||||
| </tr> | </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> | </table> | ||||
| <p> | <p> | ||||
| <h4>Example:</h4> | <h4>Example:</h4> | ||||
| @@ -329,11 +335,46 @@ Convenience method: | |||||
| </linecontains> | </linecontains> | ||||
| </pre></blockquote> | </pre></blockquote> | ||||
| Negation: | |||||
| <blockquote><pre> | |||||
| <filterreader classname="org.apache.tools.ant.filters.LineContains"> | |||||
| <param type="negate" value="true"/> | |||||
| <param type="contains" value="foo"/> | |||||
| <param type="contains" value="bar"/> | |||||
| </filterreader> | |||||
| </pre></blockquote> | |||||
| <i>or</i> | |||||
| <blockquote><pre> | |||||
| <linecontains negate="true"> | |||||
| <contains value="foo"/> | |||||
| <contains value="bar"/> | |||||
| </linecontains> | |||||
| </pre></blockquote> | |||||
| <h3><a name="linecontainsregexp">LineContainsRegExp</a></h3> | <h3><a name="linecontainsregexp">LineContainsRegExp</a></h3> | ||||
| Filter which includes only those lines that contain the user-specified | Filter which includes only those lines that contain the user-specified | ||||
| regular expression matching strings. | 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 | 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. | the choice of regular expression implementation. | ||||
| <h4>Example:</h4> | <h4>Example:</h4> | ||||
| @@ -352,6 +393,20 @@ Convenience method: | |||||
| </linecontainsregexp> | </linecontainsregexp> | ||||
| </pre></blockquote> | </pre></blockquote> | ||||
| Negation: | |||||
| <blockquote><pre> | |||||
| <filterreader classname="org.apache.tools.ant.filters.LineContainsRegExp"> | |||||
| <param type="negate" value="true"/> | |||||
| <param type="regexp" value="foo*"/> | |||||
| </filterreader> | |||||
| </pre></blockquote> | |||||
| <i>or</i> | |||||
| <blockquote><pre> | |||||
| <linecontainsregexp negate="true"> | |||||
| <regexp pattern="foo*"/> | |||||
| </linecontainsregexp> | |||||
| </pre></blockquote> | |||||
| <h3><a name="prefixlines">PrefixLines</a></h3> | <h3><a name="prefixlines">PrefixLines</a></h3> | ||||
| Attaches a prefix to every line. | Attaches a prefix to every line. | ||||
| @@ -25,6 +25,26 @@ | |||||
| </fixcrlf>--> | </fixcrlf>--> | ||||
| </target> | </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"> | <target name="testEscapeUnicode" depends="init"> | ||||
| <copy todir="result" encoding="UTF-8"> | <copy todir="result" encoding="UTF-8"> | ||||
| <fileset dir="input"> | <fileset dir="input"> | ||||
| @@ -0,0 +1,3 @@ | |||||
| This is line 1 with alpha. | |||||
| This is line 4 with gamma. | |||||
| This is line 6 with delta. | |||||
| @@ -19,6 +19,7 @@ package org.apache.tools.ant.filters; | |||||
| import java.io.IOException; | import java.io.IOException; | ||||
| import java.io.Reader; | import java.io.Reader; | ||||
| import java.util.Vector; | import java.util.Vector; | ||||
| import org.apache.tools.ant.Project; | |||||
| import org.apache.tools.ant.types.Parameter; | import org.apache.tools.ant.types.Parameter; | ||||
| /** | /** | ||||
| @@ -49,6 +50,9 @@ public final class LineContains | |||||
| /** Parameter name for the words to filter on. */ | /** Parameter name for the words to filter on. */ | ||||
| private static final String CONTAINS_KEY = "contains"; | 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. */ | /** Vector that holds the strings that input lines must contain. */ | ||||
| private Vector contains = new Vector(); | private Vector contains = new Vector(); | ||||
| @@ -59,6 +63,8 @@ public final class LineContains | |||||
| */ | */ | ||||
| private String line = null; | private String line = null; | ||||
| private boolean negate = false; | |||||
| /** | /** | ||||
| * Constructor for "dummy" instances. | * Constructor for "dummy" instances. | ||||
| * | * | ||||
| @@ -104,31 +110,22 @@ public final class LineContains | |||||
| line = line.substring(1); | line = line.substring(1); | ||||
| } | } | ||||
| } else { | } else { | ||||
| line = readLine(); | |||||
| final int containsSize = contains.size(); | 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); | 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; | break; | ||||
| } | } | ||||
| } | } | ||||
| if (line != null) { | if (line != null) { | ||||
| return read(); | return read(); | ||||
| } | } | ||||
| } | } | ||||
| return ch; | return ch; | ||||
| } | } | ||||
| @@ -142,6 +139,22 @@ public final class LineContains | |||||
| this.contains.addElement(contains.getValue()); | this.contains.addElement(contains.getValue()); | ||||
| } | } | ||||
| /** | |||||
| * 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 | * 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. | * from the original stream in order for it to match this filter. | ||||
| @@ -179,7 +192,7 @@ public final class LineContains | |||||
| public Reader chain(final Reader rdr) { | public Reader chain(final Reader rdr) { | ||||
| LineContains newFilter = new LineContains(rdr); | LineContains newFilter = new LineContains(rdr); | ||||
| newFilter.setContains(getContains()); | newFilter.setContains(getContains()); | ||||
| newFilter.setInitialized(true); | |||||
| newFilter.setNegate(isNegated()); | |||||
| return newFilter; | return newFilter; | ||||
| } | } | ||||
| @@ -192,6 +205,8 @@ public final class LineContains | |||||
| for (int i = 0; i < params.length; i++) { | for (int i = 0; i < params.length; i++) { | ||||
| if (CONTAINS_KEY.equals(params[i].getType())) { | if (CONTAINS_KEY.equals(params[i].getType())) { | ||||
| contains.addElement(params[i].getValue()); | contains.addElement(params[i].getValue()); | ||||
| } else if (NEGATE_KEY.equals(params[i].getType())) { | |||||
| setNegate(Project.toBoolean(params[i].getValue())); | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| @@ -19,6 +19,7 @@ package org.apache.tools.ant.filters; | |||||
| import java.io.IOException; | import java.io.IOException; | ||||
| import java.io.Reader; | import java.io.Reader; | ||||
| import java.util.Vector; | import java.util.Vector; | ||||
| import org.apache.tools.ant.Project; | |||||
| import org.apache.tools.ant.types.Parameter; | import org.apache.tools.ant.types.Parameter; | ||||
| import org.apache.tools.ant.types.RegularExpression; | import org.apache.tools.ant.types.RegularExpression; | ||||
| import org.apache.tools.ant.util.regexp.Regexp; | import org.apache.tools.ant.util.regexp.Regexp; | ||||
| @@ -47,6 +48,9 @@ public final class LineContainsRegExp | |||||
| /** Parameter name for the regular expression to filter on. */ | /** Parameter name for the regular expression to filter on. */ | ||||
| private static final String REGEXP_KEY = "regexp"; | 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. */ | /** Vector that holds the expressions that input lines must contain. */ | ||||
| private Vector regexps = new Vector(); | private Vector regexps = new Vector(); | ||||
| @@ -57,6 +61,8 @@ public final class LineContainsRegExp | |||||
| */ | */ | ||||
| private String line = null; | private String line = null; | ||||
| private boolean negate = false; | |||||
| /** | /** | ||||
| * Constructor for "dummy" instances. | * Constructor for "dummy" instances. | ||||
| * | * | ||||
| @@ -103,34 +109,24 @@ public final class LineContainsRegExp | |||||
| line = line.substring(1); | line = line.substring(1); | ||||
| } | } | ||||
| } else { | } else { | ||||
| line = readLine(); | |||||
| final int regexpsSize = regexps.size(); | 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()); | 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; | break; | ||||
| } | } | ||||
| } | } | ||||
| if (line != null) { | if (line != null) { | ||||
| return read(); | return read(); | ||||
| } | } | ||||
| } | } | ||||
| return ch; | return ch; | ||||
| } | } | ||||
| @@ -184,10 +180,26 @@ public final class LineContainsRegExp | |||||
| public Reader chain(final Reader rdr) { | public Reader chain(final Reader rdr) { | ||||
| LineContainsRegExp newFilter = new LineContainsRegExp(rdr); | LineContainsRegExp newFilter = new LineContainsRegExp(rdr); | ||||
| newFilter.setRegexps(getRegexps()); | newFilter.setRegexps(getRegexps()); | ||||
| newFilter.setInitialized(true); | |||||
| newFilter.setNegate(isNegated()); | |||||
| return newFilter; | 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. | * Parses parameters to add user defined regular expressions. | ||||
| */ | */ | ||||
| @@ -200,6 +212,8 @@ public final class LineContainsRegExp | |||||
| RegularExpression regexp = new RegularExpression(); | RegularExpression regexp = new RegularExpression(); | ||||
| regexp.setPattern(pattern); | regexp.setPattern(pattern); | ||||
| regexps.addElement(regexp); | regexps.addElement(regexp); | ||||
| } else if (NEGATE_KEY.equals(params[i].getType())) { | |||||
| setNegate(Project.toBoolean(params[i].getValue())); | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| @@ -48,4 +48,8 @@ public class LineContainsTest extends BuildFileTest { | |||||
| assertTrue(FILE_UTILS.contentEquals(expected, result)); | assertTrue(FILE_UTILS.contentEquals(expected, result)); | ||||
| } | } | ||||
| public void testNegateLineContains() throws IOException { | |||||
| executeTarget("testNegateLineContains"); | |||||
| } | |||||
| } | } | ||||