ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From peter reilly <peter.rei...@corvil.com>
Subject Re: Custom data type problem
Date Mon, 12 May 2003 15:34:38 GMT
I would not use another class for the referenced
type and the normal attribute in ant for reference is
"refid", also the setid method is not needed as
this is done by ant xml parsing and not as part
of the introspection of the class.

: MyValue.java :
import org.apache.tools.ant.ProjectComponent;
import org.apache.tools.ant.types.Reference;

public class MyValue
    extends ProjectComponent
{
    private String text;

    public void addText(String text) {
        this.text = text;
    }
    
    public void setRefId(Reference id) {
        this.text =
            id.getReferencedObject(getProject()).toString();
    }

    public String toString() {
        return text;
    }
}

or using a DataType

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

public class MyValue
    extends DataType
{
    private String text;

    public void addText(String text) {
        this.text = text;
    }

    public String toString() {
        if (isReference()) {
            return ref.getReferencedObject(getProject()).toString();
        }
        return text;
    }
}


Build script:

    <taskdef name="myexec" classname="MyExec"
             classpath="classes" loaderref="classes"/>
    <typedef name="myvalue" classname="MyValue"
             classpath="classes" loaderref="classes"/>
    <myvalue id="fred">Hello Fred</myvalue>
    <myexec>
      <value refid="fred"/>
    </myexec>

Peter.

On Monday 12 May 2003 15:51, Tony Thompson wrote:
> 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
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message