19

In Ant, I have a property named 'some_property', and let's say its value is "hello".

I'm trying to replace a place-holder 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?

martin clayton
  • 76,436
  • 32
  • 213
  • 198
Poni
  • 11,061
  • 25
  • 80
  • 121

3 Answers3

36

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.

Community
  • 1
  • 1
martin clayton
  • 76,436
  • 32
  • 213
  • 198
  • Knew about – Poni Aug 20 '11 at 23:15
  • 1
    To use – Dov Dec 07 '11 at 18:59
0

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.

Nazik
  • 8,696
  • 27
  • 77
  • 123
Dhinesh M
  • 21
  • 1
  • 3
0

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.

Alan Snyder
  • 408
  • 2
  • 13