From 6d54725ac9d2fc606d5b4d8ec6574b648399f68d Mon Sep 17 00:00:00 2001 From: Erik Hatcher Date: Sat, 27 Apr 2002 12:51:03 +0000 Subject: [PATCH] DynamicConfigurator - allows tasks themselves to handle unknown elements and attributes rather than being constrained by the IntrospectionHelper rules. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@272586 13f79535-47bb-0310-9956-ffa450edef68 --- src/etc/testcases/taskdefs/dynamictask.xml | 21 +++++ .../apache/tools/ant/DynamicConfigurator.java | 67 +++++++++++++++ .../apache/tools/ant/IntrospectionHelper.java | 31 +++++-- .../tools/ant/taskdefs/DynamicTask.java | 82 +++++++++++++++++++ .../tools/ant/taskdefs/DynamicTest.java | 76 +++++++++++++++++ 5 files changed, 269 insertions(+), 8 deletions(-) create mode 100644 src/etc/testcases/taskdefs/dynamictask.xml create mode 100644 src/main/org/apache/tools/ant/DynamicConfigurator.java create mode 100644 src/testcases/org/apache/tools/ant/taskdefs/DynamicTask.java create mode 100644 src/testcases/org/apache/tools/ant/taskdefs/DynamicTest.java diff --git a/src/etc/testcases/taskdefs/dynamictask.xml b/src/etc/testcases/taskdefs/dynamictask.xml new file mode 100644 index 000000000..63ee19ad5 --- /dev/null +++ b/src/etc/testcases/taskdefs/dynamictask.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/org/apache/tools/ant/DynamicConfigurator.java b/src/main/org/apache/tools/ant/DynamicConfigurator.java new file mode 100644 index 000000000..763b7bdcf --- /dev/null +++ b/src/main/org/apache/tools/ant/DynamicConfigurator.java @@ -0,0 +1,67 @@ +/* + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2002 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowlegement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowlegement may appear in the software itself, + * if and wherever such third-party acknowlegements normally appear. + * + * 4. The names "The Jakarta Project", "Ant", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Group. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +package org.apache.tools.ant; + +/** + * Enables a task to control unknown attributes and elements. + * + * @author Erik Hatcher + * @since Ant 1.5 + */ +public interface DynamicConfigurator { + public void setDynamicAttribute(String name, String value); + + public Object createDynamicElement(String name); +} diff --git a/src/main/org/apache/tools/ant/IntrospectionHelper.java b/src/main/org/apache/tools/ant/IntrospectionHelper.java index 971ff79cb..e288d678c 100644 --- a/src/main/org/apache/tools/ant/IntrospectionHelper.java +++ b/src/main/org/apache/tools/ant/IntrospectionHelper.java @@ -54,6 +54,7 @@ package org.apache.tools.ant; +import org.apache.tools.ant.DynamicConfigurator; import org.apache.tools.ant.types.Path; import org.apache.tools.ant.types.EnumeratedAttribute; @@ -405,15 +406,19 @@ public class IntrospectionHelper implements BuildListener { * method fails. */ public void setAttribute(Project p, Object element, String attributeName, - String value) - throws BuildException { - AttributeSetter as - = (AttributeSetter) attributeSetters.get(attributeName); + String value) throws BuildException { + AttributeSetter as = (AttributeSetter) attributeSetters.get(attributeName); if (as == null) { - String msg = p.getElementName(element) + - //String msg = "Class " + element.getClass().getName() + - " doesn't support the \"" + attributeName + "\" attribute."; - throw new BuildException(msg); + if (element instanceof DynamicConfigurator) { + DynamicConfigurator dc = (DynamicConfigurator) element; + dc.setDynamicAttribute(attributeName, value); + return; + } + else { + String msg = getElementName(p, element) + + " doesn't support the \"" + attributeName + "\" attribute."; + throw new BuildException(msg); + } } try { as.set(p, element, value); @@ -498,6 +503,16 @@ public class IntrospectionHelper implements BuildListener { public Object createElement(Project project, Object parent, String elementName) throws BuildException { NestedCreator nc = (NestedCreator) nestedCreators.get(elementName); + if (nc == null && parent instanceof DynamicConfigurator) { + DynamicConfigurator dc = (DynamicConfigurator) parent; + Object nestedElement = dc.createDynamicElement(elementName); + if (nestedElement != null) { + if (nestedElement instanceof ProjectComponent) { + ((ProjectComponent) nestedElement).setProject(project); + } + return nestedElement; + } + } if (nc == null) { String msg = project.getElementName(parent) + " doesn't support the nested \"" + elementName + "\" element."; diff --git a/src/testcases/org/apache/tools/ant/taskdefs/DynamicTask.java b/src/testcases/org/apache/tools/ant/taskdefs/DynamicTask.java new file mode 100644 index 000000000..2623addc5 --- /dev/null +++ b/src/testcases/org/apache/tools/ant/taskdefs/DynamicTask.java @@ -0,0 +1,82 @@ +/* + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2002 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowlegement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowlegement may appear in the software itself, + * if and wherever such third-party acknowlegements normally appear. + * + * 4. The names "The Jakarta Project", "Ant", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Group. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +package org.apache.tools.ant.taskdefs; + +import org.apache.tools.ant.DynamicConfigurator; +import org.apache.tools.ant.Task; + +public class DynamicTask extends Task implements DynamicConfigurator { + + public void execute() { + } + + public void setDynamicAttribute(String name, String value) { + project.setNewProperty(name, value); + } + + public Object createDynamicElement(String name) { + return new Sub(); + } + + public class Sub implements DynamicConfigurator { + public void setDynamicAttribute(String name, String value) { + project.setNewProperty(name, value); + } + + public Object createDynamicElement(String name) { + return null; + } + } +} diff --git a/src/testcases/org/apache/tools/ant/taskdefs/DynamicTest.java b/src/testcases/org/apache/tools/ant/taskdefs/DynamicTest.java new file mode 100644 index 000000000..86b548f67 --- /dev/null +++ b/src/testcases/org/apache/tools/ant/taskdefs/DynamicTest.java @@ -0,0 +1,76 @@ +/* + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2002 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowlegement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowlegement may appear in the software itself, + * if and wherever such third-party acknowlegements normally appear. + * + * 4. The names "The Jakarta Project", "Ant", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Group. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +package org.apache.tools.ant.taskdefs; + +import org.apache.tools.ant.BuildFileTest; + +public class DynamicTest extends BuildFileTest { + + public DynamicTest(String name) { + super(name); + } + + public void setUp() { + configureProject("src/etc/testcases/taskdefs/dynamictask.xml"); + } + + public void testSimple() { + executeTarget("simple"); + assertEquals("1", project.getProperty("prop1")); + assertEquals("2", project.getProperty("prop2")); + assertEquals("3", project.getProperty("prop3")); + assertEquals("4", project.getProperty("prop4")); + } +}