sorry for former stupid code snippets.
the patch against the latest sources and a doc patch are attached.

example:

<project name="test" default="doit">
        <target name="doit" depends="test"/>
        <target name="call">
                <antcall target="test"/>
        </target>
        <target name="test" internal="true">
                <echo message="EXECUTED"/>
        </target>
</project>                                                                    
  

target 'doit' and 'call' can be executed from outside but not the target 
'test'.


Claas Thiele


diff -ur -x *~ src/main/org/apache/tools/ant/Main.java ../jakarta-ant/src/main/org/apache/tools/ant/Main.java
--- src/main/org/apache/tools/ant/Main.java	Fri Feb 23 18:35:32 2001
+++ ../jakarta-ant/src/main/org/apache/tools/ant/Main.java	Fri Feb 23 21:04:08 2001
@@ -417,7 +417,11 @@
             if (projectHelp) {
                 printTargets(project);
             } else {
-                // actually do some work
+		String internalTargetName = allPublicTargets(project, targets);
+		if (internalTargetName != null) {
+			throw new BuildException("Target " + internalTargetName + " cannot be executed from outside");
+		}
+		// actually do some work
                 project.executeTargets(targets);
             }
         }
@@ -607,4 +611,26 @@
         }
         System.out.println(msg.toString());
     }
+
+    /**
+     * Checks if all targets to be executed from outside not being internal
+     * @param project project holds the targets to be checked
+     * @param tNames a vector of Strings holding names of targets to be executed
+     * @return first target name that is internal but should be executed from outside
+     */	
+    private static String allPublicTargets(Project project, Vector tNames) {
+	Target currentTarget;
+	Enumeration targetNames = tNames.elements();
+	Hashtable targets = project.getTargets();
+	while (targetNames.hasMoreElements()) {
+	    String name = (String)targetNames.nextElement();
+	    if (name != null) {
+		currentTarget = (Target)targets.get(name);
+		if (currentTarget != null) {
+		    if (currentTarget.isInternal()) return currentTarget.getName();
+		}
+	    }
+        }
+	return null;
+    }	
 }
diff -ur -x *~ src/main/org/apache/tools/ant/ProjectHelper.java ../jakarta-ant/src/main/org/apache/tools/ant/ProjectHelper.java
--- src/main/org/apache/tools/ant/ProjectHelper.java	Fri Feb 23 18:35:32 2001
+++ ../jakarta-ant/src/main/org/apache/tools/ant/ProjectHelper.java	Fri Feb 23 21:44:19 2001
@@ -375,6 +375,7 @@
             String unlessCond = null;
             String id = null;
             String description = null;
+	    String internal = null;
 
             for (int i = 0; i < attrs.getLength(); i++) {
                 String key = attrs.getName(i);
@@ -392,6 +393,8 @@
                     id = value;
                 } else if (key.equals("description")) {
                     description = value;
+		} else if (key.equals("internal")) {
+		    internal = value;
                 } else {
                     throw new SAXParseException("Unexpected attribute \"" + key + "\"", locator);
                 }
@@ -406,6 +409,7 @@
             target.setIf(ifCond);
             target.setUnless(unlessCond);
             target.setDescription(description);
+	    target.setInternal(internal);
             project.addTarget(name, target);
 
             if (id != null && !id.equals(""))
diff -ur -x *~ src/main/org/apache/tools/ant/Target.java ../jakarta-ant/src/main/org/apache/tools/ant/Target.java
--- src/main/org/apache/tools/ant/Target.java	Fri Feb 23 18:35:32 2001
+++ ../jakarta-ant/src/main/org/apache/tools/ant/Target.java	Fri Feb 23 21:06:38 2001
@@ -71,6 +71,7 @@
     private Vector tasks = new Vector(5);
     private Project project;
     private String description = null;
+    private boolean internal = false;
 
     public void setProject(Project project) {
         this.project = project;
@@ -133,6 +134,12 @@
         this.description = description;
     }
 
+    public void setInternal(String internal) {
+	if (internal != null) {
+	    this.internal = project.toBoolean(internal);
+	}
+    }
+	
     public String getDescription() {
         return description;
     }
@@ -188,5 +195,13 @@
     private boolean testUnlessCondition() {
         return "".equals(unlessCondition) 
             || project.getProperty(unlessCondition) == null;
+    }
+    
+    /*
+     * Returns the executable state of this target
+     * @return the executable state of this target
+     */
+    public boolean isInternal() {
+	return internal;
     }
 }
diff -ur -x *~ docs/manual/using.html ../jakarta-ant/docs/manual/using.html
--- docs/manual/using.html	Fri Feb 23 18:35:25 2001
+++ ../jakarta-ant/docs/manual/using.html	Fri Feb 23 21:59:08 2001
@@ -130,6 +130,11 @@
     <td valign="top">a short description of this target's function.</td>
     <td align="center" valign="top">No</td>
   </tr>
+  <tr>
+    <td valign="top">internal</td>
+    <td valign="top">if set true target cannot be executed from outside. Only internal use via depends or antcall is possible. Default value is false.</td>
+    <td align="center" valign="top">No</td>
+  </tr>
 </table>
 
 <a name="tasks">

Reply via email to