现在重写一遍,我知道更清楚的是:
我有一些Ant propertyregex任务,它们从文件中的图像链接中提取三个属性。然后我想用这些来重写链接。
我到目前为止提取了我需要的属性,然后如果我使用replaceregex任务,我可以使用属性重写链接。但是,即使replaceregex一次只运行一行,完成一行,然后从下一行开始,它将从第一个链接中提取属性,并将它们用作文件中每个链接的替换。 / p>
有没有办法将这两者结合起来,以便整个操作从一个链接开始,提取属性,重写它,然后移动到下一个链接并开始一切?
以下是我所拥有的:
<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>
输入文件中的链接如下:
<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>
结果如下:
<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>
一旦我开始工作,我希望(.*?jpg|png)
选项也能正常运行,根据链接包含的内容提取image_name.jpg
或image_name.png
,然后替换同样的方式。
更新
这是我现在所拥有的,在这里的帮助之后:
<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>
评论<fileset></fileset>
中的<replaceregexp>
是尝试根据<for>
循环中指定的输入运行所有内容,因为问题在于<replaceregexp>
使用文件集在返回<propertyregex>
部分之前逐行执行整个文件。但是现在,如果没有<fileset>
,构建只会忽略<replaceregexp>
部分。如果我重新放入<fileset>
,它就像以前一样,将每个属性更改为文件中遇到的第一个属性。
向<propertyregex>
添加覆盖修复了有关保留该属性的问题,因此我不需要<unset>
。
答案 0 :(得分:0)
在Ant属性中,一旦设置为不可变,因此您需要首先使用antcontrib unset取消设置属性。然后你将获得你的replaceregexp内容for循环:
<for param="line" delimiter="${line.separator}" list="${file}">
<sequential>
<var name="imageName" unset="true"/>
<var name="width" unset="true"/>
<var name="alt" unset="true"/>
<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" />
<replaceregexp>
<!-- ... -->
</replaceregexp>
</sequential>
</for>
答案 1 :(得分:0)
<for>
循环已在<replaceregexp>
之前结束,因此您并未真正为${imageName}, ${width} and ${alt}
任务的每一行提取<replaceregexp>
的值。因此,由于缺少<propertyregex>
,它们可能会保留override="true"
经过的 FIRST 行的值。
因此,请在<replaceregexp>
中包含<for><sequential></..></..>
任务,如下一个答案中所示,或者,而不是在imageName, width and alt
表达式中指定<substitution>
...指定组,例如\2, \5, \X..
,并将<regex>
模式字符串替换为包含整个文件行的模式字符串。
如:
<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>"/>
编辑:
propertyregex
已允许属性的 可变性 。因此,imageName, alt and width
很好,可以更改( 只需添加override="true"
作为属性 - <propertyregex ... override="true".../>
,您就不需要任何unset="true"
强>)。
first line values for every line
只imageName, alt and width
<propertyregex>
override="true"
<for>
<replaceregexp>
错过了<sequential>
,因此第一次设置这些属性时,它们仍然< override
的后续循环中强>未变
现在,
- 在<propertyregex .. override="true".. />
中加入<replaceregexp>
- 将<fileset>
添加到所有三个属性:<sequential>
(还有,您当前的fileset basedir
针对{{1>}中的 所有 文件运行。如果您将其包含在<include name= regex>
中确保避免grouping
中的无关文件被<for> & <propertyregexp>
提取
它应该有效。
OR,尝试我提到的target
方法。我会选择这个。
在这里,我想,你 根本不需要任何 <replaceregexp>
。只需在整个<fileset>
上执行<regex pattern
仅 <substitution expression
任务,就像您一样,但更改<propertyregex>
和{ {1}}以容纳 分组 。
事实上,如果仔细观察,relevant groupings
所做的只是从输入行中选择 <replaceregexp>
,并且您的实现 - <replaceregexp>
取代文件/行中的那些值。 使用<replaceregexp>
任务中的分组将这两者结合起来。
有关{{1}}的更多信息,请参阅HERE。