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.000000000 -0500
+++ /tmp/ContainsSelector.java	2003-04-09 17:59:11.000000000 -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.000000000 -0500
+++ /tmp/selectors.html	2003-04-09 17:59:17.000000000 -0500
@@ -86,6 +86,13 @@
         </td>
         <td valign="top" align="center">No</td>
       </tr>
+      <tr>
+        <td valign="top">regularexpression</td>
+        <td valign="top">Use the <code>text</code> attribute as a 
+          regular expression. Default is No.
+        </td>
+        <td valign="top" align="center">No</td>
+      </tr>
     </table>
 
     <p>Here is an example of how to use the Contains Selector:</p>

Reply via email to