Rewriting this now that I know what's going on more clearly:
I've got some Ant propertyregex tasks that extract three properties from image links in files. Then I want to use these to rewrite the links.
What I have so far extracts the properties I need, and then if I follow that with a replaceregex task I can use the properties to rewrite the links. However, even though the replaceregex will operate one line at a time, finishing one, then starting over at the next one, it takes the properties that were extracted from the first link and uses them as the replacements in every link in the file.
Is there a way to combine these two, so that the entire operation starts at one link, extracts properties, rewrites it, then moves to the next link and starts everything over?
Here's what I've got:
<project name="BuildModule" basedir="." default="extract.stuff">
<taskdef resource="net/sf/antcontrib/antlib.xml" />
<property environment="env" />
<loadfile property="file" srcfile="${basedir}/inputLog.log"/>
<target name="extract.stuff">
<for param="line" delimiter="${line.separator}" list="${file}">
<sequential>
<propertyregex property="imageName" input="@{line}" regexp="(<img class.*?graphics/)(.*?jpg|png)" select="\2" />
<propertyregex property="width" input="@{line}" regexp="(<img class)(.*?)(width.*?)(alt)" select="\3" />
<propertyregex property="alt" input="@{line}" regexp="(<img class)(.*?)(width.*?)(alt=)(.*?)(/>)" select="\5" />
</sequential>
</for>
<replaceregexp byline="true">
<regexp pattern="(<img class)(.*?$)"/>
<substitution expression="\<ac:image ac:title=${alt} ac:${width}><ri:attachment ri:filename="${imageName}"><ri:page ri:content-title="acme_shared_graphics" /></ri:attachment></ac:image></p>"/>
<fileset dir=".">
<include name="**.log"/>
</fileset>
</replaceregexp>
</target>
The links in the input file are like this:
<p class="p"><img class="image" id="concept_kdv_4zf_k4__image_qt1_pvc_q4" src="../../../shared_assets/acme_shared_graphics/acme_image_test.jpg" width="400" alt="Some alt title text"/></p>
and the result is this:
<p class="p"><ac:image ac:title="Some alt title text" ac:width="400" ><ri:attachment ri:filename="acme_image_test.jpg"><ri:page ri:content-title="acme_shared_graphics" /></ri:attachment></ac:image></p>
Once I get this working, I'm hoping that the (.*?jpg|png)
option will work as well, extracting either image_name.jpg
or image_name.png
depending on which the link contains, then replacing it the same way.
UPDATE
Here's what I have now, after the help here:
<project name="BuildModule" basedir="." default="extract.stuff">
<taskdef resource="net/sf/antcontrib/antlib.xml" />
<property environment="env" />
<loadfile property="file" srcfile="${basedir}/inputLog.log"/>
<target name="extract.stuff">
<for param="line" delimiter="${line.separator}" list="${file}">
<sequential>
<propertyregex override="true" property="imageName" input="@{line}" regexp="(<img class.*?graphics/)(.*?jpg|png)" select="\2" />
<propertyregex override="true" property="width" input="@{line}" regexp="(<img class)(.*?)(width.*?)(alt)" select="\3" />
<propertyregex override="true" property="alt" input="@{line}" regexp="(<img class)(.*?)(width.*?)(alt=)(.*?)(/>)" select="\5" />
<replaceregexp byline="true">
<regexp pattern="(<img class)(.*?$)"/>
<substitution expression="\<ac:image ac:title=${alt} ac:${width}><ri:attachment ri:filename="${imageName}"><ri:page ri:content-title="BSW_shared_graphics" /></ri:attachment></ac:image></p>"/>
<!--
<fileset dir=".">
<include name="**.log"/>
</fileset>
-->
</replaceregexp>
</sequential>
</for>
</target>
Commenting out the <fileset></fileset>
in the <replaceregexp>
is an attempt to make everything run according to input specified in the <for>
loop, since the problem was that the <replaceregexp>
using a fileset was doing the entire file, line by line, before returning to the <propertyregex>
section. However now, without the <fileset>
, the build just ignores the <replaceregexp>
section. If I put <fileset>
back in, it acts as before, changing each property to the first encountered in the file.
Adding override to the <propertyregex>
fixed the problem about retaining the property, so I don't need <unset>
.
the <for>
loop has ended before <replaceregexp>
therefore you aren't really extracting values for ${imageName}, ${width} and ${alt}
for every line for the <replaceregexp>
task. thus, they aren't changing and would probably retain the values of the FIRST line gone through by <propertyregex>
due to the missing override="true"
.
therefore, either include the <replaceregexp>
task within <for><sequential></..></..>
, as shown in the next answer, or, instead of specifying imageName, width and alt
in the <substitution>
expression... specify groups, such as \2, \5, \X..
and replace the <regex>
pattern string with one that incorporates the entire line of the file.
such as:
<regexp pattern="<img class.*?/(.*?graphics)/(.*?)" width="(.*?)" -alt="(.*?)"/>"/>
<substitution expression="\<ac:image ac:title="\4" ac:"\3"><ri:attachment ri:filename="\2"><ri:page ri:content-title="\1" /></ri:attachment></ac:image></p>"/>
EDIT:
propertyregex
already allows for mutability of properties. thus, imageName, alt and width
are fine and can be changed (just add override="true"
as an attribute - <propertyregex ... override="true".../>
and you wont need any unset="true"
).
the reason you were getting only the first line values for every line
for imageName, alt and width
is that <propertyregex>
was missing the override="true"
, therefore the first time these properties were being set, they remained unchanged in the subsequent loops of <for>
so now,
-- include <replaceregexp>
within <sequential>
.
-- add override
to all three properties: <propertyregex .. override="true".. />
(and also, your current <replaceregexp>
runs for all the files in the <fileset>
. if you include it within <sequential>
make sure to avoid irrelevant files in the fileset basedir
from getting picked up by the <include name= regex>
)
and it should work.
OR, try the grouping
method i mentioned. i would have gone with this one.
here, i think, you wont need any <for> & <propertyregexp>
at all. just run (within the target
) only with the <replaceregexp>
task over the entire <fileset>
, like you have, BUT change the <regex pattern
and <substitution expression
to accommodate groupings.
infact, if you look closely, all that <propertyregex>
does is select the relevant groupings
from its input lines and your-implementation-of-<replaceregexp>
replaces those values in the files/lines. combine these two by using groupings in the <replaceregexp>
task itself.
refer HERE for more on <replaceregexp>
.