ant

Ant LineContains comma separated Value


I have a property which contained a string and now want to change it to be a comma separated list to test lines in a file.

Currently with one value the following works:

<loadfile property="contents" srcFile="output.log">
    <filterchain>
        <linecontains>
            <contains value="${findvalue}"></contains>
        </linecontains>
    </filterchain>
</loadfile>

So if a line in a file contains:
Hello World!

And value of findvalue = 'World' it would find the line. Now we want to find all lines that may match multiple words. So if lines of a file contain:
Hello World!
Bye Everyone!

We want to set the property findvalue = World,Everyone and pickup both lines of the file. Hopefully I am making sense on this, little hard for me to fully explain. Any ideas on how to best accomplish this?


Solution

  • One way is to use a linecontainsregexp filter to get lines that match a certain regular expression. The trick is to convert the comma-separated list of values into a single regex.

    If the property findvalue is World,Everyone, the regex could simply be World|Everyone, which means the line contains either World or Everyone.

    <property name="findvalue" value="World,Everyone" />
    <loadresource property="findvalueRegex">
        <propertyresource name="findvalue"/>
        <filterchain>
            <tokenfilter>
                <filetokenizer/>
                <replacestring from="," to="|" />
            </tokenfilter>
        </filterchain>
    </loadresource>
    

    Then pass the findvalueRegex property containing this regex to the linecontainsregexp filter:

    <loadfile property="contents" srcFile="output.log">
        <filterchain>
            <linecontainsregexp>
                <regexp pattern="${findvalueRegex}" />
            </linecontainsregexp>
        </filterchain>
    </loadfile>