ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From <Jan.Mate...@rzf.fin-nrw.de>
Subject AW: How do I do text manipulation on a property string?
Date Mon, 30 Apr 2007 06:17:03 GMT
>I have a property value (string) that I receive from a command 
>line argument as lowercase text.
>
>On some platforms I need it to be converted to Title case, on 
>others I need it to be converted to UPPER case before passing it on to
my 
>build tools.
>
>Is there a preferred method to perform case conversions in ant scripts?

I would use a <scriptdef>.


Jan

<project>


    <scriptdef name="convert" language="javascript">
        <attribute name="property"/>
        <attribute name="upper"/>
        <attribute name="title"/>
        <element name="fileset" type="fileset"/>
        <![CDATA[
            // Access to <attribute>s with default values
            property = attributes.get("property");
            upper    = getDefault("upper", property + ".upper");
            title    = getDefault("title", property + ".title");
            
            // compute the new values
            valueOrg = project.getProperty(property);
            valueUpc = valueOrg.toUpperCase();
            valueTit = valueOrg.substring(0,1).toUpperCase() +
valueOrg.substring(1);
            
            // store the values
            project.setNewProperty(upper, valueUpc);
            project.setNewProperty(title, valueTit);
            
            // Helper function 
            function getDefault(propName, defaultValue) {
                x = attributes.get(propName);
                if (x == null) {
                    return defaultValue; 
                } else {
                    return x;
                }
            }
        ]]>
    </scriptdef>


    <property name="one" value="one"/>
    <property name="two" value="two"/>
    
    <convert property="one" upper="upper" title="title"/>
    <echo>
        one    : ${one}
        upper  : ${upper}
        title  : ${title}
    </echo>
    
    <convert property="two"/>
    <echoproperties prefix="two"/>


</project>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message