ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From peter reilly <peter.rei...@corvil.com>
Subject Re: Free-form ANT Task Structure
Date Wed, 21 May 2003 08:36:33 GMT
Here is a simple example task:

import java.util.*;
import org.apache.tools.ant.DynamicConfigurator;
import org.apache.tools.ant.Task;

public class Dy
    extends Task
    implements DynamicConfigurator
{
    private String subName;
    private Map attributes = new HashMap();
    private List elements   = new ArrayList();
    
    public Object createDynamicElement(String name) {
        Dy dy = new Dy();
        dy.setSubName(name);
        dy.setProject(getProject());
        elements.add(dy);
        return dy;
    }

    private void setSubName(String subName) {
        this.subName = subName;
    }

    private String getSubName() {
        return subName;
    }
    
    public void setDynamicAttribute(String name, String value) {
        attributes.put(name, value);
    }

    private void print(int level) {
        StringBuffer b = new StringBuffer();
        for (int i = 0; i < level; ++i) {
            b.append("  ");
        }
        String indent = b.toString();
        for (Iterator i = attributes.entrySet().iterator(); i.hasNext();)
        {
            Map.Entry e = (Map.Entry) i.next();
            log(indent + e.getKey() + " = " + e.getValue());
        }

        for (Iterator i = elements.iterator(); i.hasNext();) {
            Dy dy = (Dy) i.next();
            log(indent + "element " + dy.getSubName());
            dy.print(level + 1);
        }
    }
    
    public void execute() {
        log("Dy");
        print(1);
    }
}

and the build.xml:
<project name="t" default="t">
  <target name="t">
    <mkdir dir="classes"/>
    <javac srcdir="src" destdir="classes"/>
    <taskdef name="dy" classname="Dy" classpath="classes"/>
    <dy>
      <integration-test>
        <test on-components="foo,bar">
          <include name="**/XYZComp/*Test"/>
          <configuration>
            <xyz-comp id="foo"/>
            <zyx-comp id="bar">
              <arbitrary value="null"/>
            </zyx-comp>
          </configuration>
        </test>
      </integration-test>
    </dy>
  </target>
</project>

generates:
Compiling 1 source file to /home/preilly/proj/learning/dy/classes
Dy
  element integration-test
    element test
      on-components = foo,bar
      element include
        name = **/XYZComp/*Test
      element configuration
        element xyz-comp
          id = foo
        element zyx-comp
          id = bar
          element arbitrary
            value = null

Peter.

On Tuesday 20 May 2003 17:38, Berin Loritsch wrote:
> peter reilly wrote:
> > To embed the config information, the easiest
> > is to map the info to objects.
> >
> > Ant introspection is very powerfull and easy
> > to use for normal java data object.
>
> The problem is that it only works when you know in advance
> what the configuration elements are going to be.  In this
> example, that is not the case.
>
> > Alternatively one can use the DynamicConfigurator
> > interface. This provides the name of the unknown element
> > and the name/value of unknown attributes.
>
> That might be something worth pursuing.  Now, can I have
> a mixture of DyanimicConfigurator objects and mapped
> objects in the *same* task?

Yes.

>
> Or more importantly as a child of one of the mapped
> objects?

Yes - you may implement what you want.
Ant's introspection looks add the addX and setX patterns
before handing the element/attribute over to DynamicConfigurator.

>
> For example:
>
> <integration-test>
>    <test on-components="foo,bar">
>      <include name="**/XYZComp/*Test"/>
>      <configuration>
>         <xyz-comp id="foo"/>
>         <zyx-comp id="bar">
>           <arbitrary value="null"/>
>         </zyx-comp>
>      </configuration>
>    </test>
> </integration-test>
>
> The child of the <configuration/> element is completely
> free-form, although the rest of the task elements would
> map to real objects.
>
> Is there an Task already written that uses the DynamicConfigurator
> where I can see how it works with it?
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> For additional commands, e-mail: dev-help@ant.apache.org


Mime
View raw message