ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Tony Thompson" <tony.thomp...@stone-ware.com>
Subject Re: Custom data type problem
Date Mon, 12 May 2003 14:51:11 GMT
That works for me as well but, your example is not quite like the issue
I described.  I was able to do some more work to figure out that if I
hold on to the "Value" object and get the text just before I need it, it
is there (see the toString() method in MyData).

Here is the example:

import org.apache.tools.ant.Task;

public class MyExec extends Task {
    public void addConfiguredValue(MyValue v) {
        System.out.println("v is " + v);
    }
}

**************************************************************

import org.apache.tools.ant.types.DataType;

public class MyData extends DataType {
    private String id;
    private String text;
    private MyValue value;

    public MyData() {
    }

    public void addConfiguredValue( MyValue value ) {
        text = value.toString();
        this.value = value;
    }

    public void setId( String id ) {
        this.id = id;
    }

    public String getId() {
        return id;
    }

    public String toString() {
        System.out.println( "Late resolve: "+value );
        return text;
    }
}

*************************************************************

import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.Reference;

public class MyValue extends Task {
    private StringBuffer sb = new StringBuffer();
    private Reference id;

    public void addText(String text) {
        sb.append( text );
    }

    public void setUseData( Reference id ) {
        this.id = id;
    }

    public String toString() {
        if( id != null ) {
            return ((MyData)id.getReferencedObject( getProject()
)).toString();
        }

        if( sb.length() == 0 ) {
            return null;
        }

        return sb.toString();
    }
}

*************************************************************

Here is what you would need in the script:

    <taskdef name="myexec" classname="MyExec"/>
    <typedef name="data" classname="MyData"/>

    <data id="fred"><value>This is data text.</value></data>

  <target name="t">
    <myexec>
      <value usedata="fred"/>
    </myexec>
  </target>

>>> peter.reilly@corvil.com 05/12/03 03:10AM >>>
I do not see this problem.

:MyExec.java:
import org.apache.tools.ant.Task;

public class MyExec extends Task {
    public void addConfiguredValue(MyValue v) {
        System.out.println("v is " + v);
    }
    public static class MyValue {
        private String text;
        
        public void addText(String text) {
            this.text = text;
        }

        public String toString() {
            return text;
        }
    }
}

:build.xml:
<project name="t" default="t">
  <target name="t">
    <mkdir dir="classes"/>
    <javac srcdir="src" destdir="classes"/>
    <taskdef name="myexec" classname="MyExec"
             classpath="classes"/>
    <myexec>
      <value>Hello world</value>
    </myexec>
  </target>
</project>

This outputs:
v is Hello world

Tested with ant 1.5.3-1 and 1.5.2

Peter

On Friday 09 May 2003 20:09, Tony Thompson wrote:
> I created my own custom data type that handles a data type that
looks
> something like this:
>
> <mytype>
>     <attribute>
>         <value>Value 1</value>
>         <value>Value 2</value>
>     </attribute>
> </mytype>
>
> My "Value" class has the following methods:
>
> {
> ...
>     public void addText( final String text ) {
>         m_value.append( text );
>     }
>
>     public String toString() {
>         return m_value.toString();
>     }
> }
>
> My "Attribute" class has a method to add configured "Value" objects:
>
>     public final void addConfiguredValue( final Value value ) {
>         m_values.add( value.toString() );
>     }
>
> When I call value.toString() at this time, Value.addText() has not
been
> invoked yet so the string is empty.  I thought that the
> addConfiguredXXX() methods were supposed to add already configured
> objects.  What should I change to make this behave properly?
>
> I am using version 1.5.3.
>
> Thanks.
> Tony
>
>
---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org 
> For additional commands, e-mail: user-help@ant.apache.org 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org 
For additional commands, e-mail: user-help@ant.apache.org 


Mime
View raw message