ANT script to replace XML properties from text properties

This script fixes TDI XML properties to match the actual .properties files that the XML properties are based on. It contains numerous examples on string substitution, property and file processing with ANT.

<?xml version="1.0" encoding="UTF-8"?>
<!-- ======================================================================
     TDI Property fix
     This script fixes TDI XML properties to match the actual .properties files
     that the XML properties are based on
     by Alex Ivkin
     ====================================================================== -->
<project name="RefreshProperties" default="fixup" basedir=".">
    <description>Fix TDI XML properties</description>
    <xmlproperty file=".project" />
    <taskdef resource="net/sf/antcontrib/antcontrib.properties">
      <classpath>
        <pathelement location="../Tools/ant-contrib/ant-contrib-1.0b3.jar"/>
      </classpath>
    </taskdef>
    <!-- Loop over all TDI XML properties, extract referenced plain text properties and then replace ones in the XML with the ones from the plain text -->
    <target name="fixup" description="Go over all XML properties and fix them">
        <echo>Ensuring TDI XML properties are up to date...</echo>
        <foreach target="fixafile" param="filfil">
            <path id="tdiprop.files"><fileset dir="Resources/Properties" includes="*.tdiproperties" /></path>
        </foreach>
    </target>
    <target name="fixafile" description="Extract a text property file from an XML file">
        <xmlproperty file="${filfil}" />
        <echo>Processing ${filfil}</echo>
        <foreach list="${MetamergeConfig.Container.Properties.Stores.PropertyStore.RawConnector.parameter}" target="pickaprop" param="propraram" />
    </target>
    <target name="pickaprop" description="Extract all properties from a text property file">
        <if>
            <and>
                <!-- come propraram are not the the names of the plain text properties, but is other TDI stuff -->
                <contains string="${propraram}" substring=".properties"/>
                <!-- Some references contain @SUBSTITUTE that we can't process right now. Could also handle this as <not><contains ... </not> -->
                <available file="${propraram}" />
            </and>
            <then>
                <!-- for each one that is a text property file, extract the actual props -->
                <property file="${propraram}" prefix="authoritative"/>
                <!-- combine them into a comma delimited list -->
                <propertyselector property="allprops" match="authoritative\.(.*)" select="\1" />
                <!-- loop over this list -->
                <foreach list="${allprops}" target="replaceproperty" param="propname" />
            </then>
        </if>
    </target>
    <target name="replaceproperty" description="Replace a property in an XML file with a given property">
        <!-- Use loadproperties instead of <property file="${propraram}" prefix="authoritative"/> to fix XML characters before insertion -->
        <!-- could also use <property file... combined with <propertyregex property="propvalue" input="${proprawvalue}" regexp="&lt;" replace="... for the same purpose-->
        <loadproperties srcFile="${propraram}"><filterchain><tokenfilter>
            <!--  for some reason &quot; and &apos; are not escaped by TDI, so we skip them here. Strip the protect string -->
            <replacestring from="&amp;" to="&amp;amp;" />
            <replacestring from="&gt;" to="&amp;gt;" />
            <replacestring from="&lt;" to="&amp;lt;" />
            <replacestring from="{protect}-" to="" />
        </tokenfilter></filterchain></loadproperties>
        <if>
            <!-- deal with the protected variables appropriately -->
            <contains string="${propname}" substring="{protect}-" />
            <then>
                <!-- strip the {protect} heading -->
                <propertyregex property="propname" input="${propname}" regexp="\{protect\}-" replace="" override="true"/>
                <!-- enable variable protection -->
                <replaceregexp file="${filfil}"
                    match="ParameterList name=&quot;${propname}&quot;(.*?)name=&quot;Protect&quot;&gt;.*?&lt;/"
                    replace="ParameterList name=&quot;${propname}&quot;\1name=&quot;Protect&quot;&gt;true&lt;/"
                    flags="s"/>
           </then>
        </if>
        <!-- propvalue becomes the value of the property by the name propname -->
        <propertycopy name="propvalue" from="${propname}" />
        <!-- <echo>${propname}=${propvalue}</echo> -->
        <!-- Replace a value with a regexp spanning multiple lines (flag=s) -->
        <replaceregexp file="${filfil}"
            match="ParameterList name=&quot;${propname}&quot;(.*?)name=&quot;value&quot;&gt;.*?&lt;/"
            replace="ParameterList name=&quot;${propname}&quot;\1name=&quot;value&quot;&gt;${propvalue}&lt;/"
            flags="s"/>
    </target>
</project>

@Tools