首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ant macrodef:有没有办法获取元素参数的内容?

Ant macrodef:有没有办法获取元素参数的内容?
EN

Stack Overflow用户
提问于 2010-07-23 20:59:19
回答 1查看 6.3K关注 0票数 9

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

代码语言:javascript
复制
<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>

示例运行:

代码语言:javascript
复制
$ 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树。有没有人知道其中任何一个的实现?当然,任何第三种、意想不到的获取数据的方式也是受欢迎的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-07-24 07:19:43

那么echoxml任务呢?

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

代码语言:javascript
复制
<echo>The element works only in restricted cases: <elem /> </echo>

使用

代码语言:javascript
复制
<echoxml><elem /></echoxml>

结果:

代码语言:javascript
复制
$ 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声明并不是必需的。您可以使用file属性将输出放到一个临时文件中,然后读取该文件并删除声明,或者根据需要重新格式化信息。

编辑

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

代码语言:javascript
复制
<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>

给出

代码语言:javascript
复制
$ 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>
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3318323

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档