I either could not get the ones I found working, or they were just too 
complicated.  Here is the one I wrote which is not a for loop, but can become 
one by maintaining your own index property and setting the if value to the loop 
maximum.


import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.MacroDef;
import org.apache.tools.ant.taskdefs.MacroInstance;

public class WhileTask extends Task {
  protected String _property = null;
  protected String _ifVal = null;
  protected String _unlessVal = null;
  protected boolean _keepgoing = false;
  protected MacroDef _macroDef = null;
  protected int _taskCount = 0;
  protected int _errorCount = 0;

  public WhileTask()
  {
  }

  public void setKeepgoing(boolean keepgoing)
  {
    _keepgoing = keepgoing;
  }

  public void setProperty(String property)
  {
    _property = property;
  }

  public void setIf(String ifVal)
  {
    _ifVal = ifVal;
  }

  public void setUnless(String unlessVal)
  {
    _unlessVal = unlessVal;
  }

  public Object createSequential()
  {
    _macroDef = new MacroDef();
    _macroDef.setProject(getProject());
    return(_macroDef.createSequential());
  }

  public void execute()
  {
    if(_property == null)
      throw new BuildException("You must supply a property name to check 
terminating conditions on");
    if((_ifVal == null) && (_unlessVal == null))
      throw new BuildException("You must supply either an if value or unless 
value for termination of the while loop");
    if(_macroDef == null)
      throw new BuildException("You must supply an embedded sequential to 
perform");
    doTheTasks();
  }

  protected void doSequentialIteration()
  {
    MacroInstance instance = new MacroInstance();
    instance.setProject(getProject());
    instance.setOwningTarget(getOwningTarget());
    instance.setMacroDef(_macroDef);
    instance.execute();
  }

  protected void doTask()
  {
    try {
      _taskCount++;
      doSequentialIteration();
    } catch(BuildException be) {
      if(_keepgoing) {
        _errorCount++;
      } else {
        throw be;
      }
    }
  }

  protected void doTheTasks()
  {
    _errorCount = 0;
    _taskCount = 0;
    Project proj = getProject();
    if(!proj.getProperties().containsKey(_property))
      throw new BuildException("Property: " + _property + " does not exist");
    String propVal = proj.getProperty(_property);
    while(((_ifVal != null) && (propVal.equals(_ifVal))) || ((_unlessVal != 
null) && (!propVal.equals(_unlessVal)))) {
      doTask();
      propVal = proj.getProperty(_property);
    }
    if((_keepgoing) && (_errorCount != 0))
      throw new BuildException("Keepgoing execution: " + _errorCount + " of " + 
_taskCount + " iterations failed.");
  }
}


Shawn Castrianni

----------------------------------------------------------------------
This e-mail, including any attached files, may contain confidential and 
privileged information for the sole use of the intended recipient.  Any review, 
use, distribution, or disclosure by others is strictly prohibited.  If you are 
not the intended recipient (or authorized to receive information for the 
intended recipient), please contact the sender by reply e-mail and delete all 
copies of this message.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@ant.apache.org
For additional commands, e-mail: user-h...@ant.apache.org

Reply via email to