2012年7月27日金曜日

java.text.MessageFormat

MessageFormatは、文字列フォーマットを行うためのクラスです。本文はMessageFormatクラスのよくある使い方についてサンプルから学びます。
MessageFormatは何をするか

アプリケーションでは、エンドユーザに表示するメッセージがほとんどは定められたフォーマットに沿って出力されています。

たとえば、

ユーザIDを入力してください。
金額を入力してください。

などのメッセージでは、【を入力してください。】は同じです。

MessageFormatは、このようなメッセージを統一したパターンで簡単にフォーマットすることができます。

MessageFormatの使用方法(手順)

1)メッセージパターンを定義します。
  例:{0}を入力してください。
  フォーマットしたい要素は{}で囲まれます。
2)置き換えデータを配列に設定し、MessageFormat.formatメソッドでフォーマットします。
  例:String message = MessageFormat.format("{0}を入力してください。", new String[]{"ユーザID"});
  戻り値は置き換えた後の文字列になります。

java.text.MessageFormat構造

java.text.Format
�java.text.MessageFormat

■コンストラクタ
 ◇ MessageFormat(String pattern)
 ◇ MessageFormat(String pattern, Locale locale)

パラメータ pattern:メッセージフォーマット・パターン
パラメータ locale:ロケール

■MessageFormat主たるメソッド
 ◇ StringBuffer format(Object[] arguments, StringBuffer result, FieldPosition pos)
 ◇ StringBuffer format(Object arguments, StringBuffer result, FieldPosition pos)
 ◇ static String format(String pattern, Object... arguments)
※JDK1.5以前のバージョンでは、static String format(String pattern, Object []arguments)になります。
指定された文字列パターンをフォーマット要素に置き換えフォーマットします。

 ◇ Object[] parse(String source)
文字列を解析してパターンとマッチしたオブジェクトの配列を返します。


■フォーマット要素の定義
フォーマット要素は{}で囲まれます。
{ ArgumentIndex }
{ ArgumentIndex , FormatType }
{ ArgumentIndex , FormatType , FormatStyle }

◇ ArgumentIndex:0,1,2...などのインデックスを表す数字。置き換えデータの配列のインデックス
◇ FormatType:number(数字)、date(日付)、time(日時)、choice(ある範囲の数値)などの文字
◇ FormatStyle:
short
medium
long
full
integer
currency
percent
SubformatPattern(サブフォーマットパターン)

◇ 定義例:
{0}
{0,number,integer}
{1,number,#.##}


サンプル

TestMessageFormat.java
view plaincopy to clipboardprint?
import java.text.MessageFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

public class TestMessageFormat {

/**
* MessageFormatのよくある使い方
*/
public static void main(String[] args) {
System.out.println("********* 1 ***********");
String message = MessageFormat.format("{0}を入力してください。", new Object[]{"ユーザID"});
System.out.println(message);

System.out.println("********* 2 ***********");
String pattern2 = "a={0,number,#.##} , b={1,number,integer} , c={2,date}";
Object []arguments = {
new Float(123.4567),
new Integer(345),
new Date()
};

String text2 = MessageFormat.format(pattern2, arguments);
System.out.println(pattern2);
System.out.println(text2);

System.out.println("********* 3 ***********");
String pattern3 = "a={0} , b={1,number,percent} , c={2,time}";
System.out.println(pattern3);
Object []arguments3 = {
"Hello MessageFormat!",
new Float(0.778),
Calendar.getInstance().getTime()
};

MessageFormat messageFormat = new MessageFormat(pattern3);
String text3 = messageFormat.format(arguments3);
System.out.println(text3);

System.out.println("********* 4 ***********");
String pattern4 = "a={0} , b={1} , c={2}";
String message4 = "a=100 , b=あいうえお , c=2009/03/05";
System.out.println(pattern4);
System.out.println(message4);

MessageFormat messageFormat4 = new MessageFormat(pattern4);
try {
Object []objs = messageFormat4.parse(message4);
System.out.println(objs[0]);
System.out.println(objs[1]);
System.out.println(objs[2]);
} catch (ParseException e) {
e.printStackTrace();
}
}

}



実行結果
********* 1 ***********
ユーザIDを入力してください。
********* 2 ***********
a={0,number,#.##} , b={1,number,integer} , c={2,date}
a=123.46 , b=345 , c=2009/03/05
********* 3 ***********
a={0} , b={1,number,percent} , c={2,time}
a=Hello MessageFormat! , b=78% , c=14:38:25
********* 4 ***********
a={0} , b={1} , c={2}
a=100 , b=あいうえお , c=2009/03/05
100
あいうえお
2009/03/05

0 件のコメント:

コメントを投稿