diff options
author | peterkittreilly <[email protected]> | 2006-10-10 21:11:39 +0000 |
---|---|---|
committer | peterkittreilly <[email protected]> | 2006-10-10 21:11:39 +0000 |
commit | c513cca898a61058886426192538446322a4032a (patch) | |
tree | b3763e30006ac954e54a457ba5b59ee4650d94d5 | |
parent | 549e6a7ebcfd11570cecb48b81557446ca5a18ae (diff) |
add begin/step/end attributes to <for>
git-svn-id: file:///home/sven/projects/JOGL/temp/ant-contrib/svn/ant-contrib-code/trunk/ant-contrib@52 32d7a393-a5a9-423c-abd3-5d954feb1f2f
-rw-r--r-- | docs/manual/tasks/for.html | 52 | ||||
-rw-r--r-- | 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 @@ <td valign="top">The list of values to process, with the delimiter character, indicated by the "delimiter" attribute, separating each value.</td> - <td align="center" valign="top">Yes, unless a nested path - has been specified.</td> + <td align="center" valign="top" rowspan="2">Yes, one of these needs to + be set, unless a nested path + has been specified.</td> + </tr> + <tr> + <td valign="top">end</td> + <td valign="top"> + 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. + </td> </tr> <tr> <td valign="top">param</td> @@ -98,6 +107,22 @@ </td> <td align="center" valign="top">No. Defaults to false.</td> </tr> + <tr> + <td valign="top">begin</td> + <td valign="top"> + Sets the starting index value. This in only used + if the "end" attribute is set. + </td> + <td align="center" valign="top">No. Defaults to "1".</td> + </tr> + <tr> + <td valign="top">step</td> + <td valign="top"> + Sets the index increment step. + This in only used if the "end" attribute is set. + </td> + <td align="center" valign="top">No. Defaults to "1".</td> + </tr> </table> <h2>Parameters specified as nested elements</h2> @@ -190,8 +215,29 @@ </ac:sequential> </ac:for> </pre> + + <p> + The following example loops from one to ten. + </p> + <pre> + <ac:for param="i" end="10"> + <sequential> + <echo>i is @{i}</echo> + </sequential> + </ac:for> + </pre> + <p> + The following example counts down from 10 to 0 in steps of -2. + </p> + <pre> + <ac:for param="i" begin="10" step="-2" end="0"> + <sequential> + <echo>i is @{i}</echo> + </sequential> + </ac:for> + </pre> <hr> - <p align="center">Copyright © 2003-2005 Ant-Contrib Project. All + <p align="center">Copyright © 2003-2006 Ant-Contrib Project. All rights Reserved.</p> </body> 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 <code>For</code> instance. - * This checks if the ant version is correct to run this task. */ public ForTask() { } @@ -173,6 +178,31 @@ public class ForTask extends Task { } /** + * 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 * if there are ok, it calls doTheTasks() @@ -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)) { |