ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bruce Atherton <br...@callenish.com>
Subject cullers
Date Sun, 24 Feb 2002 20:37:14 GMT
Here, as promised, is my cullers submission. It is still in early stages, and 
needs things like tests and many more cullers (including a dynamic load one), 
but represents the essence of what is involved.

I've included two cullers so far, <sizecull> and <datecull>, but implementing

new ones is fairly straightforward. I've included a bit of documentation to 
show you how it all works, though it is not yet ready for prime time. It is 
cullers.html in the types/cullers directory.

The tar file should be extracted in src/main/org/apache/tools/ant. If you 
want to put it into the proposal sandbox, go ahead.

I've also included a patch file below for those existing files that I have 
made changes to, for those for whom patches are an easier read. I don't think 
any of my changes could possibly impact people using the classes 
programatically, save for the unavoidable namespace collisions. Note that I 
put any added methods of DirectoryScanner into CullerScanner, so anyone using 
FileScanner will not experience problems with unimplemented methods.

All feedback, suggestions for improvement, pointing out of errors, and 
comparisons with the selectors proposal welcomed.

Index: DirectoryScanner.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/DirectoryScanner.java,v
retrieving revision 1.22
diff -u -b -r1.22 DirectoryScanner.java
--- DirectoryScanner.java	18 Feb 2002 18:27:58 -0000	1.22
+++ DirectoryScanner.java	24 Feb 2002 20:22:32 -0000
@@ -58,6 +58,9 @@
 import java.util.Vector;
 import java.util.StringTokenizer;
 
+import org.apache.tools.ant.types.cullers.CullerScanner;
+import org.apache.tools.ant.types.cullers.Culler;
+
 /**
  * Class for scanning a directory for files/directories which match certain
  * criteria.
@@ -140,7 +143,7 @@
  * <a href="mailto:ajkuiper@wxs.nl">ajkuiper@wxs.nl</a>
  * @author <a href="mailto:umagesh@rediffmail.com">Magesh Umasankar</a>
  */
-public class DirectoryScanner implements FileScanner {
+public class DirectoryScanner implements FileScanner, CullerScanner {
 
     /**
      * Patterns which should be excluded by default.
@@ -170,7 +173,9 @@
     /** The patterns for the files to be excluded. */
     protected String[] excludes;
 
-    /** The files which matched at least one include and no excludes. */
+    /** The files which matched at least one include and no excludes
+     *  and were not culled.
+     */
     protected Vector filesIncluded;
 
     /** The files which did not match any includes. */
@@ -182,7 +187,9 @@
      */
     protected Vector filesExcluded;
 
-    /** The directories which matched at least one include and no excludes. 
*/
+    /** The directories which matched at least one include and no excludes
+     *  and were not culled.
+     */
     protected Vector dirsIncluded;
 
     /** The directories which were found and did not match any includes. */
@@ -194,6 +201,19 @@
      */
     protected Vector dirsExcluded;
 
+    /** Cullers that will filter files out of our candidate list. */
+    protected Culler[] cullers = null;
+
+    /** The files which matched at least one include and no excludes but
+     *  which a culler discarded.
+     */
+    protected Vector filesCulled;
+
+    /** The directories which matched at least one include and no excludes
+     *  but which a culler discarded.
+     */
+    protected Vector dirsCulled;
+
     /** Whether or not our results were built by a slow scan. */
     protected boolean haveSlowResults = false;
 
@@ -711,6 +731,17 @@
         }
     }
 
+
+    /**
+     * Sets the cullers that will trim the filelist.
+     *
+     * @param cullers specifies the cullers to be invoked on a scan
+     */
+    public void setCullers(Culler[] cullers) {
+        this.cullers = cullers;
+    }
+
+
     /**
      * Sets the list of exclude patterns to use. All '/' and '\' characters 
      * are replaced by <code>File.separatorChar</code>, so the separator 
used 
@@ -752,7 +783,8 @@
 
     /**
      * Scans the base directory for files which match at least one include
-     * pattern and don't match any exclude patterns.
+     * pattern and don't match any exclude patterns. If there are cullers,
+     * then the files must pass muster there, as well.
      *
      * @exception IllegalStateException if the base directory was set 
      *            incorrectly (i.e. if it is <code>null</code>, doesn't 
exist,
@@ -783,13 +815,20 @@
         filesIncluded    = new Vector();
         filesNotIncluded = new Vector();
         filesExcluded    = new Vector();
+        filesCulled      = new Vector();
         dirsIncluded     = new Vector();
         dirsNotIncluded  = new Vector();
         dirsExcluded     = new Vector();
+        dirsCulled       = new Vector();
 
         if (isIncluded("")) {
             if (!isExcluded("")) {
+                if (!isCulled("",basedir)) {
                 dirsIncluded.addElement("");
+                }
+              else {
+                    dirsCulled.addElement("");
+                }
             } else {
                 dirsExcluded.addElement("");
             }
@@ -838,8 +877,8 @@
     /**
      * Scans the given directory for files and directories. Found files and
      * directories are placed in their respective collections, based on the
-     * matching of includes and excludes. When a directory is found, it is
-     * scanned recursively.
+     * matching of includes, excludes, and cullers. When a directory is
+     * found, it is scanned recursively.
      *
      * @param dir   The directory to scan. Must not be <code>null</code>.
      * @param vpath The path relative to the base directory (needed to 
@@ -876,12 +915,21 @@
             if (file.isDirectory()) {
                 if (isIncluded(name)) {
                     if (!isExcluded(name)) {
+                        if (!isCulled(name,file)) {
                         dirsIncluded.addElement(name);
                         if (fast) {
                             scandir(file, name+File.separator, fast);
                         }
                     } else {
                         everythingIncluded = false;
+                            dirsCulled.addElement(name);
+                            if (fast && couldHoldIncluded(name)) {
+                                scandir(file, name+File.separator, fast);
+                            }
+                        }
+
+                    } else {
+                        everythingIncluded = false;
                         dirsExcluded.addElement(name);
                         if (fast && couldHoldIncluded(name)) {
                             scandir(file, name+File.separator, fast);
@@ -900,9 +948,14 @@
             } else if (file.isFile()) {
                 if (isIncluded(name)) {
                     if (!isExcluded(name)) {
+                        if (!isCulled(name,file)) {
                         filesIncluded.addElement(name);
                     } else {
                         everythingIncluded = false;
+                            filesCulled.addElement(name);
+                        }
+                    } else {
+                        everythingIncluded = false;
                         filesExcluded.addElement(name);
                     }
                 } else {
@@ -965,6 +1018,24 @@
     }
 
     /**
+     * Tests whether a name should be culled.
+     *
+     * @param name the name to check for culling
+     * @return <code>true</code> when at least one culler says that the name
+     *         should be culled, <code>false</code> otherwise.
+     */
+    protected boolean isCulled(String name, File file) {
+        if (cullers != null) {
+            for (int i = 0; i < cullers.length; i++) {
+                if (cullers[i].checkForCull(name, file)) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
+    /**
      * Returns the names of the files which matched at least one of the 
      * include patterns and none of the exclude patterns.
      * The names are relative to the base directory.
@@ -1023,6 +1094,25 @@
     }
 
     /**
+     * Returns the names of the files which were culled. The names
+     * are relative to the base directory. This involves performing
+     * a slow scan if one has not already been completed.
+     *
+     * @return the names of the files which were culled.
+     *
+     * @see #slowScan
+     */
+    public String[] getCulledFiles() {
+        slowScan();
+        int count = filesCulled.size();
+        String[] files = new String[count];
+        for (int i = 0; i < count; i++) {
+            files[i] = (String)filesCulled.elementAt(i);
+        }
+        return files;
+    }
+
+    /**
      * Returns the names of the directories which matched at least one of 
the 
      * include patterns and none of the exclude patterns.
      * The names are relative to the base directory.
@@ -1076,6 +1166,25 @@
         String[] directories = new String[count];
         for (int i = 0; i < count; i++) {
             directories[i] = (String)dirsExcluded.elementAt(i);
+        }
+        return directories;
+    }
+
+    /**
+     * Returns the names of the directories which were culled. The names
+     * are relative to the base directory. This involves performing a
+     * slow scan if one has not already been completed.
+     *
+     * @return the names of the directories which were culled.
+     *
+     * @see #slowScan
+     */
+    public String[] getCulledDirectories() {
+        slowScan();
+        int count = dirsCulled.size();
+        String[] directories = new String[count];
+        for (int i = 0; i < count; i++) {
+            directories[i] = (String)dirsCulled.elementAt(i);
         }
         return directories;
     }
Index: types/FileSet.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/types/FileSet.java,v
retrieving revision 1.22
diff -u -b -r1.22 FileSet.java
--- types/FileSet.java	26 Jan 2002 20:16:22 -0000	1.22
+++ types/FileSet.java	24 Feb 2002 20:22:35 -0000
@@ -58,6 +58,8 @@
 import org.apache.tools.ant.FileScanner;
 import org.apache.tools.ant.DirectoryScanner;
 import org.apache.tools.ant.Project;
+import org.apache.tools.ant.types.cullers.Culler;
+import org.apache.tools.ant.types.cullers.CullerScanner;
 
 import java.io.File;
 import java.util.Stack;
@@ -180,6 +182,26 @@
     }
     
     /**
+     * add a culler date entry to the default PatternSet
+     */
+    public Culler createDatecull() {
+        if (isReference()) {
+            throw noChildrenAllowed();
+        }
+        return defaultPatterns.createDatecull();
+    }
+
+    /**
+     * add a culler size entry to the default PatternSet
+     */
+    public Culler createSizecull() {
+        if (isReference()) {
+            throw noChildrenAllowed();
+        }
+        return defaultPatterns.createSizecull();
+    }
+
+    /**
      * Sets the set of include patterns. Patterns may be separated by a comma
      * or a space.
      *
@@ -301,6 +323,19 @@
         
         ds.setIncludes(defaultPatterns.getIncludePatterns(p));
         ds.setExcludes(defaultPatterns.getExcludePatterns(p));
+        if (ds instanceof CullerScanner) {
+            CullerScanner cs = (CullerScanner)ds;
+            Culler[] cullers = defaultPatterns.getCullers(p);
+            for (int i = 0; i < cullers.length; i++) {
+                cullers[i].setBasedir(dir);
+                cullers[i].setProject(p);
+                String msg = cullers[i].checkForError();
+                if (msg != null) {
+                    throw new BuildException(msg);
+                }
+            }
+            cs.setCullers(cullers);
+        }
         if (useDefaultExcludes) {
           ds.addDefaultExcludes();
         }
Index: types/PatternSet.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/types/PatternSet.java,v
retrieving revision 1.19
diff -u -b -r1.19 PatternSet.java
--- types/PatternSet.java	26 Jan 2002 20:16:22 -0000	1.19
+++ types/PatternSet.java	24 Feb 2002 20:22:35 -0000
@@ -58,6 +58,8 @@
 
 import org.apache.tools.ant.BuildException;
 
+import org.apache.tools.ant.types.cullers.*;
+
 import java.io.File;
 import java.io.BufferedReader;
 import java.io.FileReader;
@@ -84,6 +86,7 @@
     private Vector excludeList = new Vector();
     private Vector includesFileList = new Vector();
     private Vector excludesFileList = new Vector();
+    private Vector cullersList = new Vector();
 
     /**
      * inner class to hold a name on list.  "If" and "Unless" attributes
@@ -205,6 +208,31 @@
     }
 
     /**
+     * add a culler date entry on the culler list
+     */
+    public Culler createDatecull() {
+        if (isReference()) {
+            throw noChildrenAllowed();
+        }
+        Culler culler = new DateCuller();
+        cullersList.addElement(culler);
+        return culler;
+    }
+
+    /**
+     * add a culler size entry on the culler list
+     */
+    public Culler createSizecull() {
+        if (isReference()) {
+            throw noChildrenAllowed();
+        }
+
+        Culler culler = new SizeCuller();
+        cullersList.addElement(culler);
+        return culler;
+    }
+
+    /**
      * Sets the set of include patterns. Patterns may be separated by a comma
      * or a space.
      *
@@ -332,6 +360,14 @@
                 createExclude().setName(excl[i]);
             }
         }
+
+        /* Does this culler need to be cloned first? */
+        Culler[] cullers = other.getCullers(p);
+        if (cullers != null) {
+            for (int i=0; i<cullers.length; i++) {
+                cullersList.addElement(cullers[i]);
+            }
+        }
     }
 
     /**
@@ -347,7 +383,7 @@
     }
 
     /**
-     * Returns the filtered include patterns.
+     * Returns the filtered exclude patterns.
      */
     public String[] getExcludePatterns(Project p) {
         if (isReference()) {
@@ -359,6 +395,19 @@
     }
 
     /**
+     * Returns the set of cullers as an array.
+     */
+    public Culler[] getCullers(Project p) {
+        if (isReference()) {
+            return getRef(p).getCullers(p);
+        } else {
+            Culler[] result = new Culler[cullersList.size()];
+            cullersList.copyInto(result);
+            return result;
+        }
+    }
+
+    /**
      * helper for FileSet.
      */
     boolean hasPatterns() {
@@ -409,7 +458,7 @@
     }
         
     /**
-     * Read includesfile ot excludesfile if not already done so.
+     * Read includesfile and excludesfile contents if not already done so.
      */
     private void readFiles(Project p) {
         if (includesFileList.size() > 0) {
@@ -449,10 +498,22 @@
         }
     }
 
-    public String toString()
-    {
-        return "patternSet{ includes: " + includeList + 
-            " excludes: " + excludeList + " }";
+    public String toString() {
+        StringBuffer buf = new StringBuffer("patternSet{ includes: " +
+            includeList + " excludes: " + excludeList);
+        Enumeration e = cullersList.elements();
+        if (e.hasMoreElements()) {
+            buf.append("cullers: ");
+        }
+        while(e.hasMoreElements()) {
+            buf.append(e.nextElement().toString());
+            if (e.hasMoreElements()) {
+                buf.append(", ");
+            }
+        }
+        buf.append(" }");
+
+        return buf.toString();
     }
 
 }

Mime
View raw message