2013-03-08 8 views
0

는 I은 X-POS = "NN" 및 Y-POS NN이 양수 또는 음수 = "NN" 같은 특성 (다른 사람들)와 XML 스트링을 갖는다. 모든 값을 읽고이를 산술 제품으로 변경하려고합니다. # {NN * 15}, 즉 x-pos = "3"은 x_pos = "45"로 변경됩니다.각 xml 숫자 속성을 ant 또는 ant 확장명을 사용하는 regexp에 의한 문자열의 산술 곱으로 바꿉니 까?

은 따라서 나는 이런 식으로 뭔가가 필요합니다

<ant-contrib:propertyregex property="xval" 
    input="${xmlfile.contents}" 
    regexp="x-pos\s*=\s*&quot;([0-9\-]+)&quot;" 
    replace="x-pos=&quot;_TRICKY_EXPR_EVALUATOR_{\1 * 15}&quot;" 
    override="true" global="yes"/> 

을 아니면 어떻게 든 모든 /의 X-POS의 \ S * = \는 \ *이야 캡처 할 수 있습니다 "([0-9 -] +) \"/matches (PHP preg_match_all 함수와 동일) flaka 목록이나 '-'로 구분 된 문자열에서 가져옵니다. 일단 내가 그것을 분할 할 수 있으며 그것을 통해 반복하여 각 값을 '수동으로'대체 할 수 있습니다.

perl과 같은 정규 표현식에서 작동하는 다른 개미 확장 프로그램이 있습니까? 나는 flaka와 ant-contrib을 알았지 만 도움이되지 못합니다.

의견을 보내 주셔서 감사합니다. 갱신

:

<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

당신이 정규 표현식을 사용하지 말아야 XML을 구문 분석 : 여기에 코드입니다. xmltask (http://www.oopsconsultancy.com/software/xmltask/index.html)를 사용하고 xpath를 통해 모든 x-pos 노드 목록을 가져옵니다. 이후에 해당 목록을 반복하기가 쉽습니다. 답을 수정하고 xmlstructure를 보여 주면 해결책을 게시 할 것입니다. – Rebse

+0

@Rebse, 요점은 XML 구조가 중요하지 않다는 것입니다. 그것은 하나의 태스크가 아니라 XML 클래스를위한 태스크 클래스입니다. propertyregex를 사용하여 구조 고유의 솔루션을 알고 있습니다. 문자열로 문자열을 분석하고 속성으로 속성을 분석합니다. 그러나 보편적 인 것은 아니며, flaka list (s)를 만들 수있는 확장이 있다면 훨씬 더 쉽습니다. 어쨌든, 당신의 구체적인 해결책을보고 싶습니다. 그래서 나는 내 질문을 업데이트 할 것이다. – Tertium

+0

마침내 내가 몇 가지 예를 게시 할 시간이있어, 내 대답을 참조하십시오 - HTH – Rebse

답변

1

을 아마 때 사용 이미 flaka를 사용하여 :

#{ x * y} 

:

<!-- Activate flaka for all ant tasks --> 
<fl:install-property-handler/> 

가 함께 여기 는 XML의 hypotetic 조각 구문 분석하는 것입니다 어떻게 든 당신을 위해 일할 것입니다. 내 컴퓨터에 antcontrib가 설치되어 있지 않으므로 테스트 해보십시오.
속성 처리기를 사용하면 모든 개미 작업에서 EL 표현식을 사용할 수 있습니다. 여기


는 주어진 파일 foo.xml있는 작은 예입니다 xmltaskflaka 필요 :

<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> 

출력 :

장소에

<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을

[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에서 시작하여 개미의 속성이 변경되지 않으므로 안전하게 무시할 수 있습니다. 또는 소스에서 더 이상 삭제하지 말고 xmltask.jar을 다시 작성하십시오.

+0

해결책을위한 탁구. 그것이 원시적이며 많은 유스 케이스를 다루지는 않지만, 게시 해 주시면 감사하겠습니다. xmltask는 새 것처럼 보이고 나에게 약속합니다. – Tertium

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="&lt;resprops&gt;" /> 
    </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*&quot;([0-9\-]+)&quot;" 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*&quot;([0-9\-]+)&quot;" 
            replace="pos-x = &quot;#{gotv}&quot;" 
            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> 

+1

복잡해지면 실제 프로그래밍 언어가 필요합니다. Groovy를 사용하여 개미 => http://groovy.codehaus.org/The+groovy+Ant+Task – Rebse

+0

에서 첫 번째로 보이는 것은 고맙습니다. 고맙습니다. , 마음과 북마크에 계속됩니다! – Tertium