String Casing in Ant

One of the various build tools we use is trusty, reliable Apache Ant. In order to take some of the regex load off our Apache web server, we decided to deploy versions of our javascript files in various cases (uppercase, lowercase, etc.) in addition to our standard mixed case. Normally this type of thing isn’t a problem, since our JS is only called from within our code, but somehow, browsers are messing with String case. Annoying.

Unfortunately, amongst the numerous tasks provided within Ant and contrib functions, there isn’t one for String manipulation. After a bit of Googling, the best I found was in a blog post by Peter Thomas which suggested writing a small Java class, that was compiled during the Ant build, and then used to read in a value and write the output to a temp file, which could then be set into a property. It inspired me, but I wanted to avoid the temp file. The main thinking I liked was trying to avoid manipulating the Ant install environment for simple string manipulation.

Before starting down this path, I considered alternate approaches, that were mostly RegEx based, but I couldn’t find a way to move forward without adding in contrib tasks.

Finally, I worked up the following Java class that was a bit more flexible, able to do lowercase, uppercase, and true titlecase (Peter’s implementation only title cased one word). And most importantly, it avoids the temp file.

/**
* Simple class that takes an input value, converts the letter case and writes
* the result to standard out. The intention is for this class to be used within
* ant.
*
* Takes two parameters, the first is a command:
*
* <ul>
* <li>uc - Converts the remaining arguments to all uppercase</li>
* <li>lc - Converts the remaining arguments to all lowercase</li>
* <li>tc - Converts the remaining arguments to title case</li>
* </ul>
*
* Example conversion to title case:
* Usage: java StringCase -tc here IS some String to case
* Output: Here Is Some String To Case
*
* If the usage is incorrect, the error will be written to standard error.
*
* To use this within Ant, invoke as:
*
* <pre>
* <java classpath="contrib" classname="StringCase" outputproperty="target.property">
* <arg value="-uc" />
* <arg value="string to uppercae" />
* </java>
* </pre>
*
*
* Inspired from:
* http://ptrthomas.wordpress.com/2007/04/22/how-to-do-string-operations-in-ant/
*
* @author <a href="mailto:lbinder@linkstorms.com">Lon F. Binder</a>
*
* $Id$
*/
public class StringCase {


/* —– CONSTANTS —– */


private final static String UPPERCASE = “-uc”;
private final static String LOWERCASE = “-lc”;
private final static String TITLECASE = “-tc”;








/* —– METHODS —– */


public static void main(String[] args) throws Exception {
if (args==null || args.length==0) {
System.err.println(”Incorrect usage, no arguments provided.”);
return;
}


String command = args[0].toLowerCase();


if (!(UPPERCASE.equals(command) || LOWERCASE.equals(command) ||
TITLECASE.equals(command))) {
System.err.println(
“Incorrect usage, unrecognized command argument provided: ” +
command);
return;
}


if (args.length == 1) {
System.err.println(”Incorrect usage, no text provided.”);
return;
}


String word = “”;
for (int i=1; i<args.length; ++i) {
if (UPPERCASE.equals(command)) {
word = args[i].toUpperCase();
} else if (LOWERCASE.equals(command)) {
word = args[i].toLowerCase();
} else if (TITLECASE.equals(command)) {
word = args[i].substring(0,1).toUpperCase();
if (args[i].length() > 1) {
word += args[i].substring(1).toLowerCase();
}
}


if (i>1) {
System.out.print(’ ‘);
}


System.out.print(word);
}
}
} // Everyone likes the end of class

I dropped this Java file into a contrib directory within my Ant project. Then I modified my build.xml file, at the top, adding this one line:

<javac srcdir="contrib" includes="StringCase.java" />

Then wherever I needed a manipulated property, I use this command:

<java classpath="contrib" classname="StringCase" outputproperty="dist.file.initializer.uc">
<arg value="-uc" />
<arg value="${dist.file.initializer}" />
</java>

In this case the outputproperty will be a new Ant property (unfortunately, immutable as usual), that is cased as indicated by the first arg (”-uc” or uppercase). Works perfectly.

You can leave a response, or trackback from your own site.

Leave a Reply



Your Privacy is important to us. We will never give, lease or sell your personal information.