遍历数量不明的财产使用Ant和搜索和替换 [英] iterate over unknown number of properties using ant and search and replace
问题描述
我有一只蚂蚁属性,文件看起来像这样(尽管如果需要,我可以调整格式 - 我可以把它的XML文件,如果这是更合适):</ p>
libraries.properties
1名
http://www.url1.com?a=b
2名
http://www.url2.com?c=d
3名
http://www.url3.com?e=f
注:我知道这是不是一个有效的属性格式,因为它不是在形式A = B。我可以,因为它很容易在未来添加条目将其更改为任何合适的,只要
我想蚂蚁采取检查,在一个文件如下:
options.html(来源)
&LT;选择&GT;
&LT;期权价值=@ URL @&GT; @名@&LT; /选项&GT;
&LT; /选择&GT;
...和做搜索和替换的令牌的多次有名称/值项的在libraries.properties,所以造成options.html文件是这样的:
options.html(编译后)
&LT;选择&GT;
&LT;期权价值=http://www.url1.com?a=b&GT;名1 LT; /选项&GT;
&LT;期权价值=http://www.url2.com?c=d&GT;名2'; /选项&GT;
&LT;期权价值=http://www.url3.com?3=f&GT;名3'; /选项&GT;
&LT; /选择&GT;
由于与属性文件,该options.html源可以是不同的格式。我只需要定义什么是我的/复制的某种方式。
什么是做最彻底的方法?
谢谢!
您需要的是一个的模板引擎来生成HTML文件。
最接近此蚂蚁直接支持是 XSLT转换。
示例
以下项目
| - build.xml文件
` - SRC
| - options.xsl
` - properties.xml中
在运行生成一个HTML文件
| - 构建
| ` - options.html
请注意,这不是一个正确格式的HTML文件。您的规格看起来更像是设计导入到另一个文件中的片段。
properties.xml中
&LT;性状&gt;
&LT;性&gt;
&LT;名称&gt;姓名1 LT; /名称&gt;
&LT; VALUE&GT; HTTP://www.url1.com A = B&LT; /值&GT;
&LT; /性&gt;
&LT;性&gt;
&LT;名称&gt;姓名2'; /名称&gt;
&LT; VALUE&GT; HTTP://www.url2.com C = D&LT; /值&GT;
&LT; /性&gt;
&LT;性&gt;
&LT;名称&gt;姓名3'; /名称&gt;
&LT; VALUE&GT; HTTP://www.url3.com E = F&LT; /值&GT;
&LT; /性&gt;
&LT; /性状&gt;
options.xsl
&LT;的xsl:样式版本=1.0的xmlns:XSL =http://www.w3.org/1999/XSL/Transform&GT; &LT; XSL:输出方法=HTML/&GT; &LT;的xsl:模板匹配=/&GT;
&LT;选择&GT;
&LT; XSL:申请模板选择=属性/属性/&GT;
&LT; /选择&GT;
&LT; / XSL:模板&GT; &LT;的xsl:模板匹配=财产&GT;
&LT;期权价值={}值&GT;&LT;的xsl:value-of的选择=名/&GT; /选项&GT;&LT;
&LT; / XSL:模板&GT;&LT; / XSL:样式&GT;
的build.xml
&LT;项目名称=演示默认值=生成&GT; &lt;目标名称=初始化&GT;
&LT; MKDIR DIR =构建/&GT;
&LT; /目标与GT; &lt;目标名称=生成取决于=初始化&GT;
&LT; XSLT样式= =中的SRC / properties.xml中出=建立/ options.html/&GT的src / options.xsl
&LT; /目标与GT; &lt;目标名称=干净&GT;
&LT;删除DIR =构建/&GT;
&LT; /目标与GT;&LT; /项目&GT;
I have an ant "properties" file that looks like this (although I can adjust the format if needed - I can make it an XML file if that's more appropriate):
libraries.properties
name 1
http://www.url1.com?a=b
name 2
http://www.url2.com?c=d
name 3
http://www.url3.com?e=f
NOTE: I know this is not a valid properties format because it's not in the form a=b. I can change it to anything that's appropriate, as long as it's easy to add entries in future.
I want ant to take a file that is checked-in as follows:
options.html (source)
<select>
<option value="@URL@">@NAME@</option>
</select>
... and do a search and replace on the tokens as many times as there are name/value entries in libraries.properties, so the resulting options.html file would look like this:
options.html (after build)
<select>
<option value="http://www.url1.com?a=b">name 1</option>
<option value="http://www.url2.com?c=d">name 2</option>
<option value="http://www.url3.com?3=f">name 3</option>
</select>
As with the properties file, the options.html source can be a different format. I just need some way of defining what I copy from/to.
What's the cleanest way to do this?
Thanks!
What you need is a templating engine to generate your HTML file.
The closest thing to this that ANT directly supports is an XSLT transformation.
Example
The following project
|-- build.xml
`-- src
|-- options.xsl
`-- properties.xml
When run generates a single HTML file
|-- build
| `-- options.html
Note, this is not a properly formatted HTML file. Your specification looks more like a fragment designed to be imported into another file.
properties.xml
<properties>
<property>
<name>name 1</name>
<value>http://www.url1.com?a=b</value>
</property>
<property>
<name>name 2</name>
<value>http://www.url2.com?c=d</value>
</property>
<property>
<name>name 3</name>
<value>http://www.url3.com?e=f</value>
</property>
</properties>
options.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<select>
<xsl:apply-templates select="properties/property"/>
</select>
</xsl:template>
<xsl:template match="property">
<option value="{value}"><xsl:value-of select="name"/></option>
</xsl:template>
</xsl:stylesheet>
build.xml
<project name="demo" default="generate">
<target name="init">
<mkdir dir="build"/>
</target>
<target name="generate" depends="init">
<xslt style="src/options.xsl" in="src/properties.xml" out="build/options.html"/>
</target>
<target name="clean">
<delete dir="build"/>
</target>
</project>
这篇关于遍历数量不明的财产使用Ant和搜索和替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!