我有一个xml字符串,其中包含 x-pos =“NN”和 y-pos =“NN”等属性,其中 NN 是正数或负数。 我想读取每个值并将其更改为其算术产品 - 评估#{NN * 15},即x-pos =“3”将更改为x_pos =“45”。
因此我需要这样的事情:
<ant-contrib:propertyregex property="xval"
input="${xmlfile.contents}"
regexp="x-pos\s*=\s*"([0-9\-]+)""
replace="x-pos="_TRICKY_EXPR_EVALUATOR_{\1 * 15}""
override="true" global="yes"/>
或许我可以以某种方式捕获所有/ x-pos \ s * = \ s * \“([0-9 - ] +)\”/匹配(就像在PHP preg_match_all函数中一样)并将它们放入flaka list或 - 比如 - 用';'分隔的字符串?一旦我拥有它,我可以拆分它,并迭代它以“手动”替换每个值。
是否有任何其他ant扩展适用于类似perl的正则表达式?我了解了flaka和ant-contrib,但那些无济于事。
感谢您的想法!
更新: 这是一个解析的xml的hypotetic片段:
<sprite name="timer" xref="" pos-x="25" pos-y="4" path="img/folder1/img1.jpg" />
<sprite name="timer1" xref="" pos-x="25" pos-y="4" offset-x="100" offset-y="10" path="img/folder1/img2.jpg" />
<control name="timer2" xref="" pos-x="25" pos-y="4" size="100" offset-y="10" path="img/folder1/img2.jpg" />
答案 0 :(得分:1)
使用flaka时可能已经使用了:
<!-- Activate flaka for all ant tasks -->
<fl:install-property-handler/>
结合:
#{ x * y}
会以某种方式为你工作,没有测试,因为我的机器上没有安装antcontrib
属性处理程序允许在所有ant任务中使用EL表达式。
这是一个给定文件foo.xml的小例子,需要xmltask和flaka:
<whatever>
<sprite name="timer" path="img/folder1/img1.jpg" pos-x="25" pos-y="4" xref=""/>
<sprite name="timer1" offset-x="100" offset-y="10" path="img/folder1/img2.jpg" pos-x="26" pos-y="4" xref=""/>
<control name="timer2" offset-y="10" path="img/folder1/img2.jpg" pos-x="27" pos-y="4" size="100" xref=""/>
</whatever>
编辑foo.xml:
<project xmlns:fl="antlib:it.haefelinger.flaka">
<!-- Activate flaka for all ant tasks -->
<fl:install-property-handler/>
<!-- Import XMLTask -->
<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask"/>
<!-- get a list with all pos-x attribute values -->
<xmltask source="foo.xml">
<copy path="//whatever/*/@pos-x" append="true" propertySeparator="," property="posxlist"/>
</xmltask>
<echo>$${posxlist} => ${posxlist}</echo>
<fl:let>counter ::= 1</fl:let>
<!-- for loop with xmltask editing foo.xml in place => source = dest -->
<fl:for var="posx" in="split('${posxlist}', ',')">
<xmltask source="foo.xml" dest="foo.xml" report="true">
<!-- i.e. multiplicating value * 3 -->
<attr path="//whatever/*[${counter}]" attr="pos-x" value="#{posx * 3}"/>
</xmltask>
<fl:let>counter ::= '${counter}' + 1</fl:let>
</fl:for>
</project>
输出:
[xmltask] Cannot append values to properties
[xmltask] Cannot append values to properties
[xmltask] Cannot append values to properties
[echo] ${posxlist} => 25,26,27
[xmltask] Document -->
[xmltask] <whatever>
[xmltask] <sprite name="timer" path="img/folder1/img1.jpg" pos-x="75" pos-y="4" xref=""/>
[xmltask] <sprite name="timer1" offset-x="100" offset-y="10" path="img/folder1/img2.jpg" pos-x="26" pos-y="4" xref=""/>
[xmltask] <control name="timer2" offset-y="10" path="img/folder1/img2.jpg" pos-x="27" pos-y="4" size="100" xref=""/>
[xmltask] </whatever>
[xmltask] Document <--
[xmltask] Document -->
[xmltask] <whatever>
[xmltask] <sprite name="timer" path="img/folder1/img1.jpg" pos-x="75" pos-y="4" xref=""/>
[xmltask] <sprite name="timer1" offset-x="100" offset-y="10" path="img/folder1/img2.jpg" pos-x="78" pos-y="4" xref=""/>
[xmltask] <control name="timer2" offset-y="10" path="img/folder1/img2.jpg" pos-x="27" pos-y="4" size="100" xref=""/>
[xmltask] </whatever>
[xmltask] Document <--
[xmltask] Document -->
[xmltask] <whatever>
[xmltask] <sprite name="timer" path="img/folder1/img1.jpg" pos-x="75" pos-y="4" xref=""/>
[xmltask] <sprite name="timer1" offset-x="100" offset-y="10" path="img/folder1/img2.jpg" pos-x="78" pos-y="4" xref=""/>
[xmltask] <control name="timer2" offset-y="10" path="img/folder1/img2.jpg" pos-x="81" pos-y="4" size="100" xref=""/>
[xmltask] </whatever>
[xmltask] Document <--
BUILD SUCCESSFUL
Total time: 826 milliseconds
警告'无法将属性附加到属性'来自com.oopsconsultancy.xmltask.CopyAction第80行,以强调ant中的属性是不可变的并且可以安全地被忽略 - 或者甚至更好地从源中删除它并重建xmltask.jar < / p>
答案 1 :(得分:0)
这是我自己解决方案的一个片段。我不会将其标记为已接受,因为它无法解决一般的文本解析问题(如php的preg_match_all)。但也许有人觉得它很有趣。 它有点乱 - 我使用ant属性和flaka EL变量,但它说明了如何一起使用它们。 这是代码:
filename: process.xml
<!-- these includes are needed so that eclipse can load autocompletion base from plugins,
and they tell to ant where plugins' jars are (in 'ut' folder on the same level)-->
<taskdef uri="antlib:it.haefelinger.flaka" resource="it/haefelinger/flaka/antlib.xml" classpath="ut/ant-flaka.jar" />
<taskdef uri="antlib:net.sf.antcontrib" resource="net/sf/antcontrib/antlib.xml" classpath="ut/ant-contrib-1.0b3.jar" />
<!-- call: >> ant -Dfname="folder/with/xmls" -f process.xml correct-xmls -->
<target name="correct-xmls">
<fl:install-property-handler />
<property name="slashn" value="${line.separator}" />
<!-- get xmls - only with existing root resprops element -->
<fileset dir="${fname}" includes="**/*.xml" id="xml-classes">
<contains text="<resprops>" />
</fileset>
<fl:for var="xn" in="split('${toString:xml-classes}', ';')">
<fl:let>curfile = file(concat('${fname}','/',xn))</fl:let>
<fl:let>tgtfile = file(concat('${fname}','/',xn,'.new'))</fl:let>
<fl:echo>#{ format('file %s, last modified %tD, size: %d',
curfile.path, curfile.mtime, curfile.isdir ? 0 : curfile.size) }</fl:echo>
<fl:unset>xmlfile.contents</fl:unset>
<loadfile property="xmlfile.contents" srcFile="#{curfile}" />
<fl:let>outstr = ''</fl:let>
<fl:for var="str" in="split('${xmlfile.contents}', '\n')">
<fl:unset> xval </fl:unset>
<ac:propertyregex property="xval" input="#{str}" regexp="pos-x\s*=\s*"([0-9\-]+)"" select="\1" override="true" />
<!-- force set property 'resstr' to value of var 'str'-->
<fl:let>resstr ::= str</fl:let>
<!-- process only if pos-x is found and we have its value in 'xval' -->
<fl:when test="not empty '#{property.xval}'">
<fl:let>outval = (property.xval * 15.5 + 0.5) </fl:let>
<!-- kinda int-from-float -->
<ac:propertyregex property="gotv" input="#{outval}" regexp="([0-9\-]+)\." select="\1" override="true" />
<ac:propertyregex property="resstr"
input="${resstr}"
regexp="pos-x\s*=\s*"([0-9\-]+)""
replace="pos-x = "#{gotv}""
override="true"
/>
</fl:when>
<!-- add to output string by string -->
<fl:let> outstr = format('%s%s%s', outstr , resstr, slashn) </fl:let>
</fl:for>
<!--save processed file -->
<echo file="#{tgtfile}" encoding="utf-8">#{outstr}</echo>
</fl:for>
</target>