Re: [PATCH] add regular expressions to ContainsSelector.java
On Thu, 10 Apr 2003, Jay [EMAIL PROTECTED] wrote: I was thinking that the filters were more geared to changing text, not just identifiying a file has a specific content. You are absolutely correct. What I tried to say in my clumsy way: We have to tasks that perform replacements, replace and replaceregexp. We have two filter readers that select lines based on matching, linecontains and linecontainsregexp. Following that it would be consistent to have to selectors based on matching, contains and containsregexp. So would it be ok to make ContainsRegexpSelector under /ant/types/selectors that is basically the same as ContainsSelector but just accepts a single parm and does the regular expression search only? Exactly. Thanks for your time on this. Thank you Stefan
[PATCH] add regular expressions to ContainsSelector.java
Hello, Attached is a update to ContainsSelector.java (based on 1.5.3) that allows the users to specify the text attribute is to be used as a regular expression. I found it useful when we needed to select files based on more than a simple string comparison. Could this patch be applied to the base? Thanks, Jay van der Meer --- ContainsSelector.java 2003-04-08 10:24:06.0 -0500 +++ /tmp/ContainsSelector.java 2003-04-09 17:59:11.0 -0500 @@ -63,6 +63,8 @@ import org.apache.tools.ant.Project; import org.apache.tools.ant.types.Parameter; import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.types.RegularExpression; +import org.apache.tools.ant.util.regexp.Regexp; /** * Selector that filters files based on whether they contain a @@ -75,8 +77,12 @@ private String contains = null; private boolean casesensitive = true; +private boolean textisregexp = false; +private RegularExpression MyRegExp = null; +private Regexp MyExpression = null; public final static String CONTAINS_KEY = text; public final static String CASE_KEY = casesensitive; +public final static String REGEXP_KEY = regularexpression; public ContainsSelector() { @@ -91,6 +97,12 @@ } else { buf.append(false); } +buf.append( regularexpression: ); +if (textisregexp) { +buf.append(true); +} else { +buf.append(false); +} buf.append(}); return buf.toString(); } @@ -112,6 +124,15 @@ public void setCasesensitive(boolean casesensitive) { this.casesensitive = casesensitive; } + +/** + * Whether to use the text as a regular expression or not. + * + * @param use text as a regular expression + */ +public void setRegularExpression(boolean textisregexp) { +this.textisregexp = textisregexp; +} /** * When using this as a custom selector, this method will be called. @@ -131,6 +152,10 @@ setCasesensitive(Project.toBoolean( parameters[i].getValue())); } +else if (REGEXP_KEY.equalsIgnoreCase(paramname)) { +setRegularExpression(Project.toBoolean( +parameters[i].getValue())); + } else { setError(Invalid parameter + paramname); } @@ -159,30 +184,48 @@ * @return whether the file should be selected or not */ public boolean isSelected(File basedir, String filename, File file) { - + boolean mymatch = false; + String userstr = contains; + // throw BuildException on error validate(); if (file.isDirectory()) { return true; } - -String userstr = contains; -if (!casesensitive) { -userstr = contains.toLowerCase(); -} + + if (textisregexp == true) { + if (MyRegExp == null) { + MyRegExp = new RegularExpression(); + MyRegExp.setPattern(contains); + MyExpression = MyRegExp.getRegexp(getProject()); + } + } + else { + if (!casesensitive) { + userstr = contains.toLowerCase(); + } + } + BufferedReader in = null; try { in = new BufferedReader(new InputStreamReader( new FileInputStream(file))); String teststr = in.readLine(); while (teststr != null) { -if (!casesensitive) { -teststr = teststr.toLowerCase(); -} -if (teststr.indexOf(userstr) -1) { -return true; -} + if (textisregexp == true) { + mymatch = MyExpression.matches(teststr); + if (mymatch == true) + return true; + } + else { + if (!casesensitive) { + teststr = teststr.toLowerCase(); + } + if (teststr.indexOf(userstr) -1) { + return true; + } + } teststr = in.readLine(); } return false; --- selectors.html 2003-04-08 10:24:19.0 -0500 +++ /tmp/selectors.html 2003-04-09 17:59:17.0 -0500 @@ -86,6 +86,13 @@ /td td valign=top align=centerNo/td /tr + tr +td valign=topregularexpression/td +td valign=topUse the codetext/code attribute as a + regular expression. Default is No. +/td +td valign=top align=centerNo/td + /tr /table pHere is an example of how to use the Contains Selector:/p
Re: [PATCH] add regular expressions to ContainsSelector.java
I'm still new to how ant is organized, let me make sure I understand the preferred way to do this. I had started to do the patch as a filter under /ant/filters but I was thinking that the filters were more geared to changing text, not just identifiying a file has a specific content. So would it be ok to make ContainsRegexpSelector under /ant/types/selectors that is basically the same as ContainsSelector but just accepts a single parm and does the regular expression search only? If that's not what you were asking for could you point me in the right direction? Thanks for your time on this. Jay van der Meer On Thursday 10 April 2003 01:13, Stefan Bodewig wrote: On Wed, 09 Apr 2003, Jay [EMAIL PROTECTED] wrote: Attached is a update to ContainsSelector.java (based on 1.5.3) that allows the users to specify the text attribute is to be used as a regular expression. I'd prefer a separate ContainsRegexpSelector for this. This would mirror the replace tasks or the linecontains filter readers. Stefan - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: [PATCH] add regular expressions to ContainsSelector.java
I'll try to answer for Stefan, since he won't read your message until tomorrow because of his timezone... Yes, 'make ContainsRegexpSelector under /ant/types/selectors ...'. I believe Stefan was merely pointing out that there are separate classes for filtering on string literals and regular expressions, and thus there should be separate selectors selecting on string literals and regular expressions, to remain consistent. Does that help? --DD -Original Message- From: Jay [mailto:[EMAIL PROTECTED] Sent: Thursday, April 10, 2003 5:19 PM To: Ant Developers List Subject: Re: [PATCH] add regular expressions to ContainsSelector.java I'm still new to how ant is organized, let me make sure I understand the preferred way to do this. I had started to do the patch as a filter under /ant/filters but I was thinking that the filters were more geared to changing text, not just identifiying a file has a specific content. So would it be ok to make ContainsRegexpSelector under /ant/types/selectors that is basically the same as ContainsSelector but just accepts a single parm and does the regular expression search only? If that's not what you were asking for could you point me in the right direction? Thanks for your time on this. Jay van der Meer On Thursday 10 April 2003 01:13, Stefan Bodewig wrote: On Wed, 09 Apr 2003, Jay [EMAIL PROTECTED] wrote: Attached is a update to ContainsSelector.java (based on 1.5.3) that allows the users to specify the text attribute is to be used as a regular expression. I'd prefer a separate ContainsRegexpSelector for this. This would mirror the replace tasks or the linecontains filter readers. Stefan