All-You-Can-Gulp Steamin' Hot Hard-Core Java! No-compromise, professional, non-newbie tips, tricks and solutions. You're invited to join!

Thursday, April 11, 2002

Utility Function of the Day

The following example class makes use of JDK 1.4's new Regular Expression classes (In the java.util.regex package) to create a printf(...)-like function.
The comments contain all the information you'll require, so without further ado:

The Code:
import  java.util.*;

import java.util.regex.*;


/**
* Class to show the Regular Expresion example
*/
public class FormatExample
{


/**
* Similar to C's printf(...) routine, this method takes an input string with
* embedded opcodes and replaces these opcodes with the content of specified
* arguments.
*
* The op-code is %{argument_name}, where "argument_name" is the key-name in the
* specified Properties object that holds the content to fill-in.
*
* An example of a string to format would be:
* "Welcome %{user_name} and good %{day_part}!"
* The Properties object contains two entries:
* "user_name" which contains, for example "John Smith" and
* "day_part" which contains, for example: "morning".
* The resulting string is:
* "Welcome John Smith and good morning!"
*
* You could also place any other object as the value for the argument,
* as the routine automatically does a "toString()" call to convert the
* value of the argument to a string.
*
* @param formatString The string to format
* @param arguments By-name hash of arguments to fill-in
* @return Fully qualified string with all references resolved.
* @author Tal Rotbart
*/
public static String format(String formatString, Properties arguments)
{
StringBuffer formattedString = new StringBuffer(formatString.length() * 2);
Pattern tokenRegex = Pattern.compile("%\\{[\\p{Alnum}\\p{Punct}]*\\}");

if ((arguments == null) || (arguments.size() <= 0))
{
return formatString;
}

Matcher matcher = tokenRegex.matcher(formatString);

/**
* Match the %{n} instances and replace them with the specified string argument
*/
while (matcher.find())
{
/**
* Take the matched argument name and use it
*/
String argumentNameString = formatString.substring(matcher.start() + 2, matcher.end() - 1);
Object argumentValue = arguments.get(argumentNameString);

/**
* If we do not have the argument, it doesn't mean we have to choke, it may be filled at a later "format" call.
*/
if (argumentValue != null)
{
/**
* Recursively resolve the referenced string resources
*/
matcher.appendReplacement(formattedString, argumentValue.toString());
}
}


/**
* Append everything after the last match, or from the top if no match
*/
matcher.appendTail(formattedString);

return formattedString.toString();
}


/**
* Main method for testing the "format" routine.
*/
public static void main(String[] args)
{
String formatString = "Welcome %{user_name}! Have a good %{day_part}!";
Properties formatProperties = new Properties();

formatProperties.setProperty("user_name", "John Smith");
formatProperties.setProperty("day_part", "evening");

System.out.println("Result = \"" + format(formatString, formatProperties) + "\".");
}


}




Enjoy :)

0 Comments:

Post a Comment

<< Home