Ant macrodef:有没有办法获取元素参数的内容? [英] Ant macrodef: Is there a way to get the contents of an element parameter?

查看:24
本文介绍了Ant macrodef:有没有办法获取元素参数的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Ant 中调试一个宏定义.我似乎找不到一种方法来显示作为元素发送的参数的内容.

I'm trying to debug a macrodef in Ant. I cannot seem to find a way to display the contents of a parameter sent as an element.

<project name='debug.macrodef'>
  <macrodef name='def.to.debug'>
    <attribute name='attr' />
    <element name='elem' />
    <sequential>
      <echo>Sure, the attribute is easy to debug: @{attr}</echo>
      <echo>The element works only in restricted cases: <elem /> </echo>
      <!-- This works only if <elem /> doesn't contain anything but a
           textnode, if there were any elements in there echo would
           complain it doesn't understand them. -->
    </sequential>
  </macrodef>

  <target name='works'>
    <def.to.debug attr='contents of attribute'>
      <elem>contents of elem</elem>
    </def.to.debug>
  </target>

  <target name='does.not.work'>
    <def.to.debug attr='contents of attribute'>
      <elem><sub.elem>contents of sub.elem</sub.elem></elem>
    </def.to.debug>
  </target>
</project>

示例运行:

$ ant works
...
works:
 [echo] Sure, the attribute is easy to debug: contents of attribute
 [echo] The element works only in restricted cases:  contents of elem
...

$ ant does.not.work
...
does.not.work:
 [echo] Sure, the attribute is easy to debug: contents of attribute

BUILD FAILED
.../build.xml:21: The following error occurred while executing this line:
.../build.xml:7: echo doesn't support the nested "sub.elem" element.
...

所以我想我需要一种方法来以某种方式将 <elem/> 的内容放入一个属性中(某些扩展的宏定义实现可能有),或者我需要一种<element-echo><elem/></element-echo> 可以打印出您放入的任何 XML 树.有谁知道其中任何一个的实现?当然,也欢迎采用任何第三种出乎意料的方式来获取数据.

So I guess I need either a way to get the contents of the <elem /> into a property somehow (some extended macrodef implementation might have that), or I need a sort of <element-echo><elem /></element-echo> that could print out whatever XML tree you put inside. Does anyone know of an implementation of either of these? Any third, unanticipated way of getting the data out is of course also welcome.

推荐答案

<代码>echoxml 任务?

在您的示例构建文件中替换该行

In your example build file replacing the line

<echo>The element works only in restricted cases: <elem /> </echo>

<echoxml><elem /></echoxml>

结果

$ ant does.not.work
...
does.not.work:
     [echo] Sure, the attribute is easy to debug: contents of attribute
<?xml version="1.0" encoding="UTF-8"?>
<sub.elem>contents of sub.elem</sub.elem>

也许不需要 XML 声明.您可以使用 echoxml file 属性将输出放入临时文件,然后读取该文件并删除声明,或者根据需要重新格式化信息.

Perhaps the XML declaration is not wanted though. You might use the echoxml file attribute to put the output to a temporary file, then read that file and remove the declaration, or reformat the information as you see fit.

编辑

经过反思,您可能会接近您所描述的内容,例如您的宏定义的连续主体

On reflection, you can probably get close to what you describe, for example this sequential body of your macrodef

<sequential>
  <echo>Sure, the attribute is easy to debug: @{attr}</echo>
  <echoxml file="macro_elem.xml"><elem /></echoxml>
  <loadfile property="elem" srcFile="macro_elem.xml">
    <filterchain>
      <LineContainsRegexp negate="yes">
      <regexp pattern=".xml version=.1.0. encoding=.UTF-8..." />
      </LineContainsRegexp>
    </filterchain>
  </loadfile>
  <echo message="${elem}" />
</sequential>

给予

$ ant does.not.work
...
does.not.work:
     [echo] Sure, the attribute is easy to debug: contents of attribute
     [echo] <sub.elem>contents of sub.elem</sub.elem>

这篇关于Ant macrodef:有没有办法获取元素参数的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆