UPPERCASE, lowercase, Capitalize an Ant property
In Ant, I have a property named 'some_property
', and let's say its value is "hello
".
I'm trying to replace a place-holde开发者_如何学编程r inside a text file with this property's value ("hello") as an upper-case.
So, I have this task:<replaceregexp match="SOME_PLACE_HOLDER" replace="${some_property}" byline="true">
And I want it to work as if I would have this task:
<replaceregexp match="SOME_PLACE_HOLDER" replace="HELLO" byline="true">
I wish to avoid external Ant tasks (such as Ant-Contrib), therefore the solution needs to be a pure regex - it must be possible!
UPPERCASE, lowercase, and Capitalized.
Anyone knows the correct regexes?
I understand that you want to avoid Ant extensions, but the constraint that the solution be implemented using regex is a little tight - apologies if the following bends (breaks?) that rule too much.
Ant ships with a javascript engine these days, so anything that seems problematic to implement in Ant xml can usually be hidden away in a scriptdef
. Below are four that do case changing.
In your case, you would take your some_property
property and process it through the upper
script to get an uppercased version of the string to use in the replaceregexp
task.
<scriptdef language="javascript" name="upper">
<attribute name="string" />
<attribute name="to" />
project.setProperty( attributes.get( "to" ),
attributes.get( "string" ).toUpperCase() );
</scriptdef>
<scriptdef language="javascript" name="lower">
<attribute name="string" />
<attribute name="to" />
project.setProperty( attributes.get( "to" ),
attributes.get( "string" ).toLowerCase() );
</scriptdef>
<scriptdef language="javascript" name="ucfirst">
<attribute name="string" />
<attribute name="to" />
var the_string = attributes.get( "string" );
project.setProperty( attributes.get( "to" ),
the_string.substr(0,1).toUpperCase() + the_string.substr(1) );
</scriptdef>
<scriptdef language="javascript" name="capitalize">
<attribute name="string" />
<attribute name="to" />
var s = new String( attributes.get( "string" ) );
project.setProperty( attributes.get( "to" ),
s.toLowerCase().replace( /^.|\s\S/g,
function(a) { return a.toUpperCase(); }) );
</scriptdef>
Example use:
<property name="phrase" value="the quick brown FOX jUmped oVer the laZy DOG" />
<upper string="${phrase}" to="upper" />
<lower string="${phrase}" to="lower" />
<ucfirst string="${phrase}" to="ucfirst" />
<capitalize string="${phrase}" to="capitalize" />
<echo message="upper( ${phrase} )${line.separator}= '${upper}'" />
<echo message="lower( ${phrase} )${line.separator}= '${lower}'" />
<echo message="ucfirst( ${phrase} )${line.separator}= '${ucfirst}'" />
<echo message="capitalize( ${phrase} )${line.separator}= '${capitalize}'" />
And output:
[echo] upper( the quick brown FOX jUmped oVer the laZy DOG )
[echo] = 'THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG'
[echo] lower( the quick brown FOX jUmped oVer the laZy DOG )
[echo] = 'the quick brown fox jumped over the lazy dog'
[echo] ucfirst( the quick brown FOX jUmped oVer the laZy DOG )
[echo] = 'The quick brown FOX jUmped oVer the laZy DOG'
[echo] capitalize( the quick brown FOX jUmped oVer the laZy DOG )
[echo] = 'The Quick Brown Fox Jumped Over The Lazy Dog'
Thanks to Poni and Marco Demaio for the implementation of the Capitalization.
You can use something similar to SCriptdef
and any convenient language.
<scriptdef language="javascript" name="upper">
<attribute name="string" />
<attribute name="to" />
project.setProperty( attributes.get( "to" ),
attributes.get( "string" ).toUpperCase() );
</scriptdef>
Here JavaScript is mentioned as an example. You can also use any other.
Using JavaScript in ant has become a bit of a hassle. This approach might work if you have a Unix shell available:
<exec dir="."
executable="/usr/bin/tr"
os="Mac OS X"
inputstring="${ant.project.name}"
outputproperty="ant.project.name.lower"
>
<arg value="[:upper:]"/>
<arg value="[:lower:]"/>
</exec>
Substitute or add OS names as appropriate. I'm not sure if tr
can capitalize, but case conversion is easy.
精彩评论