April 2005
Intermediate to advanced
336 pages
7h 20m
English
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 ...
Read now
Unlock full access