Section 12.7. Creating New Types


12.7. Creating New Types

When creating new tasks, it's sometimes useful to create new datatypes to be used by those tasks. In this example, I'll create a new datatype that extends the Ant FileList type and interfaces easily to attributes in a custom task.

This example centers around a new data type, ProjectType, which extends the Ant FileList class. To use that data type, I'll develop a new custom task, projectTask, that supports nested elements named multiFile, each of which supports an attribute named files. You can set the files attribute to a string containing multiple file-names such as "ed.txt george.txt", and the code will create an object of a custom data type, ProjectType, to contain that list of files.

Example 12-22 holds the code for the new datatype, ProjectType, which extends the org.apache.tools.ant.types.FileList class. In this example, the code simply passes the list of files on back to the FileSet base class, but you can adapt this code to handle the list of files any way you want.

Example 12-22. The new data type (ch12/type/src/ProjectType.java)
package data; import org.apache.tools.ant.types.FileList; public class ProjectType extends FileList {     public void setFiles(String files)     {         super.setFiles(files);     } }

The build file for the custom task appears in Example 12-23; the nested multiFile element's files attribute takes the list of files that will be stored in internal filelist of the object of the custom data type.

Example 12-23. Build file for the datatype example (ch12/type/build.xml)
<?xml version="1.0" ?> <project basedir="." default="main">     <property name="src" value="src"/>     <property name="output" value="output"/>     <target name="main" depends="jar">     <taskdef name="projectTask" classname="ProjectTask" classpath="Project.jar" />                <projectTask>             <multiFile dir="src" files="ed.txt george.txt"/>         </projectTask>     </target>     <target name="compile">         <mkdir dir="${output}"/>         <javac srcdir="${src}" destdir="${output}"/>     </target>     <target name="jar" depends="compile">         <jar destfile="Project.jar" basedir="${output}"/>     </target> </project>

ProjectTask.java, which implements the projectTask task and uses the new datatype, appears in Example 12-24.

Example 12-24. Task that uses the new type (ch12/type/src/ProjectTask.java)
import data.ProjectType; import org.apache.tools.ant.Task; import org.apache.tools.ant.BuildException; public class ProjectTask extends Task {     ProjectType multiFile = null;     public void execute( ) throws BuildException     {         String[] files = multiFile.getFiles(getProject( ));         for(int loopIndex = 0; loopIndex < files.length; loopIndex++)         {             System.out.println(files[loopIndex]);         }     }     public ProjectType createMultiFile( )     {         multiFile = new ProjectType( );         return multiFile;     } }

Here's how it works. ProjectTask.java handles the nested multiFile element with a createMultiFile( ) method, which creates a new object of the ProjectType class (our custom data type, based on the FileList class):

    ProjectType multiFile = null;         .         .         .     public ProjectType createMultiFile( )     {         multiFile = new ProjectType( );         return multiFile;     }

The setFiles( ) method in the custom data type's support code creates a filelist from the list of files in the multiFile element's files attribute:

    public void setFiles(String files)     {         super.setFiles(files);     }

The files are now stored in the new datatype object named multiFile. When the task executes, it will display their names, using that object:

    public void execute( ) throws BuildException     {         String[] files = multiFile.getFiles(getProject( ));         for(int loopIndex = 0; loopIndex < files.length; loopIndex++)         {             System.out.println(files[loopIndex]);         }     }

When this build file executes, the custom task creates a new object of the custom datatype and displays the files in it:

%ant Buildfile: build.xml compile:     [javac] Compiling 3 source files to /home/steven/ant/ch12/type/output jar:       [jar] Building jar: /home/steven/ant/ch12/type/Project.jar main: [projectTask] ed.txt [projectTask] george.txt BUILD SUCCESSFUL Total time: 3 seconds

Customizing data types for use with custom tasks can be a powerful technique as you develop and extend Ant to better fulfill your needs.



    Ant. The Definitive Guide
    Ant: The Definitive Guide, 2nd Edition
    ISBN: 0596006098
    EAN: 2147483647
    Year: 2003
    Pages: 115
    Authors: Steve Holzner

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net