Creating your own filters is very simple, write a class that extends either IteratingFilter, or AbstractFilter and implement the necessary functions.
Let's say you want to create a new filter that adds the words Text-Zap
to the beginning
and end of each file. Here is how building this new filter should be approached. The code for this demo
can be found below.
AddText_Filter
, and have it extend the
IteratingFilter class (see the API docs
for more details about this class).doFilter
method. This method takes two parameters a StringBuffer
containing the text of the file, and a String which represents the files name. The file name can be
ignored. Append "Text-Zap" to beginning and end of the StringBuffer, and return it.
Here is the code that this example should look like:
package mypackage; import com.jmw.tzap.IteratingFilter; public class AddText_Filter extends IteratingFilter{ public StringBuffer doFilter(StringBuffer buffer, String fileName){ buffer.append(0, "Text-Zap"); //add text-zap to the beginning of the buffer buffer.append("Text-Zap"); //add text-zap to the end of the buffer return buffer; } }
Add the following line to your build file:
<taskdef name="addtext" classname="mypackage.AddText_Filter"/>
Here is what your new filter element should look like:
<addtext/>
The following filters are meant to be extended:
AbstractFilter
This is the base class for all filters. It does not provide any services other than
loging back to Ant. View API.
IteratingFilter
This is the base class of most Text-Zap filters. It sequentially iterates over each
file. With this filter one need only implement the doFilter
function.
View API.
ReplaceFilterPlus
A base filter that takes a regular expression and then does some processing to it before replacing it.
For a full treatment of this base class see
ReplaceFilterPlus
View API.
SplitFilter
This filter breaks up each file into a number of others. To extend this filter one must
implment the split
function which takes a single file and should return an
ArrayList of FilterFiles. View API.