ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hi...@apache.org
Subject svn commit: r1035335 - in /ant/core/trunk/src/main/org/apache/tools/ant: ProjectHelper.java helper/ProjectHelper2.java taskdefs/BindTargets.java
Date Mon, 15 Nov 2010 16:09:07 GMT
Author: hibou
Date: Mon Nov 15 16:09:07 2010
New Revision: 1035335

URL: http://svn.apache.org/viewvc?rev=1035335&view=rev
Log:
Make an enum like class for the values of the missing extension point behavior

Modified:
    ant/core/trunk/src/main/org/apache/tools/ant/ProjectHelper.java
    ant/core/trunk/src/main/org/apache/tools/ant/helper/ProjectHelper2.java
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/BindTargets.java

Modified: ant/core/trunk/src/main/org/apache/tools/ant/ProjectHelper.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/ProjectHelper.java?rev=1035335&r1=1035334&r2=1035335&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/ProjectHelper.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/ProjectHelper.java Mon Nov 15 16:09:07 2010
@@ -83,21 +83,57 @@ public class ProjectHelper {
     }
 
     /**
-     * Possible value for target's onMissingExtensionPoint attribute:
-     * fail if the extension-point is not defined.
-     */
-    public static final String MISSING_EP_FAIL = "fail";
-    /**
-     * Possible value for target's onMissingExtensionPoint attribute:
-     * warn if the extension-point is not defined.
-     */
-    public static final String MISSING_EP_WARN = "warn";
-    /**
-     * Possible value for target's onMissingExtensionPoint attribute:
-     * ignore the extensionOf attribute if the extension-point is not
-     * defined.
+     * Possible value for target's onMissingExtensionPoint attribute. It determines how to
deal with
+     * targets that want to extend missing extension-points.
+     * <p>
+     * This class behaves like a Java 1.5 Enum class.
+     * 
+     * @since 1.8.2
      */
-    public static final String MISSING_EP_IGNORE = "ignore";
+    public final static class OnMissingExtensionPoint {
+
+        /** fail if the extension-point is not defined */
+        public static final OnMissingExtensionPoint FAIL = new OnMissingExtensionPoint(
+                "fail");
+
+        /** warn if the extension-point is not defined */
+        public static final OnMissingExtensionPoint WARN = new OnMissingExtensionPoint(
+                "warn");
+
+        /** ignore the extensionOf attribute if the extension-point is not defined */
+        public static final OnMissingExtensionPoint IGNORE = new OnMissingExtensionPoint(
+                "ignore");
+
+        private static final OnMissingExtensionPoint[] values = new OnMissingExtensionPoint[]
{
+                                FAIL, WARN, IGNORE };
+
+        private final String name;
+
+        private OnMissingExtensionPoint(String name) {
+            this.name = name;
+        }
+
+        public String name() {
+            return name;
+        }
+
+        public String toString() {
+            return name;
+        }
+
+        public static OnMissingExtensionPoint valueOf(String name) {
+            if (name == null) {
+                throw new NullPointerException();
+            }
+            for (int i = 0; i < values.length; i++) {
+                if (name.equals(values[i].name())) {
+                    return values[i];
+                }
+            }
+            throw new IllegalArgumentException(
+                    "Unknown onMissingExtensionPoint " + name);
+        }
+    }
 
     /** Default constructor */
     public ProjectHelper() {
@@ -127,9 +163,8 @@ public class ProjectHelper {
      *
      * @return a list of three element string arrays where the first
      * element is the name of the extensionpoint, the second the name
-     * of the target and the third one of the MISSINS_EP constants
-     * defined inside this class - it determines how to deal with
-     * targets that want to extend missing extension-points.
+     * of the target and the third the name of the enum like class
+     * {@link OnMissingExtensionPoint}.
      */
     public List getExtensionStack() {
         return extensionStack;

Modified: ant/core/trunk/src/main/org/apache/tools/ant/helper/ProjectHelper2.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/helper/ProjectHelper2.java?rev=1035335&r1=1035334&r2=1035335&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/helper/ProjectHelper2.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/helper/ProjectHelper2.java Mon Nov 15 16:09:07
2010
@@ -184,15 +184,16 @@ public class ProjectHelper2 extends Proj
                 String[] extensionInfo = (String[]) i.next();
                 String tgName = extensionInfo[0];
                 String name = extensionInfo[1];
-                String missingBehaviour = extensionInfo[2];
+                OnMissingExtensionPoint missingBehaviour = OnMissingExtensionPoint
+                        .valueOf(extensionInfo[2]);
                 Hashtable projectTargets = project.getTargets();
                 if (!projectTargets.containsKey(tgName)) {
                     String message = "can't add target " + name
                         + " to extension-point " + tgName
                         + " because the extension-point is unknown.";
-                    if (missingBehaviour.equals(MISSING_EP_FAIL)) {
+                    if (missingBehaviour == OnMissingExtensionPoint.FAIL) {
                         throw new BuildException(message);
-                    } else if (missingBehaviour.equals(MISSING_EP_WARN)) {
+                    } else if (missingBehaviour == OnMissingExtensionPoint.WARN) {
                         Target target = (Target) projectTargets.get(name);
                         context.getProject().log(target,
                                                  "Warning: " + message,
@@ -914,7 +915,7 @@ public class ProjectHelper2 extends Proj
             String name = null;
             String depends = "";
             String extensionPoint = null;
-            String extensionPointMissing = null;
+            OnMissingExtensionPoint extensionPointMissing = null;
 
             Project project = context.getProject();
             Target target = "target".equals(tag)
@@ -951,7 +952,11 @@ public class ProjectHelper2 extends Proj
                 } else if (key.equals("extensionOf")) {
                     extensionPoint = value;
                 } else if (key.equals("onMissingExtensionPoint")) {
-                    extensionPointMissing = value;
+                    try {
+                        extensionPointMissing = OnMissingExtensionPoint.valueOf(value);
+                    } catch (IllegalArgumentException e) {
+                        throw new BuildException("Invalid onMissingExtensionPoint " + value);
+                    }
                 } else {
                     throw new SAXParseException("Unexpected attribute \"" + key + "\"", context
                                                 .getLocator());
@@ -1039,25 +1044,12 @@ public class ProjectHelper2 extends Proj
                         tgName = prefix + sep + tgName;
                     }
                     if (extensionPointMissing == null) {
-                        extensionPointMissing = MISSING_EP_FAIL;
-                    }
-                    if (extensionPointMissing.equals(MISSING_EP_FAIL) ||
-                        extensionPointMissing.equals(MISSING_EP_IGNORE) ||
-                        extensionPointMissing.equals(MISSING_EP_WARN)) {
-                        // defer extensionpoint resolution until the full
-                        // import stack has been processed
-                        helper.getExtensionStack().add(new String[] {
-                                tgName, name, extensionPointMissing
-                            });
-                    } else {
-                        throw new BuildException("onMissingExtensionPoint"
-                                                 + " attribute can only be '"
-                                                 + MISSING_EP_FAIL
-                                                 + "', '" + MISSING_EP_WARN
-                                                 + "' or '" + MISSING_EP_IGNORE
-                                                 + "'", 
-                                                 target.getLocation());
+                        extensionPointMissing = OnMissingExtensionPoint.FAIL;
                     }
+                    // defer extensionpoint resolution until the full
+                    // import stack has been processed
+                    helper.getExtensionStack().add(new String[] {
+                            tgName, name, extensionPointMissing.name() });
                 }
             }
         }

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/BindTargets.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/BindTargets.java?rev=1035335&r1=1035334&r2=1035335&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/BindTargets.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/BindTargets.java Mon Nov 15 16:09:07
2010
@@ -23,6 +23,7 @@ import java.util.List;
 
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.ProjectHelper;
+import org.apache.tools.ant.ProjectHelper.OnMissingExtensionPoint;
 import org.apache.tools.ant.Task;
 
 /**
@@ -34,13 +35,21 @@ public class BindTargets extends Task {
 
     private List/* <String> */targets = new ArrayList();
 
-    private String onMissingExtensionPoint;
+    private OnMissingExtensionPoint onMissingExtensionPoint;
 
     public void setExtensionPoint(String extensionPoint) {
         this.extensionPoint = extensionPoint;
     }
 
     public void setOnMissingExtensionPoint(String onMissingExtensionPoint) {
+        try {
+            this.onMissingExtensionPoint = OnMissingExtensionPoint.valueOf(onMissingExtensionPoint);
+        } catch (IllegalArgumentException e) {
+            throw new BuildException("Invalid onMissingExtensionPoint: " + onMissingExtensionPoint);
+        }
+    }
+
+    public void setOnMissingExtensionPoint(OnMissingExtensionPoint onMissingExtensionPoint)
{
         this.onMissingExtensionPoint = onMissingExtensionPoint;
     }
 
@@ -66,18 +75,7 @@ public class BindTargets extends Task {
         }
 
         if (onMissingExtensionPoint == null) {
-            onMissingExtensionPoint = ProjectHelper.MISSING_EP_FAIL;
-        }
-        if (!onMissingExtensionPoint.equals(ProjectHelper.MISSING_EP_FAIL)
-                && !onMissingExtensionPoint
-                        .equals(ProjectHelper.MISSING_EP_IGNORE)
-                && !onMissingExtensionPoint
-                        .equals(ProjectHelper.MISSING_EP_WARN)) {
-            throw new BuildException("onMissingExtensionPoint"
-                    + " attribute can only be '"
-                    + ProjectHelper.MISSING_EP_FAIL + "', '"
-                    + ProjectHelper.MISSING_EP_WARN + "' or '"
-                    + ProjectHelper.MISSING_EP_IGNORE + "'", getLocation());
+            onMissingExtensionPoint = OnMissingExtensionPoint.FAIL;
         }
         ProjectHelper helper = (ProjectHelper) getProject().getReference(
                 ProjectHelper.PROJECTHELPER_REFERENCE);
@@ -86,7 +84,7 @@ public class BindTargets extends Task {
         while (itTarget.hasNext()) {
             helper.getExtensionStack().add(
                     new String[] { extensionPoint, (String) itTarget.next(),
-                                            onMissingExtensionPoint });
+                                            onMissingExtensionPoint.name() });
         }
 
     }



Mime
View raw message