You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by peter reilly <pe...@corvil.com> on 2003/02/20 11:11:15 UTC

[PATCH] Allow custom conditions using datatypes

Hi,

The included patch allows custom conditions.

Background:

Currently in ant one can implement custom mappers, selectors and
filters (see Java Development with Ant, chapter 20).
However there is no way to implement custom conditions. Also
the usage of the custom filters, mappers and selectors in build files
is not nice, in comparison to custom tasks and datatypes.

The idea of the patch is to allow the current custom datatypes to be
used as conditions (and mapper, filters and selectors). The classes
handling conditions, filters and selectors implement DynamicConfigurator
create the custom object from the datatype api.

The following examples are from the testsuite.
example: custom condition

<target name="custom-condition">

    <path id="testclasses">
      <pathelement location="../../../../build/testcases" />
      <pathelement path="${java.class.path}" />
    </path>

    <typedef
      name="custom_condition" 
      classname="org.apache.tools.ant.taskdefs.condition.CustomCondition"
      classpathref="testclasses"
    />
    <condition property="custom-condition-true">
      <custom_condition testvalue="true"/>
    </condition>
    <echo>${custom-condition-true}</echo>
  </target>

custom filter:

  <target name="test-custom-filter">
    <path id="testclasses">
      <pathelement location="../../../../build/testcases" />
      <pathelement path="${java.class.path}" />
    </path>
    
    <typedef
      name="custom_filter" 
      classname="org.apache.tools.ant.types.CustomFilter"
      classpathref="testclasses"
    />

    <delete file="dest_custom.txt"/>
    <copy file="custom_filter.txt" tofile="dest_custom.txt">
      <filterchain>
        <custom_filter replace="@" with="*"/>
      </filterchain>
    </copy>
  </target>

Mappers are done differently:
the datatype is used as an attribute.

  <target name="custom_mapper">
    <path id="testclasses">
      <pathelement location="../../../../build/testcases" />
      <pathelement path="${java.class.path}" />
    </path>
    
    <typedef
      name="custom_mapper" 
      classname="org.apache.tools.ant.types.CustomMapper"
      classpathref="testclasses"
    />
    <mkdir dir="customcopytest"/>
    <copy todir="customcopytest">
      <fileset dir="../../../main">
        <include name="**/taskdefs/*.java"/>
      </fileset>
      <mapper type="custom_mapper"/>
    </copy>
  </target>


---------------------

Changes to src/main/org/apache/tools/ant:

TaskAdapter.java:
    The setProject() method has been overridden to set the project on the
    proxied task. If this is not done, createDynamicElement in
    taskdefs/condition/ConditionBase is called before setProject.


taskdefs/condition/ConditionBase.java:
    Implements DynamicConfigurator.createDynamicElement(String name).
    this method creates the datatype (name), checks if it is a condition
    and addes it to the conditions.

types/FilterChain.java:
    same as ConditionBase

types/AbstractFileSet.java:
    same as ConditionBase.

types/Mapper.java:
    *** THIS IS A CHANGE TO THE CURRENT API *******
    The setType(MapperType type) method has been changed to
    setType(String type).
    ****************************************************
    The getImplementation() method has been modified to
    check if the name is a known mapper and if not to treat
    it as a datatype and use the getProject().createDataType()
    api call to create the mapper.


taskdefs/optional/RenameExtensions.java:
   This as been modified to use the Mapper.setType(String) method.


Changes to unit tests:

tests have been added to ensure that the mechanism works. Also
the mapper java test code has been changed to reflect the new
api.

Patch information:
The patch is against the current cvs (or a few hours ago) and
generated using:

diff -Naur ant ant-custom > custom-patch

The patch was then applied to a copy of ant (cvs source) 
and the unit tests were run.

I have made an entry in bugzilla - 17199.

I have tried to follow the ant task guidelines.
however.
 I do not have access to java1.1, the changes should work in that env.
 doc changes are not yet done
 the cvs diff -u command did not inclued added files (neither did
  cvs diff -Nu, cvs diff -u --new-file)