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 16:44:50 GMT
I am not having an issue with multiple values.  I was just saying that I
couldn't do something like this:

<data id="fred">Value 1.</data>

because I need to be able to keep track of individual values, like
this:

<data id="fred">
   <value>Value 1.</value>
   <value>Value 2.</value>
   <value>Value 3.</value>
</data>

I can keep track of the individual values without an issue.  The
problem is when I addConfiguredValue() to "data", the addText() method
of "value" hasn't executed yet so, addConfiguredValue() thinks the
"value" is null.  The modified example that I sent demonstrates the
issue.  If I hold on to "value" until later, addText() has been called
and it works.

Tony

>>> peter reilly <peter.reilly@corvil.com> 05/12/03 11:21AM >>>
I am not to sure I follow what you want to do..
However for the multi value you could do something like:

public class MyMultiValue
    extends ProjectComponent
{
    private StringBuffer buffer = new StringBuffer();

    public void addConfiguredValue(MyValue value) {
        buffer.append(value.toString());
    }

    public String toString() {
        return buffer.toString();
    }
}

And since my code for value did not check the
type of the refernce the following now works:

    <taskdef name="myexec" classname="MyExec"
             classpath="classes" loaderref="classes"/>
    <typedef name="myvalue" classname="MyValue"
             classpath="classes" loaderref="classes"/>
    <typedef name="mymultivalue" classname="MyMultiValue"
             classpath="classes" loaderref="classes"/>

    <myvalue id="fred">Fred</myvalue>

    <mymultivalue id="hello fred">
      <value>Hello </value>
      <value refid="fred"/>
    </mymultivalue>

    <myexec>
      <value refid="hello fred"/>
    </myexec>

Peter
On Monday 12 May 2003 16:59, you wrotee:
> I modified your example to model the type of thing that I am doing
that
> also demonstrates the issue that I am having.  The problem is, I need
to
> have more than one value declared as part of the "data" type:
>
> <data id="fred">
>    <value>Value 1.</value>
>    <value>Value 2.</value>
>    <value>Value 3.</value>
> </data>
>
> Thank you for your continued help.
> Tony
>
> >>> peter.reilly@corvil.com 05/12/03 10:34AM >>>
>
> 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: als.ant.Task;
> >
> > import org.auser-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 
>
>
---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org 
> For additional commands, e-mail: user-help@ant.apache.org 


Mime
View raw message