FileDocCategorySizeDatePackage
MessageFormatter.javaAPI DocGlassfish v2 API9304Fri May 04 22:34:10 BST 2007com.sun.enterprise.admin.util

MessageFormatter

public class MessageFormatter extends Object implements IStringSource
Class for replacing numbered tokens in Strings with other Strings. The static methods can be used for working with passed-in String arguments. Use the non-static methods for working with indirect Strings. I.e. String keys are passed in which in turn are used to locate the actual String to substitute. The class in that case is instantiated by passing in a IStringSource object which is used for fetching Strings.

The token format is {#}. Where # is 1-9. Note that the numbering is one-based, not zero-based. E.g.

MessageFormatter.format("Hello {1}, how are {2}?", "Carbon-based Lifeform", "you");

Fields Summary
private IStringSource
mSource
Constructors Summary
public MessageFormatter(IStringSource source)
Create an instance using the supplied IStringSource to find Strings

param
lookup The string library

		setSource(source);
	
Methods Summary
public static java.lang.Stringformat(java.lang.String base, java.lang.Object[] toInsert)
This is the workhorse method of all of these overloaded methods. The array of Objects' toString() methods are called one at a time. The resulting Strings are used to replace numbered tokens. I.e. replacement String #0 replaces this token: {1} and replacement String #1 replaces this token: {2}. Avoid messy calling code by dedicating methods to the common cases of 1-3 inserts.

param
base String with embedded tokens
param
toInsert An ordered array of Objects to replace the tokens with
return
The original String with token substitution

		// the only "format" method that actually does much work!
		// let's be gentle with nulls & empty Strings...
		
		//ArgChecker.checkValid(base, "base");
		
		if(toInsert == null || toInsert.length <= 0)
			return null;
		
		String ret = base;
		
		for(int i = 0; i < toInsert.length; i++)
		{
			String token	= makeToken(i + 1);
			String replace	= toInsert[i].toString();
			ret = replaceToken(ret, token, replace);
			
			if(ret == null || ret.length() <= 0)
				return null;
		}
		return ret;
	
public static java.lang.Stringformat(java.lang.String base, java.lang.Object o1)
Version of format with one token to replace

param
base String with embedded tokens
param
o1 An Object whose toString() results replace the token
return
The original String with token substitution

		return format(base, new Object[] {o1});
	
public static java.lang.Stringformat(java.lang.String base, java.lang.Object o1, java.lang.Object o2)
Version of format with two tokens to replace

param
base String with embedded tokens
param
o1 An Object whose toString() results replace the token
param
o2 An Object whose toString() results replace the token
return
The original String with token substitution

		return format(base, new Object[] {o1, o2});
	
public static java.lang.Stringformat(java.lang.String base, java.lang.Object o1, java.lang.Object o2, java.lang.Object o3)
Version of format with three tokens to replace

param
base String with embedded tokens
param
o1 An Object whose toString() results replace the token
param
o2 An Object whose toString() results replace the token
param
o3 An Object whose toString() results replace the token
return
The original String with token substitution

		return format(base, new Object[] {o1, o2, o3});
	
public java.lang.StringgetAndFormat(java.lang.String key, java.lang.Object o1, java.lang.Object o2)
Get the String from the member IStringSource object -- then call format using that String.

param
key String key to locate String value with
param
o1 An Object whose toString() results replace the token
param
o2 An Object whose toString() results replace the token
return
The token-replaced String

		return getAndFormat(key, new Object[] { o1, o2} );
	
public java.lang.StringgetAndFormat(java.lang.String key, java.lang.Object o1, java.lang.Object o2, java.lang.Object o3)
Get the String from the member IStringSource object -- then call format using that String.

param
key String key to locate String value with
param
o1 An Object whose toString() results replace the token
param
o2 An Object whose toString() results replace the token
param
o3 An Object whose toString() results replace the token
return
The token-replaced String

		return getAndFormat(key, new Object[] { o1, o2, o3} );
	
public java.lang.StringgetAndFormat(java.lang.String key, java.lang.Object[] toInsert)
Get the String from the member IStringSource object -- then call format using that String.

param
key key String to locate value String with
param
toInsert An ordered array of Objects to replace the tokens with
return
The located String with token substitution

		return format(getString(key), toInsert);
	
public java.lang.StringgetAndFormat(java.lang.String key, java.lang.Object o1)
Get the String from the member IStringSource object -- then call format using that String.

param
key String key to locate String value with
param
o1 An Object whose toString() results replace the token
return
The token-replaced String

		return getAndFormat(key, new Object[] { o1} );
	
public IStringSourcegetSource()
Get the IStringSource object

return
the IStringSource object

		Assert.assertit((mSource!=null), "mSource");
		return mSource;
	
public java.lang.StringgetString(java.lang.String lookupKey)
IStringSource signature method. In this case it asks the IStringSource member variable to get the String.

param
lookupKey key to locate String with
return
The String value for the key. Null if not found.

		//ArgChecker.check(lookupKey, "lookupKey");
		return(mSource.getString(lookupKey));
	
public static voidmain(java.lang.String[] notUsed)
TEMPORARY -- until unit testing code is created...

param
notUsed


	            	
	    
	
		String test2A = "hello {1}, How are {2}?";
		String test2AResult = format(test2A, "Carbon-based lifeform", "you");
		Debug.println("INPUT:  " + test2A + "\nOUTPUT:  " + test2AResult);
	
private static java.lang.StringmakeToken(int num)

            /* this is the one and only place where the specifics of how tokens
             * are represented are kept.
             * It would have been easy (and nice!) to change this token 
             * programmatically.  But unfortunately, impossible, because we
             * have static methods
             */
            return "{" + num + "}";
	
private static java.lang.StringreplaceToken(java.lang.String s, java.lang.String token, java.lang.String replace)

            /* look for the token, 'token', inside the String, 's', and replace
             * with the String, 'replace'
             */
            
            if(s == null || s.length() <= 0 || token == null || token.length() <= 0)
			return s;
		
		int index = s.indexOf(token);

		if(index < 0)
			return s;

		int tokenLength = token.length();
		String ret = s.substring(0, index);
		ret += replace;
		ret += s.substring(index + tokenLength);

		return ret;
	
public voidsetSource(IStringSource source)
Set the IStringSource object

param
lookup The IStringSource object

		//ArgChecker.check(source, "source");
		//ArgChecker.check(source != this, "Can't setSource to self!");
		mSource	= source;