diff --git a/WHATSNEW b/WHATSNEW index 626b9328b..563f2536a 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -169,6 +169,13 @@ Other changes: * now also works for non-filesystem resources. Bugzilla Report 49756. + * The filter now supports a casesensitive + attribute. + + * The selector now supports casesensitive, multiline + and singleline attributes. + Bugzilla Report 49764. + Changes from Ant 1.8.0 TO Ant 1.8.1 =================================== diff --git a/docs/manual/Types/filterchain.html b/docs/manual/Types/filterchain.html index 9ba40eab7..e20fea470 100644 --- a/docs/manual/Types/filterchain.html +++ b/docs/manual/Types/filterchain.html @@ -414,6 +414,12 @@ regular expression matching strings. non-matching lines only. Since Ant 1.7 No + + casesensitive + Perform a case sensitive + match. Default is true. Since Ant 1.8.2 + No + See Regexp Type for the description of the nested element regexp and of diff --git a/docs/manual/Types/selectors.html b/docs/manual/Types/selectors.html index e9b1ed045..8bcd9b7cf 100644 --- a/docs/manual/Types/selectors.html +++ b/docs/manual/Types/selectors.html @@ -557,6 +557,28 @@ match true in every file Yes + + casesensitive + Perform a case sensitive match. Default is + true. since Ant 1.8.2 + No + + + multiline + + Perform a multi line match. + Default is false. since Ant 1.8.2 + No + + + singleline + + This allows '.' to match new lines. + SingleLine is not to be confused with multiline, SingleLine is a perl + regex term, it corresponds to dotall in java regex. + Default is false. since Ant 1.8.2 + No +

Here is an example of how to use the regular expression Selector:

diff --git a/src/main/org/apache/tools/ant/filters/LineContainsRegExp.java b/src/main/org/apache/tools/ant/filters/LineContainsRegExp.java index 49ef34d19..be5f2bc19 100644 --- a/src/main/org/apache/tools/ant/filters/LineContainsRegExp.java +++ b/src/main/org/apache/tools/ant/filters/LineContainsRegExp.java @@ -24,6 +24,7 @@ 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; +import org.apache.tools.ant.util.regexp.RegexpUtil; /** * Filter which includes only those lines that contain the user-specified @@ -49,9 +50,12 @@ public final class LineContainsRegExp /** Parameter name for the regular expression to filter on. */ private static final String REGEXP_KEY = "regexp"; - /** Parameter name for the words to filter on. */ + /** Parameter name for the negate attribute. */ private static final String NEGATE_KEY = "negate"; + /** Parameter name for the casesensitive attribute. */ + private static final String CS_KEY = "casesensitive"; + /** Vector that holds the expressions that input lines must contain. */ private Vector regexps = new Vector(); @@ -63,6 +67,7 @@ public final class LineContainsRegExp private String line = null; private boolean negate = false; + private int regexpOptions = Regexp.MATCH_DEFAULT; /** * Constructor for "dummy" instances. @@ -118,7 +123,7 @@ public final class LineContainsRegExp RegularExpression regexp = (RegularExpression) regexps.elementAt(i); Regexp re = regexp.getRegexp(getProject()); - matches = re.matches(line); + matches = re.matches(line, regexpOptions); } if (matches ^ isNegated()) { break; @@ -182,6 +187,10 @@ public final class LineContainsRegExp LineContainsRegExp newFilter = new LineContainsRegExp(rdr); newFilter.setRegexps(getRegexps()); newFilter.setNegate(isNegated()); + newFilter + .setCaseSensitive(!RegexpUtil.hasFlag(regexpOptions, + Regexp.MATCH_CASE_INSENSITIVE) + ); return newFilter; } @@ -193,6 +202,14 @@ public final class LineContainsRegExp negate = b; } + /** + * Whether to match casesensitevly. + * @since Ant 1.8.2 + */ + public void setCaseSensitive(boolean b) { + regexpOptions = RegexpUtil.asOptions(b); + } + /** * Find out whether we have been negated. * @return boolean negation flag. @@ -215,6 +232,8 @@ public final class LineContainsRegExp regexps.addElement(regexp); } else if (NEGATE_KEY.equals(params[i].getType())) { setNegate(Project.toBoolean(params[i].getValue())); + } else if (CS_KEY.equals(params[i].getType())) { + setCaseSensitive(Project.toBoolean(params[i].getValue())); } } } diff --git a/src/main/org/apache/tools/ant/types/selectors/ContainsRegexpSelector.java b/src/main/org/apache/tools/ant/types/selectors/ContainsRegexpSelector.java index 4de2725eb..2d84306b6 100644 --- a/src/main/org/apache/tools/ant/types/selectors/ContainsRegexpSelector.java +++ b/src/main/org/apache/tools/ant/types/selectors/ContainsRegexpSelector.java @@ -24,12 +24,14 @@ import java.io.IOException; import java.io.InputStreamReader; import org.apache.tools.ant.BuildException; +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.types.Resource; import org.apache.tools.ant.types.resources.FileResource; import org.apache.tools.ant.types.resources.selectors.ResourceSelector; import org.apache.tools.ant.util.regexp.Regexp; +import org.apache.tools.ant.util.regexp.RegexpUtil; /** * Selector that filters files based on a regular expression. @@ -42,8 +44,17 @@ public class ContainsRegexpSelector extends BaseExtendSelector private String userProvidedExpression = null; private RegularExpression myRegExp = null; private Regexp myExpression = null; + private boolean caseSensitive = true; + private boolean multiLine = false; + private boolean singleLine = false; /** Key to used for parameterized custom selector */ public static final String EXPRESSION_KEY = "expression"; + /** Parameter name for the casesensitive attribute. */ + private static final String CS_KEY = "casesensitive"; + /** Parameter name for the multiline attribute. */ + private static final String ML_KEY = "multiline"; + /** Parameter name for the singleline attribute. */ + private static final String SL_KEY = "singleline"; /** * Creates a new ContainsRegexpSelector instance. @@ -71,6 +82,34 @@ public class ContainsRegexpSelector extends BaseExtendSelector this.userProvidedExpression = theexpression; } + /** + * Whether to ignore case or not. + * @param b if false, ignore case. + * @since Ant 1.8.2 + */ + public void setCaseSensitive(boolean b) { + caseSensitive = b; + } + + /** + * Whether to match should be multiline. + * @param b the value to set. + * @since Ant 1.8.2 + */ + public void setMultiLine(boolean b) { + multiLine = b; + } + + /** + * Whether to treat input as singleline ('.' matches newline). + * Corresponsds to java.util.regex.Pattern.DOTALL. + * @param b the value to set. + * @since Ant 1.8.2 + */ + public void setSingleLine(boolean b) { + singleLine = b; + } + /** * When using this as a custom selector, this method will be called. * It translates each parameter into the appropriate setXXX() call. @@ -84,6 +123,13 @@ public class ContainsRegexpSelector extends BaseExtendSelector String paramname = parameters[i].getName(); if (EXPRESSION_KEY.equalsIgnoreCase(paramname)) { setExpression(parameters[i].getValue()); + } else if (CS_KEY.equalsIgnoreCase(paramname)) { + setCaseSensitive(Project + .toBoolean(parameters[i].getValue())); + } else if (ML_KEY.equalsIgnoreCase(paramname)) { + setMultiLine(Project.toBoolean(parameters[i].getValue())); + } else if (SL_KEY.equalsIgnoreCase(paramname)) { + setSingleLine(Project.toBoolean(parameters[i].getValue())); } else { setError("Invalid parameter " + paramname); } @@ -148,7 +194,10 @@ public class ContainsRegexpSelector extends BaseExtendSelector while (teststr != null) { - if (myExpression.matches(teststr)) { + if (myExpression.matches(teststr, + RegexpUtil.asOptions(caseSensitive, + multiLine, + singleLine))) { return true; } teststr = in.readLine();