antun
antun

Reputation: 2297

iterate over unknown number of properties using ant and search and replace

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!

Upvotes: 0

Views: 544

Answers (1)

Mark O&#39;Connor
Mark O&#39;Connor

Reputation: 77951

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>

Upvotes: 3

Related Questions