Hi,

The needed modifications to support regular expressions in the target
dependencies were simple. So this is a first patch for the ANT_16_BRANCH
which adds this feature.

For more details :
http://wiki.tryphon.org/JavaWiki/AntStarDependencies

All the comments are welcome ;o)
-- 
Alban - [EMAIL PROTECTED]
http://www.tryphon.org/~alban
Index: src/main/org/apache/tools/ant/Project.java
===================================================================
RCS file: /home/cvspublic/ant/src/main/org/apache/tools/ant/Project.java,v
retrieving revision 1.154.2.1
diff -c -b -r1.154.2.1 Project.java
*** src/main/org/apache/tools/ant/Project.java	11 Dec 2003 12:16:46 -0000	1.154.2.1
--- src/main/org/apache/tools/ant/Project.java	13 Dec 2003 10:07:06 -0000
***************
*** 63,68 ****
--- 63,71 ----
  import java.util.Enumeration;
  import java.util.Hashtable;
  import java.util.Iterator;
+ import java.util.List;
+ import java.util.LinkedList;
+ import java.util.Collections;
  import java.util.Properties;
  import java.util.Stack;
  import java.util.Vector;
***************
*** 77,82 ****
--- 80,87 ----
  import org.apache.tools.ant.util.FileUtils;
  import org.apache.tools.ant.util.JavaEnvUtils;
  import org.apache.tools.ant.util.StringUtils;
+ import org.apache.tools.ant.util.regexp.RegexpMatcher;
+ import org.apache.tools.ant.util.regexp.RegexpMatcherFactory;
  
  
  /**
***************
*** 1044,1049 ****
--- 1049,1101 ----
       */
      public Hashtable getTargets() {
          return targets;
+     }
+     
+     private RegexpMatcherFactory targetMatcherFactory = new RegexpMatcherFactory();
+ 
+     public List findTargetNames(String expression) {
+         RegexpMatcher matcher = null;
+ 
+         if (targetMatcherFactory != null) {
+             try {
+                 matcher = targetMatcherFactory.newRegexpMatcher(this);
+             } catch (BuildException e) {
+                 String msg = "no supported regular expression matcher found,"
+                     + "the star dependencies support is disabled";
+                 log(msg, MSG_VERBOSE);     
+                 targetMatcherFactory = null;
+             }
+         }
+ 
+         // if the star dependencies is disabled, return the given expression has the target name
+         if (matcher == null) {
+             return Collections.singletonList(expression);
+         }
+ 
+         if (! expression.startsWith("^")) {
+             expression = "^" + expression;
+         }
+         if (! expression.endsWith("$")) {
+             expression = expression + "$";
+         }
+ 
+         matcher.setPattern(expression);
+ 
+         List foundTargetNames = new LinkedList();
+ 
+         for (Iterator iter=targets.keySet().iterator(); iter.hasNext(); ) {
+             String targetName = (String) iter.next();
+ 
+             if (matcher.matches(targetName)) {
+                 foundTargetNames.add(targetName);
+             }
+         }
+ 
+         Collections.sort(foundTargetNames);
+ 
+         log("found targets " + foundTargetNames + " for '" + expression + "'", MSG_DEBUG);
+ 
+         return foundTargetNames;
      }
  
      /**
Index: src/main/org/apache/tools/ant/Target.java
===================================================================
RCS file: /home/cvspublic/ant/src/main/org/apache/tools/ant/Target.java,v
retrieving revision 1.46
diff -c -b -r1.46 Target.java
*** src/main/org/apache/tools/ant/Target.java	17 Sep 2003 14:23:27 -0000	1.46
--- src/main/org/apache/tools/ant/Target.java	13 Dec 2003 10:07:07 -0000
***************
*** 59,67 ****
--- 59,69 ----
  import java.util.Enumeration;
  import java.util.Iterator;
  import java.util.List;
+ import java.util.LinkedList;
  import java.util.StringTokenizer;
  
  import org.apache.tools.ant.util.CollectionUtils;
+ import org.apache.tools.ant.util.StringUtils;
  
  /**
   * Class to implement a target object with required parameters.
***************
*** 78,83 ****
--- 80,87 ----
      private String unlessCondition = "";
      /** List of targets this target is dependent on. */
      private List dependencies = null;
+     /** false if the dependencies are not expanded for the moment */
+     private boolean isDependenciesExpanded = false;
      /** Children of this target (tasks and data types). */
      private List children = new ArrayList();
  
***************
*** 217,222 ****
--- 221,245 ----
          dependencies.add(dependency);
      }
  
+     private List expandDependencies() {
+         List targetNames = new LinkedList();
+ 
+         for (Iterator iter=dependencies.iterator(); iter.hasNext(); ) {
+             String dependency = (String) iter.next();
+ 
+             // simply some common expressions
+             if (dependency.endsWith(".*")) { // expressions ended with .*
+                 dependency = dependency.substring(0, dependency.length() - 2) + "\\.[^.]*$";
+             } else if (dependency.indexOf(".*.") > -1) { // expressions with .*.
+                 dependency = StringUtils.replace(dependency, ".*.",  "\\.[^.]*\\.");
+             }
+ 
+             targetNames.addAll(getProject().findTargetNames(dependency));
+         }
+ 
+         return targetNames;
+     }
+ 
      /**
       * Returns an enumeration of the dependencies of this target.
       *
***************
*** 224,229 ****
--- 247,257 ----
       */
      public Enumeration getDependencies() {
          if (dependencies != null) {
+             if (!isDependenciesExpanded) {
+                 dependencies = expandDependencies();
+                 isDependenciesExpanded = true;
+             }
+ 
              return Collections.enumeration(dependencies);
          } else {
              return new CollectionUtils.EmptyEnumeration();

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to