adammurdoch 2002/06/06 03:57:03
Modified: antlib/src/java/org/apache/antlib/core Resources.properties
antlib/src/test/org/apache/antlib
AbstractProjectTestCase.java
Added: antlib/src/java/org/apache/antlib/core ForEachTask.java
antlib/src/test/org/apache/antlib/core/test
ForEachTaskTestCase.java for-each.ant
Log:
Added a <for-each> task. It had to be done.
Revision Changes Path
1.6 +4 -1
jakarta-ant-myrmidon/antlib/src/java/org/apache/antlib/core/Resources.properties
Index: Resources.properties
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/antlib/src/java/org/apache/antlib/core/Resources.properties,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- Resources.properties 24 May 2002 04:02:02 -0000 1.5
+++ Resources.properties 6 Jun 2002 10:57:03 -0000 1.6
@@ -31,4 +31,7 @@
load-resource.no-resource.error=No resource specified to load properties
from.
load-resource.loading.notice=Loading properties from resource "{0}".
-load-resource.missing-resource.notice=Unable to find resource "{0}".
\ No newline at end of file
+load-resource.missing-resource.notice=Unable to find resource "{0}".
+
+for-each.no-propertry.error=No property name specified.
+for-each.no-list.error=No values specified.
\ No newline at end of file
1.1
jakarta-ant-myrmidon/antlib/src/java/org/apache/antlib/core/ForEachTask.java
Index: ForEachTask.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.antlib.core;
import org.apache.myrmidon.framework.AbstractContainerTask;
import org.apache.myrmidon.api.metadata.ModelElement;
import org.apache.myrmidon.api.TaskException;
import org.apache.avalon.excalibur.i18n.ResourceManager;
import org.apache.avalon.excalibur.i18n.Resources;
import java.util.ArrayList;
import java.util.StringTokenizer;
/**
* Executes a set of tasks for each value in a list.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
* @version $Revision: 1.1 $ $Date: 2002/06/06 10:57:03 $
*
* @ant.task name="for-each"
*/
public class ForEachTask
extends AbstractContainerTask
{
private static final Resources REZ =
ResourceManager.getPackageResources( ForEachTask.class );
private String m_list;
private String m_property;
private ArrayList m_tasks = new ArrayList();
/**
* The property to set on each iteration.
*/
public void setProperty( final String property )
{
m_property = property;
}
/**
* The list to iterate over.
*/
public void setList( final String list )
{
m_list = list;
}
/**
* A nested task to execute.
*/
public void add( final ModelElement model )
{
m_tasks.add( model );
}
/**
* Executes the task.
*/
public void execute()
throws TaskException
{
if( m_property == null )
{
final String message = REZ.getString(
"for-each.no-propertry.error" );
throw new TaskException( message );
}
if( m_list == null )
{
final String message = REZ.getString( "for-each.no-list.error" );
throw new TaskException( message );
}
final Object[] values = getValues();
for( int i = 0; i < values.length; i++ )
{
final Object value = values[ i ];
// TODO - should use a child property store here
getContext().setProperty( m_property, value );
executeTasks();
}
}
/**
* Executes the nested tasks.
*/
private void executeTasks() throws TaskException
{
final int count = m_tasks.size();
for( int i = 0; i < count; i++ )
{
final ModelElement taskModel = (ModelElement)m_tasks.get( i );
executeTask( taskModel );
}
}
/**
* Returns the list of value to iterate over.
* @return
*/
private Object[] getValues()
{
final StringTokenizer tokens = new StringTokenizer( m_list, "," );
final Object[] values = new Object[ tokens.countTokens() ];
for( int i = 0; i < values.length; i++ )
{
values[ i ] = tokens.nextToken().trim();
}
return values;
}
}
1.10 +1 -4
jakarta-ant-myrmidon/antlib/src/test/org/apache/antlib/AbstractProjectTestCase.java
Index: AbstractProjectTestCase.java
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/antlib/src/test/org/apache/antlib/AbstractProjectTestCase.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- AbstractProjectTestCase.java 5 Jun 2002 07:58:22 -0000 1.9
+++ AbstractProjectTestCase.java 6 Jun 2002 10:57:03 -0000 1.10
@@ -13,10 +13,7 @@
* A base class for test cases which need to execute projects or tasks.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
- * @version $Revision: 1.9 $ $Date: 2002/06/05 07:58:22 $
- *
- * @todo - use the real project builder, rather than the simplified one
- * from framework.
+ * @version $Revision: 1.10 $ $Date: 2002/06/06 10:57:03 $
*/
public class AbstractProjectTestCase
extends AbstractTaskTestCase
1.1
jakarta-ant-myrmidon/antlib/src/test/org/apache/antlib/core/test/ForEachTaskTestCase.java
Index: ForEachTaskTestCase.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.antlib.core.test;
import org.apache.antlib.AbstractProjectTestCase;
import java.io.File;
/**
* Test cases for the <for-each> task.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
* @version $Revision: 1.1 $ $Date: 2002/06/06 10:57:03 $
*/
public class ForEachTaskTestCase
extends AbstractProjectTestCase
{
public ForEachTaskTestCase( final String name )
{
super( name );
}
/**
* Execute each of the test cases.
*/
public void testExecution() throws Exception
{
final File projectFile = getTestResource( "for-each.ant" );
executeTarget( projectFile, "list" );
executeTarget( projectFile, "singleton-list" );
executeTarget( projectFile, "empty-list" );
executeTarget( projectFile, "empty-token" );
executeTarget( projectFile, "no-list" );
executeTarget( projectFile, "no-property" );
executeTarget( projectFile, "no-tasks" );
}
}
1.1
jakarta-ant-myrmidon/antlib/src/test/org/apache/antlib/core/test/for-each.ant
Index: for-each.ant
===================================================================
<project version="2.0">
<!-- Test execution -->
<target name="list">
<expect-output>
<out>
<for-each>
<log>prop = item1</log>
<log>prop = item2</log>
<log>prop = item3</log>
</for-each>
</out>
<tasks>
<for-each list="item1, item2, item3" property="prop">
<log>prop = ${prop}</log>
</for-each>
</tasks>
</expect-output>
</target>
<target name="singleton-list">
<expect-output>
<out>
<for-each>
<log>prop = item1</log>
</for-each>
</out>
<tasks>
<for-each list="item1" property="prop">
<log>prop = ${prop}</log>
</for-each>
</tasks>
</expect-output>
</target>
<target name="empty-list">
<expect-output>
<tasks>
<for-each list="" property="prop">
<log>prop = ${prop}</log>
</for-each>
</tasks>
</expect-output>
</target>
<target name="empty-token">
<expect-output>
<out>
<for-each>
<log>prop = (item1)</log>
<log>prop = ()</log>
</for-each>
</out>
<tasks>
<for-each list="item1, " property="prop">
<log>prop = (${prop})</log>
</for-each>
</tasks>
</expect-output>
</target>
<target name="no-tasks">
<expect-output>
<tasks>
<for-each list="item1, item2" property="prop">
</for-each>
</tasks>
</expect-output>
</target>
<!-- Test validation -->
<target name="no-list">
<expect-error>
<exc>No values specified.</exc>
<tasks>
<for-each property="prop">
<fail>should not get here</fail>
</for-each>
</tasks>
</expect-error>
</target>
<target name="no-property">
<expect-error>
<exc>No property name specified.</exc>
<tasks>
<for-each list="item1, item2">
<fail>should not get here</fail>
</for-each>
</tasks>
</expect-error>
</target>
</project>
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>