conor 2003/06/18 06:02:12 Modified: src/etc/testcases/taskdefs/optional/script scriptdef.xml src/main/org/apache/tools/ant DynamicConfigurator.java src/main/org/apache/tools/ant/taskdefs/optional/script ScriptDef.java ScriptDefBase.java src/testcases/org/apache/tools/ant/taskdefs/optional/script ScriptDefTest.java Log: Rework scriptdef to use DynamicConfigurator Revision Changes Path 1.2 +16 -0 ant/src/etc/testcases/taskdefs/optional/script/scriptdef.xml Index: scriptdef.xml =================================================================== RCS file: /home/cvs/ant/src/etc/testcases/taskdefs/optional/script/scriptdef.xml,v retrieving revision 1.1 retrieving revision 1.2 diff -u -w -u -r1.1 -r1.2 --- scriptdef.xml 9 Jun 2003 13:38:06 -0000 1.1 +++ scriptdef.xml 18 Jun 2003 13:02:12 -0000 1.2 @@ -109,4 +109,20 @@ <attribute name="attr1"/> </scriptdef> </target> + + <target name="property"> + <scriptdef name="scripttest" language="javascript"> + <attribute name="attr1"/> + <![CDATA[ + + project.log("Attribute value = " + attributes.get("attr1")); + ]]> + </scriptdef> + + <property name="testproperty" value="test"/> + <scripttest attr1="${testproperty}"> + </scripttest> + </target> + + </project> 1.5 +2 -2 ant/src/main/org/apache/tools/ant/DynamicConfigurator.java Index: DynamicConfigurator.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/DynamicConfigurator.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -w -u -r1.4 -r1.5 --- DynamicConfigurator.java 10 Feb 2003 14:13:30 -0000 1.4 +++ DynamicConfigurator.java 18 Jun 2003 13:02:12 -0000 1.5 @@ -69,7 +69,7 @@ * @param value the new value of the attribute * @throws BuildException when any error occurs */ - public void setDynamicAttribute(String name, String value) + void setDynamicAttribute(String name, String value) throws BuildException; /** @@ -79,5 +79,5 @@ * @throws BuildException when any error occurs * @return the element created */ - public Object createDynamicElement(String name) throws BuildException; + Object createDynamicElement(String name) throws BuildException; } 1.2 +60 -81 ant/src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDef.java Index: ScriptDef.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDef.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -w -u -r1.1 -r1.2 --- ScriptDef.java 9 Jun 2003 13:38:07 -0000 1.1 +++ ScriptDef.java 18 Jun 2003 13:02:12 -0000 1.2 @@ -110,6 +110,10 @@ this.name = name; } + public boolean isAttributeSupported(String attributeName) { + return attributeSet.contains(attributeName); + } + /** * Set the scripting language used by this script * @@ -276,40 +280,14 @@ project.addTaskDefinition(name, ScriptDefBase.class); } - /** - * Execute the script. - * - * @param scriptConfig the RuntimeConfigurable which contains the attribute - * definitions for the script task instance. - * - * @param elements a list of UnknownElements which contain the configuration - * of the nested elements of the script instance. - */ - public void executeScript(RuntimeConfigurable scriptConfig, List elements) { - - Map configAttributes = scriptConfig.getAttributeMap(); - for (Iterator i = configAttributes.keySet().iterator(); i.hasNext();) { - String attributeName = (String) i.next(); - if (!attributeSet.contains(attributeName)) { - throw new BuildException("<" + name + "> does not support " - + "the \"" + attributeName + "\" attribute"); - } - } - - // handle nested elements - Map elementInstances = new HashMap(); - for (Iterator i = elements.iterator(); i.hasNext();) { - UnknownElement element = (UnknownElement) i.next(); - String elementTag = element.getTag().toLowerCase(Locale.US); - + public Object createNestedElement(String elementName) { NestedElement definition - = (NestedElement) nestedElementMap.get(elementTag); + = (NestedElement) nestedElementMap.get(elementName); if (definition == null) { throw new BuildException("<" + name + "> does not support " - + "the <" + elementTag + "> nested element"); + + "the <" + elementName + "> nested element"); } - // what is the type of the object to be created Object instance = null; String classname = definition.className; if (classname == null) { @@ -332,7 +310,7 @@ } catch (Throwable e2) { throw new BuildException("scriptdef: Unable to load " + "class " + classname + " for nested element <" - + elementTag + ">", e2); + + elementName + ">", e2); } } @@ -341,34 +319,35 @@ } catch (Throwable e) { throw new BuildException("scriptdef: Unable to create " + "element of class " + classname + " for nested " - + "element <" + elementTag + ">", e); + + "element <" + elementName + ">", e); } getProject().setProjectReference(instance); } if (instance == null) { throw new BuildException("<" + name + "> is unable to create " - + "the <" + elementTag + "> nested element"); + + "the <" + elementName + "> nested element"); } - - element.configure(instance); - - // find the appropriate list - List instanceList = (List) elementInstances.get(elementTag); - if (instanceList == null) { - instanceList = new ArrayList(); - elementInstances.put(elementTag, instanceList); - } - instanceList.add(instance); + return instance; } + /** + * Execute the script. + * + * @param scriptConfig the RuntimeConfigurable which contains the attribute + * definitions for the script task instance. + * + * @param elements a list of UnknownElements which contain the configuration + * of the nested elements of the script instance. + */ + public void executeScript(Map attributes, Map elements) { try { BSFManager manager = new BSFManager(); // execute the script - manager.declareBean("attributes", configAttributes, - configAttributes.getClass()); - manager.declareBean("elements", elementInstances, - elementInstances.getClass()); + manager.declareBean("attributes", attributes, + attributes.getClass()); + manager.declareBean("elements", elements, + elements.getClass()); manager.declareBean("project", getProject(), Project.class); manager.exec(language, "scriptdef <" + name + ">", 0, 0, script); } catch (BSFException e) { 1.2 +31 -19 ant/src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDefBase.java Index: ScriptDefBase.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDefBase.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -w -u -r1.1 -r1.2 --- ScriptDefBase.java 9 Jun 2003 13:38:07 -0000 1.1 +++ ScriptDefBase.java 18 Jun 2003 13:02:12 -0000 1.2 @@ -57,8 +57,9 @@ import org.apache.tools.ant.TaskContainer; import org.apache.tools.ant.MagicNames; import org.apache.tools.ant.BuildException; - +import org.apache.tools.ant.DynamicConfigurator; import java.util.Map; +import java.util.HashMap; import java.util.List; import java.util.ArrayList; @@ -71,16 +72,23 @@ * @author Conor MacNeill * @since Ant 1.6 */ -public class ScriptDefBase extends Task implements TaskContainer { +public class ScriptDefBase extends Task implements DynamicConfigurator { + + /** Nested elements */ + private Map nestedElementMap = new HashMap(); - /** Nested elements - UnknownElements passed by Ant */ - private List nestedElements = new ArrayList(); + /** Attributes */ + private Map attributes = new HashMap(); /** * Locate the script defining task and execute the script by passing * control to it */ public void execute() { + getScript().executeScript(attributes, nestedElementMap); + } + + private ScriptDef getScript() { String name = getTaskType(); Map scriptRepository = (Map) getProject().getReference(MagicNames.SCRIPT_REPOSITORY); @@ -92,24 +100,28 @@ if (definition == null) { throw new BuildException("Script definition not found for " + name); } - definition.executeScript(getWrapper(), nestedElements); + return definition; } - /** - * Method overridden to prevent configuration of this task by the core - */ - public void maybeConfigure() { - // don't configure now - do it at script execute time + public Object createDynamicElement(String name) { + List nestedElementList = (List) nestedElementMap.get(name); + if (nestedElementList == null) { + nestedElementList = new ArrayList(); + nestedElementMap.put(name, nestedElementList); + } + Object element = getScript().createNestedElement(name); + nestedElementList.add(element); + return element; } - /** - * TaskContainer method implemented to prevent processing of the task - * instance by the Ant Core. - * - * @param nestedElement the nestedElement as an UnknownElement. - */ - public void addTask(Task nestedElement) { - nestedElements.add(nestedElement); + public void setDynamicAttribute(String name, String value) { + ScriptDef definition = getScript(); + if (!definition.isAttributeSupported(name)) { + throw new BuildException("<" + getTaskType() + + "> does not support the \"" + name + "\" attribute"); + } + + attributes.put(name, value); } } 1.3 +11 -0 ant/src/testcases/org/apache/tools/ant/taskdefs/optional/script/ScriptDefTest.java Index: ScriptDefTest.java =================================================================== RCS file: /home/cvs/ant/src/testcases/org/apache/tools/ant/taskdefs/optional/script/ScriptDefTest.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -w -u -r1.2 -r1.3 --- ScriptDefTest.java 9 Jun 2003 14:40:44 -0000 1.2 +++ ScriptDefTest.java 18 Jun 2003 13:02:12 -0000 1.3 @@ -141,4 +141,15 @@ "Should have detected duplicate attribute definition", "attr1 attribute more than once"); } + + public void testProperty() { + executeTarget("property"); + // get the fileset and its basedir + Project project = getProject(); + String log = getLog(); + assertTrue("Expecting property in attribute value replaced", + log.indexOf("Attribute value = test") != -1); + } + + }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]