Section 12.5. Creating Custom Filters


12.5. Creating Custom Filters

You can implement custom Ant filters. To do that, you can extend Ant classes like org.apache.tools.ant.filters.BaseParamFilterReader. If you want your filter to be chainable, implement the org.apache.tools.ant.filters.ChainableReader interface.

Example 12-18 shows how to write a filter. In this example, the code reads each line of a Java file using the read( ) method and adds a Java single-line comment, //, at the beginning of each line. When the code reaches the end of the file, it returns a value of -1 to quit.

Example 12-18. A new filter (ch12/filter/src/ProjectFilter.java)
import java.io.Reader; import java.io.IOException; import org.apache.tools.ant.filters.ChainableReader; import org.apache.tools.ant.filters.BaseParamFilterReader; public final class ProjectFilter extends BaseParamFilterReader implements ChainableReader {     String data = null;     public ProjectFilter( )     {         super( );     }          public ProjectFilter(final Reader reader)     {         super(reader);     }     public final Reader chain(final Reader reader)     {         ProjectFilter filter = new ProjectFilter(reader);         filter.setInitialized(true);         return filter;     }     public final int read( ) throws IOException     {         int leadChar = -1;         if(data != null) {             leadChar = data.charAt(0);             data = data.substring(1);             if(data.length( ) == 0) {                 data = null;             }         }         else {             data = readLine( );             if(data == null) {                 leadChar = -1;             }             else {                 data = "// " + data;                 return read( );             }         }         return leadChar;     } }

You can see how to use this new filter in the build file in Example 12-19. This build file copies all .java files in the project, comments out each line, and stores the result in a directory named commented.

Example 12-19. Build file for the new filter (ch12/filter/build.xml)
<?xml version="1.0"?> <project basedir="." default="main">     <property name="src" value="src"/>     <property name="output" value="output"/>     <property name="commented" value="commented"/>     <target name="main" depends="jar">         <copy todir="${commented}">             <fileset dir="${src}" includes="**/*.java"/>             <filterchain>                 <filterreader                   classname="ProjectFilter"                   classpath="Project.jar"/>             </filterchain>         </copy>     </target>     <target name="compile">         <mkdir dir="${output}"/>         <mkdir dir="${commented}"/>         <javac srcdir="${src}" destdir="${output}"/>     </target>     <target name="jar" depends="compile">         <jar destfile="Project.jar" basedir="${output}"/>     </target> </project>

When you run this build file, every line in the copied Project.java file is commented out when filtered and copied:

// import java.io.Reader; // import java.io.IOException; // import org.apache.tools.ant.filters.ChainableReader; // import org.apache.tools.ant.filters.BaseParamFilterReader; //  // public final class ProjectFilter extends BaseParamFilterReader implements  ChainableReader // { //     String data = null;         .         .         .



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

    Similar book on Amazon

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