【问题标题】:Ant task capture image extensions using jpg|pngAnt 任务使用 jpg|png 捕获图像扩展
【发布时间】:2023-03-27 16:19:02
【问题描述】:

现在我更清楚地知道发生了什么,所以重写这个:

我有一些 Ant propertyregex 任务,它们从文件中的图像链接中提取三个属性。然后我想用这些来重写链接。

到目前为止,我已经提取了我需要的属性,然后如果我按照 replaceregex 任务进行操作,我可以使用这些属性来重写链接。然而,即使 replaceregex 将一次运行一行,完成一个,然后从下一个重新开始,它也会使用从第一个链接中提取的属性并将它们用作文件中每个链接的替换。

有没有办法将这两者结合起来,让整个操作从一个链接开始,提取属性,重写它,然后移动到下一个链接并重新开始?

这是我得到的:

<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="(&lt;img class.*?graphics/)(.*?jpg|png)" select="\2" />
            <propertyregex property="width" input="@{line}" regexp="(&lt;img class)(.*?)(width.*?)(alt)" select="\3" />
            <propertyregex property="alt" input="@{line}" regexp="(&lt;img class)(.*?)(width.*?)(alt=)(.*?)(/&gt;)" select="\5" />

        </sequential>
    </for>


     <replaceregexp byline="true">
      <regexp pattern="(&lt;img class)(.*?$)"/>
     <substitution expression="\&lt;ac:image ac:title=${alt} ac:${width}&gt;&lt;ri:attachment ri:filename=&quot;${imageName}&quot;&gt;&lt;ri:page ri:content-title=&quot;acme_shared_graphics&quot; /&gt;&lt;/ri:attachment&gt;&lt;/ac:image&gt;&lt;/p&gt;"/>
     <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.jpgimage_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="(&lt;img class.*?graphics/)(.*?jpg|png)" select="\2" />
        <propertyregex override="true" property="width" input="@{line}" regexp="(&lt;img class)(.*?)(width.*?)(alt)" select="\3" />
        <propertyregex override="true" property="alt" input="@{line}" regexp="(&lt;img class)(.*?)(width.*?)(alt=)(.*?)(/&gt;)" select="\5" />  

        <replaceregexp byline="true">
        <regexp pattern="(&lt;img class)(.*?$)"/>
        <substitution expression="\&lt;ac:image ac:title=${alt} ac:${width}&gt;&lt;ri:attachment ri:filename=&quot;${imageName}&quot;&gt;&lt;ri:page ri:content-title=&quot;BSW_shared_graphics&quot; /&gt;&lt;/ri:attachment&gt;&lt;/ac:image&gt;&lt;/p&gt;"/>

        <!-- 
        <fileset dir=".">
        <include name="**.log"/>
        </fileset>
        -->

        </replaceregexp>

        </sequential>

    </for>

</target>

注释掉&lt;replaceregexp&gt; 中的&lt;fileset&gt;&lt;/fileset&gt; 是为了让一切都根据&lt;for&gt; 循环中指定的输入运行,因为问题在于使用文件集的&lt;replaceregexp&gt; 正在执行整个文件,逐行,然后返回&lt;propertyregex&gt; 部分。然而现在,没有&lt;fileset&gt;,构建只是忽略&lt;replaceregexp&gt; 部分。如果我把&lt;fileset&gt; 放回去,它就像以前一样,将每个属性更改为文件中遇到的第一个属性。

将覆盖添加到&lt;propertyregex&gt; 解决了保留属性的问题,所以我不需要&lt;unset&gt;

【问题讨论】:

  • However now, without the &lt;fileset&gt;, the build just ignores the &lt;replaceregexp&gt; section.. 那是因为你没有给&lt;replaceregexp&gt;任务提供任何输入匹配-n-replace&lt;fileset&gt; 之前以文件形式提供输入以进行操作)。所以,这样做:&lt;replaceregexp file="${basedir}/inputLog.log" byline="true"&gt;..&lt;/..&gt; 或删除 &lt;fileset&gt;可选 更改为 &lt;include name="inputLog.log/&gt;
  • 我建议早先删除&lt;fileset&gt; 以避免处理那里的额外文件(相关的inputLog.log 和此类文件除外)。如果一个/多个文件需要替换,请注意&lt;include&gt; 标记,尽量避免驻留在fileset basedir 中的不需要的.log 文件。
  • 您可以将regex pattern 更改为直接匹配@{line}: &lt;replaceregexp file="..." byline="true"&gt; &lt;regexp pattern="@{line}"/&gt;... 并包含override="true"
  • 另外,我必须说这种方法可行(可能需要稍作调整),但我不会向您推荐。 &lt;propertyregex&gt;${file} 上循环,在&lt;for&gt; 循环的每个 迭代中,&lt;replaceregexp&gt; 扫描它的输入文件 i> 逐行直到达到匹配/文件结尾。 分组方法可以完全避免这些额外的迭代
  • 好的,感谢您的帮助。我现在知道了。我将使用分组方法——事实上,在仅使用 replaceregex 发布问题之前,我已经有了一个解决方案,不涉及属性,但正如我所说,我想学习如何使用属性来做到这一点,或者看看我是否可以。我最初的解决方案采用了多个 replaceregex 操作,所以你的分组想法更好,我会使用它。我将继续尝试使用属性方法,因为这是一种学习体验。再次感谢。

标签: regex ant ant-contrib


【解决方案1】:

&lt;for&gt; 循环在&lt;replaceregexp&gt; 之前结束,因此您并没有真正为&lt;replaceregexp&gt; 任务的每一行提取${imageName}, ${width} and ${alt} 的值。因此,它们不会改变,并且可能会保留 &lt;propertyregex&gt; 所经过的 FIRST 行的值,因为缺少 override="true"

因此,或者在&lt;for&gt;&lt;sequential&gt;&lt;/..&gt;&lt;/..&gt; 中包含&lt;replaceregexp&gt; 任务,如下一个答案所示,或者,而不是在&lt;substitution&gt; 表达式中指定imageName, width and alt...指定组,例如\2, \5, \X.. 和将&lt;regex&gt; 模式字符串替换为包含文件整行的字符串。

如:

<regexp pattern="&lt;img class.*?/(.*?graphics)/(.*?)&quot; width=&quot;(.*?)&quot; -alt=&quot;(.*?)&quot;/&gt;"/>
<substitution expression="\&lt;ac:image ac:title=&quot;\4&quot; ac:&quot;\3&quot;&gt;&lt;ri:attachment ri:filename=&quot;\2&quot;&gt;&lt;ri:page ri:content-title=&quot;\1&quot; /&gt;&lt;/ri:attachment&gt;&lt;/ac:image&gt;&lt;/p&gt;"/>

编辑:

propertyregex 已经允许属性可变性。因此,imageName, alt and width 很好并且可以更改(只需添加 override="true" 作为属性 - &lt;propertyregex ... override="true".../&gt; 并且您不需要任何 unset="true")。

您只获得imageName, alt and widthfirst line values for every line 的原因是&lt;propertyregex&gt; 缺少override="true",因此第一次设置这些属性时,它们保持不变 &lt;for&gt; 的后续循环 所以现在,

-- 在&lt;sequential&gt; 中包含&lt;replaceregexp&gt;
-- 将override 添加到所有三个属性中:&lt;propertyregex .. override="true".. /&gt;

(而且,您当前的&lt;replaceregexp&gt; 会针对&lt;fileset&gt; 中的所有文件运行。如果您将其包含在&lt;sequential&gt; 中,请确保避免不相关fileset basedir 中的文件被 &lt;include name= regex&gt; 拾取)

它应该可以工作。

或者,尝试我提到的grouping 方法。我会和这个一起去的。 在这里,我想,你 根本不需要任何 &lt;for&gt; &amp; &lt;propertyregexp&gt;。只需在整个&lt;fileset&gt; 上运行(在target 内) 使用&lt;replaceregexp&gt; 任务,就像您一样,但是更改&lt;regex pattern&lt;substitution expression 以适应分组

事实上,如果你仔细观察,&lt;propertyregex&gt; 所做的只是从其输入行中选择 relevant groupings 并且 your-implementation-of-&lt;replaceregexp&gt; 替换文件中的这些值/线。 通过在&lt;replaceregexp&gt; 任务本身中使用分组来组合这两者。

有关&lt;replaceregexp&gt; 的更多信息,请参阅HERE

【讨论】:

  • 非常感谢,请参阅下面的我的 cmets。我也可能会尝试您的第二种解决方案,但我很想知道我是否可以让变量起作用,因为我可以在其他地方使用该策略,也只是为了我自己的学习目的。
  • unset 可用作&lt;var&gt; 任务的属性。您需要包含 ant-contrib.jar。例如:&lt;property name="x" value="6"/&gt; &lt;echo&gt;${x}&lt;/echo&gt; &lt;!-- will print 6 --&gt; &lt;var name="x" unset="true"/&gt;&lt;property name="x" value="12"/&gt; &lt;echo&gt;${x}&lt;/echo&gt; &lt;!-- will print 12 --&gt; for var & unsetant-contrib.sourceforge.net/tasks/tasks/variable_task.html for ant-contrib 在这里:ant-contrib.sourceforge.net
  • Thx,我意识到,在我再次阅读可变任务页面后。好的,所以我似乎将 ant-contrib jar 作为资源,因为propertyregex 是一个 ant-contrib 任务,它从一开始就一直在工作。如果我在我的项目中打开&lt;sequential&gt; 之后插入&lt;var name="imageName" unset="true"/&gt;,或者在&lt;sequential&gt;&lt;/sequential&gt; 内的任何其他地方插入,它似乎并没有取消设置变量,行为和以前一样。如果我将其插入&lt;sequential&gt;&lt;/sequential&gt; 之外的任何位置,我会收到错误。
  • &lt;var name="imageName" unset="true"/&gt; 似乎正在运行;作为测试,我在&lt;propertyregex property="imageName"[....] 行之后插入了它,这样构建将图像名替换为"${imageName}" 而不是图像文件名。也就是说,它似乎确实取消了在它之前的行中设置的内容。我只需要在属性命名之前放置它,但到目前为止还没有工作。
  • 再次感谢。关于:[...]&lt;replaceregexp&gt; ... change it to serve each line at a time due to the &lt;for&gt; loop[...] 怎么样?我也一直想知道,所以我从&lt;replaceregexp&gt; 组中删除了所有&lt;fileset&gt;[...]&lt;/fileset&gt;,但它只是运行&lt;propertyregex&gt;' part. ` 现在在&lt;sequential&gt; 内部,因此也在&lt;for&gt;
【解决方案2】:

在 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="(&lt;img class.*?graphics/)(.*?jpg|png)" select="\2" />
      <propertyregex property="width" input="@{line}" regexp="(&lt;img class)(.*?)(width.*?)(alt)" select="\3" />
      <propertyregex property="alt" input="@{line}" regexp="(&lt;img class)(.*?)(width.*?)(alt=)(.*?)(/&gt;)" select="\5" />

      <replaceregexp>
       <!-- ... -->
      </replaceregexp>

     </sequential>
    </for>

【讨论】:

  • 谢谢,我曾尝试将 replaceregexp 放入 &lt;for&gt;&lt;/for&gt;,但出现错误,但在阅读了您的回复和另一个回复后,我意识到它也需要在 &lt;sequential&gt; 中。 \
  • (编辑时间用完...)但是现在,当使用您的建议时,我得到Failed to create type or task unset. The name is undefined. 这是否意味着unset,或者我猜variable 任务不是包含在我的 ant-contrib 库中?我将 Ant 版本作为 Oxygen XML 的一部分安装,里面的 Ant-contrib jar 似乎是 1.0b3,不是最新版本,而是之前的版本。我有点困惑,试图弄清楚如何用下载替换它,我在最新下载的 1.0b5 中没有看到 .jar 文件。
  • 哎呀,对不起 - 当然它必须是 而不是 => 在我的答案中修复它。
猜你喜欢
  • 1970-01-01
  • 2022-01-11
  • 2015-08-21
  • 1970-01-01
  • 2018-11-30
  • 1970-01-01
  • 2021-06-29
  • 2021-07-29
  • 1970-01-01
相关资源
最近更新 更多