FileDocCategorySizeDatePackage
MessageFormat.javaAPI DocphoneME MR2 API (J2ME)4521Wed May 02 18:00:46 BST 2007com.sun.j2me.global

MessageFormat

public class MessageFormat extends Object
MessageFormat provides means to produce concatenated messages in language-neutral way. Use this to construct messages displayed for end users. MessageFormat formats message with variable number of parameters. Pattern string contains placeholders of the form {0} .. {nn}. Placeholders are replaced by parameters. Escaped "{{" can be used to get '{' in formatted message.

MessageFormat takes a set of strings, and inserts the them into the pattern at the appropriate places.

String output = MessageFormat.format("Message {1} for formatting {2}",
new String[]{"first_arg",
"second_arg"});

Fields Summary
private static final char
LEFT_PARENTHESIS
Value for the left parenthesis char
private static final char
RIGHT_PARENTHESIS
Value for the right parenthesis char
Constructors Summary
Methods Summary
public static java.lang.Stringformat(java.lang.String message, java.lang.String[] params)
Replace placeholders in template with parameters.

param
message the template with placeholders
param
params array of parameters
return
buffer containing formatted message


                                          
           
    	if (message == null || params == null) {
    		throw new NullPointerException("Template or parameter array is null.");
    	}	    	
        boolean inside = false;
        boolean escaped = false;
        StringBuffer result = new StringBuffer();
        StringBuffer placeholder = null;
        char lookingFor = LEFT_PARENTHESIS;
        char c;

        for (int i = 0; i < message.length(); i++) {
            c = message.charAt(i);
            if (c == lookingFor) {
                if (escaped) {
                    result.append(c);
                    escaped = false;
                    continue;
                }
                if (c == LEFT_PARENTHESIS) {
                    // look ahead for escaped parenthesis
                    if ((i + 1) < message.length() &&
                            message.charAt(i + 1) == LEFT_PARENTHESIS) {
                        escaped = true;
                    } else {
                        inside = true;
                        lookingFor = RIGHT_PARENTHESIS;
                        placeholder = new StringBuffer();
                    }
                } else {
                    inside = false;
                    lookingFor = LEFT_PARENTHESIS;
                    // placeholder finished get parameter
                    try {
                        if (placeholder.length() > 2) {
                            throw new IllegalArgumentException(
                                    "Illegal placeholder.");
                        }
                        int index = Integer.parseInt(placeholder.toString());
                        result.append(params[index]);
                    } catch (ArrayIndexOutOfBoundsException ie) {
                        throw new IllegalArgumentException(
                                "Illegal number of parameters.");
                    }
                }
            } else {
                if (inside) {
                    placeholder.append(c);
                } else {
                    result.append(c);
                }
            }
        }
        // for
        return result.toString();