From c513cca898a61058886426192538446322a4032a Mon Sep 17 00:00:00 2001 From: peterkittreilly Date: Tue, 10 Oct 2006 21:11:39 +0000 Subject: add begin/step/end attributes to git-svn-id: file:///home/sven/projects/JOGL/temp/ant-contrib/svn/ant-contrib-code/trunk/ant-contrib@52 32d7a393-a5a9-423c-abd3-5d954feb1f2f --- docs/manual/tasks/for.html | 52 ++++++++++- src/java/net/sf/antcontrib/logic/ForTask.java | 126 ++++++++++++++++---------- 2 files changed, 126 insertions(+), 52 deletions(-) diff --git a/docs/manual/tasks/for.html b/docs/manual/tasks/for.html index ecf23fe..b76789c 100644 --- a/docs/manual/tasks/for.html +++ b/docs/manual/tasks/for.html @@ -41,8 +41,17 @@ The list of values to process, with the delimiter character, indicated by the "delimiter" attribute, separating each value. - Yes, unless a nested path - has been specified. + Yes, one of these needs to + be set, unless a nested path + has been specified. + + + end + + Sets the ending index value. If this attribute is + set, the <for> task will loop from "start" (default 1) + to "end", using "step" (default 1) increments. + param @@ -98,6 +107,22 @@ No. Defaults to false. + + begin + + Sets the starting index value. This in only used + if the "end" attribute is set. + + No. Defaults to "1". + + + step + + Sets the index increment step. + This in only used if the "end" attribute is set. + + No. Defaults to "1". +

Parameters specified as nested elements

@@ -190,8 +215,29 @@ </ac:sequential> </ac:for> + +

+ The following example loops from one to ten. +

+
+    <ac:for param="i" end="10">
+      <sequential>
+        <echo>i is @{i}</echo>
+      </sequential>
+    </ac:for>
+    
+

+ The following example counts down from 10 to 0 in steps of -2. +

+
+    <ac:for param="i" begin="10" step="-2" end="0">
+      <sequential>
+        <echo>i is @{i}</echo>
+      </sequential>
+    </ac:for>
+    

-

Copyright © 2003-2005 Ant-Contrib Project. All +

Copyright © 2003-2006 Ant-Contrib Project. All rights Reserved.

diff --git a/src/java/net/sf/antcontrib/logic/ForTask.java b/src/java/net/sf/antcontrib/logic/ForTask.java index 59b8c09..4290c19 100644 --- a/src/java/net/sf/antcontrib/logic/ForTask.java +++ b/src/java/net/sf/antcontrib/logic/ForTask.java @@ -53,10 +53,15 @@ public class ForTask extends Task { private boolean parallel = false; private Integer threadCount; private Parallel parallelTasks; + private int begin = 0; + private Integer end = null; + private int step = 1; + + private int taskCount = 0; + private int errorCount = 0; /** * Creates a new For instance. - * This checks if the ant version is correct to run this task. */ public ForTask() { } @@ -172,6 +177,31 @@ public class ForTask extends Task { return macroDef.createSequential(); } + /** + * Set begin attribute. + * @param begin the value to use. + */ + public void setBegin(int begin) { + this.begin = begin; + } + + /** + * Set end attribute. + * @param end the value to use. + */ + public void setEnd(Integer end) { + this.end = end; + } + + /** + * Set step attribute. + * + */ + public void setStep(int step) { + this.step = step; + } + + /** * Run the for task. * This checks the attributes and nested elements, and @@ -186,9 +216,10 @@ public class ForTask extends Task { parallelTasks.setThreadCount(threadCount.intValue()); } } - if (list == null && currPath == null && hasIterators.size() == 0) { + if (list == null && currPath == null && hasIterators.size() == 0 + && end == null) { throw new BuildException( - "You must have a list or path to iterate through"); + "You must have a list or path or sequence to iterate through"); } if (param == null) { throw new BuildException( @@ -200,6 +231,16 @@ public class ForTask extends Task { "You must supply an embedded sequential " + "to perform"); } + if (end != null) { + int iEnd = end.intValue(); + if (step == 0) { + throw new BuildException("step cannot be 0"); + } else if (iEnd > begin && step < 0) { + throw new BuildException("end > begin, step needs to be > 0"); + } else if (iEnd <= begin && step > 0) { + throw new BuildException("end <= begin, step needs to be < 0"); + } + } doTheTasks(); if (parallel) { parallelTasks.perform(); @@ -221,9 +262,23 @@ public class ForTask extends Task { } } + private void doToken(String tok) { + try { + taskCount++; + doSequentialIteration(tok); + } catch (BuildException bx) { + if (keepgoing) { + log(tok + ": " + bx.getMessage(), Project.MSG_ERR); + errorCount++; + } else { + throw bx; + } + } + } + private void doTheTasks() { - int errorCount = 0; - int taskCount = 0; + errorCount = 0; + taskCount = 0; // Create a macro attribute if (macroDef.getAttributes().isEmpty()) { @@ -241,25 +296,24 @@ public class ForTask extends Task { if (trim) { tok = tok.trim(); } - try { - taskCount++; - doSequentialIteration(tok); - } catch (BuildException bx) { - if (keepgoing) { - log(tok + ": " + bx.getMessage(), Project.MSG_ERR); - errorCount++; - } else { - throw bx; - } - } + doToken(tok); } } - if (keepgoing && (errorCount != 0)) { - throw new BuildException( - "Keepgoing execution: " + errorCount - + " of " + taskCount + " iterations failed."); - } + // Take care of the begin/end/step attributes + if (end != null) { + int iEnd = end.intValue(); + if (step > 0) { + for (int i = begin; i < (iEnd + 1); i = i + step) { + doToken("" + i); + } + } else { + for (int i = begin; i > (iEnd - 1); i = i + step) { + doToken("" + i); + } + } + } + // Take Care of the path element String[] pathElements = new String[0]; if (currPath != null) { @@ -267,40 +321,14 @@ public class ForTask extends Task { } for (int i = 0; i < pathElements.length; i++) { File nextFile = new File(pathElements[i]); - try { - taskCount++; - doSequentialIteration(nextFile.getAbsolutePath()); - } catch (BuildException bx) { - if (keepgoing) { - log(nextFile + ": " + bx.getMessage(), Project.MSG_ERR); - errorCount++; - } else { - throw bx; - } - } - } - if (keepgoing && (errorCount != 0)) { - throw new BuildException( - "Keepgoing execution: " + errorCount - + " of " + taskCount + " iterations failed."); + doToken(nextFile.getAbsolutePath()); } // Take care of iterators for (Iterator i = hasIterators.iterator(); i.hasNext();) { Iterator it = ((HasIterator) i.next()).iterator(); while (it.hasNext()) { - String s = it.next().toString(); - try { - taskCount++; - doSequentialIteration(s); - } catch (BuildException bx) { - if (keepgoing) { - log(s + ": " + bx.getMessage(), Project.MSG_ERR); - errorCount++; - } else { - throw bx; - } - } + doToken(it.next().toString()); } } if (keepgoing && (errorCount != 0)) { -- cgit v1.2.3