I'm reading the ANT Conditions task and trying to figure out how to use it based on a string found in one file.
I have an ANT build with series of replaceregexp targets that it runs on any .htm files in a directory. So I specify the fileset dir, then the regex pattern and the substitution, and makes the replacements in all .htm files in that directory. For example:
<target name="title" description="convert title">
<replaceregexp byline="false" flags="gs">
<regexp pattern="(<p.*?)(TopicTitle>.*?)(>)(.*?)(</span.*?/p>)"/>
<substitution expression="<title>\4</title>"/>
<fileset dir=".">
<include name="*.htm"/>
</fileset>
</replaceregexp>
</target>
Now what I want to do is use a regex to see if there's a certain word in the .htm file, and set a condition if it's a match. For example:
<condition property="isConcept">
<matches pattern="<body.*(?=ConceptStyle).*</body>" string=" "/>
</condition>
So if the string ConceptStyle
is matched by that regex, then the property isConcept
is set.
My question is: what do I put in the string=" "
part of that task? Or how do I give it the directory/file to search for the match?
UPDATE:
Okay so this is the new code from Manouti's answer below:
<target name="setProperties">
<loadfile property="contents" srcFile="C:\Developer\SVN\trunk\WordTemplates\conversions\Conversion_Demo.htm"/>
<condition property="isConcept">
<matches pattern="<body.*(?=ConceptStyle).*</body>" string="${contents}"/>
</condition>
</target>
To test if the property is set, my understanding is that I can add unless
to another target, like this:
<!-- sticking in a replace task to test if condition is set -->
<target name="conditionTest" unless="isConcept">
<replaceregexp byline="false" flags="gs">
<regexp pattern="conbody"/>
<substitution expression="BOOOO"/>
<fileset dir=".">
<include name="*.htm"/>
</fileset>
</replaceregexp>
</target>
So far this isn't working, that is, the target that contains unless="isConcept"
is functioning (the "BOOOO" is appearing in the result), whereas if the property is set, it shouldn't fire.
I've used unless
for other purposes but only in cases where it was based on a flag in the command that starts the ANT build. From what I've read however, this looks like it should work. What do I have wrong?
UPDATE II
Okay this is working beautifully, based on Stefan's answer:
<target name="Concept3" description="Insert closing body tag" depends="Concept2,Concept1,Concept0">
<replaceregexp byline="false" flags="gs">
<regexp pattern="(<)(/body)(>)"/>
<substitution expression="\1\/conbody>"/>
<fileset dir=".">
<include name="*.htm"/>
<not>
<contains text="TaskChar"/>
</not>
<not>
<contains text="RefChar"/>
</not>
</fileset>
</replaceregexp>
</target>
<target name="Task3" description="" depends="Task2,Task1,Task0">
<!-- Insert closing body tag-->
<replaceregexp byline="false" flags="gs">
<regexp pattern="(<)(/body)(>)"/>
<substitution expression="\1\/taskbody>"/>
<fileset dir=".">
<include name="*.htm"/>
<not>
<contains text="ConceptStyle"/>
</not>
<not>
<contains text="RefChar"/>
</not>
</fileset>
</replaceregexp>
</target>
<target name="Ref3" description="" depends="Ref2,Ref1,Ref0">
<!-- Insert closing body tag-->
<replaceregexp byline="false" flags="gs">
<regexp pattern="(<)(/body)(>)"/>
<substitution expression="\1\/refbody>"/>
<fileset dir=".">
<include name="*.htm"/>
<not>
<contains text="ConceptStyle"/>
</not>
<not>
<contains text="TaskChar"/>
</not>
</fileset>
</replaceregexp>
</target>
Each target operates on a fileset filtered for a string that mustn't appear in the file in order for the target to act on it. There are three style type names, and my source files include only one of the three (by design), and this now triggers the ANT build to handle each file differently depending on which type it is. Great.
First of all it doesn't really look as if you needed a regex at all, a simple <contains>
would probably do it - unless there are files where "ConceptStyle" appears outside of the the body.
AFAIU you want to perform the substitution on all files unless they contain ConceptStyle. Rather than using condition
and attributes on the target
I'd recommend filtering the fileset
instead.
<replaceregexp byline="false" flags="gs">
<regexp pattern="conbody"/>
<substitution expression="BOOOO"/>
<fileset dir=".">
<include name="*.htm"/>
<not>
<contains text="ConceptStyle">
</not>
</fileset>
</replaceregexp>
If you really need the regex, then use <containsregexp>
instead.