ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From <Jan.Mate...@rzf.fin-nrw.de>
Subject AW: Question about simple nested text elements
Date Mon, 08 Feb 2010 06:38:28 GMT
Main problem was having the nested classes as 'static.

Jan


import java.util.ArrayList;
import java.util.List;

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

public class MyTask extends Task {
    // Hold a list of real Things-Objects. Not just Strings.
    private List<Things> things = new ArrayList<Things>();

    public void execute() {
        System.out.println("MyTask");
        for (Things t : things) {
            t.print();
        }
    }

    public void addThings(Things things) {
        this.things.add(things);
    }



    // Class must be marked as static for being accessible by the Runtime
    public static class Things extends DataType {
        private List<Thing> things = new ArrayList<Thing>();
        public List<Thing> getThings() {
              return things;
        }
        public void addThing (Thing f) {
            things.add(f);
        }
        private String language;
        public void setLanguage(String l) {
            language = l;
        }
        public String getLanguage() {
            return language;
        }
        public void print() {
            System.out.println("- Things: language=" + getLanguage());
            for(Thing t : things) {
                t.print();
            }
        }
    }



    public static class Thing extends DataType {
        private String name = "";
        public void addText(String name) {
            Project p = getProject();
            // All nested elements can occur multiple times. So be prepared.
            this.name += p.replaceProperties(name);
        }
        public String getName() {
            return name;
        }
        public void print() {
            System.out.println("  -- Thing: text=" + getName());
        }
    }

}



<project default="all">
    <property name="src.dir" value="src"/>
    <property name="build.dir" value="build"/>
    <property name="classes.dir" value="${build.dir}/classes"/>
    
    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>
    
    <target name="build">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="true" debug="true"/>
    </target>
    
    <target name="test">
        <taskdef name="mytask" classname="MyTask" classpath="${classes.dir}"/>
        <mytask>
            <things language="english">
                <thing>one</thing>
                <thing>two</thing>
            </things>
            <things language="german">
                <thing>eins</thing>
                <thing>zwei</thing>
            </things>
        </mytask>
    </target>
    
    <target name="all" depends="clean,build,test"/>
</project> 

>-----Urspr√ľngliche Nachricht-----
>Von: scabbage [mailto:guanshan@gmail.com] 
>Gesendet: Samstag, 6. Februar 2010 00:41
>An: user@ant.apache.org
>Betreff: Re: Question about simple nested text elements
>
>
>Sorry, the code i posted above has some typos. Here's the correct one:
>
>
>import java.util.ArrayList;
>import java.util.List;
>
>import org.apache.tools.ant.Project;
>import org.apache.tools.ant.Task;
>import org.apache.tools.ant.types.DataType;
>
>public class MyTask extends Task {
>    private List<String> things = new ArrayList<String>();
>
>    public void execute() {
>        for (String t : things)
>            System.out.println (t);   
>    }
>    public void  addThings(Things things) {
>        for (Thing t : things.getThings())            
>            this.things.add(t.getName());
>    }
>    public class Things extends DataType {
>        private List<Thing> things = new ArrayList<Thing>();
>
>        public List<Thing> getThings() {            
>              return things;
>        }        
>        public void addThing (Thing f) {            
>            things.add(f);        
>        }    
>    }    
>    public class Thing extends DataType {
>        private String name;        
>        public void addText(String name) {
>            Project p = getProject();            
>            this.name = p.replaceProperties(name);
>        }        
>
>        public String getName() {
>            return name;        
>        }
>    }
>
>}
>
>Thanks.
>
>David
>
>
>scabbage wrote:
>> 
>> I'm trying to create my own customized task in Ant 1.7.1. I 
>would like to
>> use my task this way:
>> 
>> <mytask a="blah" b="foo">
>>    <things>
>>       <thing>xxx</thing>
>>       <thing>yyy</thing>
>>       <thing>zzz</thing>
>>    </things>
>> </mytask>
>> 
>> My class looks like:
>> 
>> public class MyTask extends Task {
>>    private String a;
>>    private String b;
>>    private List<String> things = new ArrayList<String>();
>> 
>>    // setting and gettings for a and b ...
>> 
>>    public void execute() {
>>       // loop through things
>>    }
>> 
>> }
>> 
>> Now can anyone tell me how I should write the setter for 
>"things"? The Ant
>> doc is very vague about simple nested text elements.
>> 
>> Thanks.
>> 
>> David
>> 
>
>-- 
>View this message in context: 
>http://old.nabble.com/Question-about-simple-nested-text-element
>s-tp27460186p27475534.html
>Sent from the Ant - Users mailing list archive at Nabble.com.
>
>
>---------------------------------------------------------------------
>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