cat foo|head -n10|grep blee > bar
Ant was not flexible enough. There was no way for the <copy> task to do something similar. If you wanted the <copy> task to get the first 10 lines, you would have had to create special attributes:
<copy file="foo" tofile="bar" head="10" contains="blee"/>
The obvious problem thus surfaced: Ant tasks would not be able to accomodate such data transformation attributes as they would be endless. The task would also not know in which order these attributes were to be interpreted. That is, must the task execute the contains attribute first and then the head attribute or vice-versa? What Ant tasks needed was a mechanism to allow pluggable filter (data tranformer) chains. Ant would provide a few filters for which there have been repeated requests. Users with special filtering needs would be able to easily write their own and plug them in.
The solution was to refactor data transformation oriented tasks to support FilterChains. A FilterChain is a group of ordered FilterReaders. Users can define their own FilterReaders by just extending the java.io.FilterReader class. Such custom FilterReaders can be easily plugged in as nested elements of <filterchain> by using <filterreader> elements.
Example:
Ant provides some built-in filter readers. These filter readers can also be declared using a syntax similar to the above syntax. However, they can be declared using some simpler syntax also.<copy file="${src.file}" tofile="${dest.file}"> <filterchain> <filterreader classname="your.extension.of.java.io.FilterReader"> <param name="foo" value="bar"/> </filterreader> <filterreader classname="another.extension.of.java.io.FilterReader"> <classpath> <pathelement path="${classpath}"/> </classpath> <param name="blah" value="blee"/> <param type="abra" value="cadabra"/> </filterreader> </filterchain> </copy>
Example:
is equivalent to:<loadfile srcfile="${src.file}" property="${src.file.head}"> <filterchain> <headfilter lines="15"/> </filterchain> </loadfile>
The following built-in tasks support nested <filterchain> elements.<loadfile srcfile="${src.file}" property="${src.file.head}"> <filterchain> <filterreader classname="org.apache.tools.ant.filters.HeadFilter"> <param name="lines" value="15"/> </filterreader> </filterchain> </loadfile>
Attribute | Description | Required |
classname | The class name of the filter reader. | Yes |
The following FilterReaders are supplied with the default distribution.
This filters basic constants defined in a Java Class, and outputs them in lines composed of the format name=value
Convenience method:<loadproperties srcfile="foo.class"> <filterchain> <filterreader classname="org.apache.tools.ant.filters.ClassConstants"/> </filterchain> </loadproperties>
<loadproperties srcfile="foo.class"> <filterchain> <classconstants/> </filterchain> </loadproperties>
If the data contains data that represents Ant properties (of the form ${...}), that is substituted with the property's actual value.
Convenience method:<echo message="All these moments will be lost in time, like teardrops in the ${weather}" file="loadfile1.tmp" /> <property name="weather" value="rain" /> <loadfile property="modifiedmessage" srcFile="loadfile1.tmp"> <filterchain> <filterreader classname="org.apache.tools.ant.filters.ExpandProperties"/> </filterchain> </loadfile>
<echo message="All these moments will be lost in time, like teardrops in the ${weather}" file="loadfile1.tmp" /> <property name="weather" value="rain" /> <loadfile property="modifiedmessage" srcFile="loadfile1.tmp"> <filterchain> <expandproperties/> </filterchain> </loadfile>
Parameter Name | Parameter Value | Required |
lines | Number of lines to be read. Defaults to "10" | No |
Convenience method:<loadfile srcfile="${src.file}" property="${src.file.head}"> <filterchain> <filterreader classname="org.apache.tools.ant.filters.HeadFilter"> <param name="lines" value="15"/> </filterreader> </filterchain> </loadfile>
<loadfile srcfile="${src.file}" property="${src.file.head}"> <filterchain> <headfilter lines="15"/> </filterchain> </loadfile>
Parameter Type | Parameter Name | Parameter Value | Required |
tokenchar | begintoken | Character marking the beginning of a token. Defaults to @ | No |
tokenchar | endtoken | Character marking the end of a token. Defaults to @ | No |
token | User defined String. | User defined search String | Yes |
Convenience method:<tstamp/> <loadfile srcfile="${src.file}" property="${src.file.replaced}"> <filterchain> <filterreader classname="org.apache.tools.ant.filters.ReplaceTokens"> <param type="token" name="DATE" value="${TODAY}"/> </filterreader> </filterchain> </loadfile>
<tstamp/> <loadfile srcfile="${src.file}" property="${src.file.replaced}"> <filterchain> <replacetokens> <token key="DATE" value="${TODAY}"/> </replacetokens> </filterchain> </loadfile>
Convenience method:<loadfile srcfile="${java.src.file}" property="${java.src.file.nocomments}"> <filterchain> <filterreader classname="org.apache.tools.ant.filters.StripJavaComments"/> </filterchain> </loadfile>
<loadfile srcfile="${java.src.file}" property="${java.src.file.nocomments}"> <filterchain> <stripjavacomments/> </filterchain> </loadfile>
Parameter Name | Parameter Value | Required |
linebreaks | Characters that are to be stripped out. Defaults to "\r\n" | No |
Convenience method:<loadfile srcfile="${src.file}" property="${src.file.contents}"> <filterchain> <filterreader classname="org.apache.tools.ant.filters.StripLineBreaks"/> </filterchain> </loadfile>
This treats the '(' and ')' characters as line break characters and strips them.<loadfile srcfile="${src.file}" property="${src.file.contents}"> <filterchain> <striplinebreaks/> </filterchain> </loadfile>
<loadfile srcfile="${src.file}" property="${src.file.contents}"> <filterchain> <filterreader classname="org.apache.tools.ant.filters.StripLineBreaks"> <param name="linebreaks" value="()"/> </filterreader> </filterchain> </loadfile>
Parameter Name | Parameter Value | Required |
lines | tablength Defaults to "8" | No |
Convenience method:<loadfile srcfile="${src.file}" property="${src.file.notab}"> <filterchain> <filterreader classname="org.apache.tools.ant.filters.TabsToSpaces"/> </filterchain> </loadfile>
<loadfile srcfile="${src.file}" property="${src.file.notab}"> <filterchain> <tabstospaces/> </filterchain> </loadfile>
Parameter Name | Parameter Value | Required |
lines | Number of lines to be read. Defaults to "10" | No |
Convenience method:<loadfile srcfile="${src.file}" property="${src.file.tail}"> <filterchain> <filterreader classname="org.apache.tools.ant.filters.TailFilter"> <param name="lines" value="15"/> </filterreader> </filterchain> </loadfile>
This stores the last 5 lines of the first 15 lines of the supplied data in the property ${src.file.mid}<loadfile srcfile="${src.file}" property="${src.file.tail}"> <filterchain> <tailfilter lines="15"/> </filterchain> </loadfile>
Convenience method:<loadfile srcfile="${src.file}" property="${src.file.mid}"> <filterchain> <filterreader classname="org.apache.tools.ant.filters.HeadFilter"> <param name="lines" value="15"/> </filterreader> <filterreader classname="org.apache.tools.ant.filters.TailFilter"> <param name="lines" value="5"/> </filterreader> </filterchain> </loadfile>
<loadfile srcfile="${src.file}" property="${src.file.mid}"> <filterchain> <HeadFilter lines="15"/> <TailFilter lines="5"/> </filterchain> </loadfile>
Copyright © 2002 Apache Software Foundation. All rights Reserved.