How to use maru-dog Beanデータバインドライブラリ (3)
第2回ではシンプルコンバータについて説明しました。今回はmaru-dogが標準で提供しているコンバータについて解説します。
maru-dogが提供しているコンバータは以下の4つです。
- @StringToDate
指定されたパターンのString型の日付データをjava.util.Date型に変換する。変換エラーの場合はIllegalArgumentExceptionがスローされます。
- @DateToString
java.util.Date型のデータを指定されたパターンでString型に変換する。
- @RelapceAll
String型のデータ内に含まれる文字列の任意の正規表現に一致するもの全てを特定の文字列(文字)に置き換える。
- @ReplaceFirst
String型のデータ内に含まれる文字列の任意の正規表現に一致する最初の文字列(文字)を特定の文字列(文字)に置き換える。
簡単に解説をしましたが、実際にサンプルを見たほうが早いと思うので早速以下に利用例を示します。
import java.util.Date; import org.maru.dog.Director; import org.maru.dog.Marudog; import org.maru.dog.annotation.Bind; import org.maru.dog.annotation.Bound; import org.maru.dog.converter.DateToString; import org.maru.dog.converter.ReplaceAll; import org.maru.dog.converter.ReplaceFirst; import org.maru.dog.converter.StringToDate; import org.maru.dog.util.DateUtil; public class Main { public static void main(String[] args) { TargetBean tBean = new TargetBean(); Director director = Marudog.getDirector(); director.bind(tBean, getInputBean()); System.out.println(tBean); } private static InputBean getInputBean() { InputBean iBean = new InputBean(); iBean.birthDay = "1984/07/25"; iBean.today = new Date(); iBean.before = "This is an input data."; iBean.content = "the output message is converted by the converter."; return iBean; } static class TargetBean { @Bound private Date birthDay; @Bound private String today; @Bound(name = "message") private String after; @Bound private String content; @Override public String toString() { return "TargetBean [birthDay=" + DateUtil.d2sConvert(birthDay, "yyyy/MM/dd") + ", today=" + today + ", after=" + after + ", content=" + content + "]"; } } static class InputBean { @Bind @StringToDate(pattern = "yyyy/MM/dd") private String birthDay; @Bind @DateToString(pattern = "yyyy/MM/dd") private Date today; @Bind(name = "message") @ReplaceAll(regexp = "input", replacement="output") private String before; @Bind @ReplaceFirst(regexp = "the", replacement="The") private String content; } }
上記プログラムの実行結果は次のようになります。
TargetBean [birthDay=1984/07/25, today=2011/07/25, after=This is an output data., content=The output message is converted by the converter.]
それでは順に説明したいと思います。
InputBeanのbirthDayプロパティには@StringToDate(pattern = "yyyy/MM/dd")アノテーションが付与されています。これはpatternで指定されたフォーマットの文字列をDate型に変換します。変換後のDate型の値がTargetBeanのbirthDayにバインドされます。上記例では1984/07/25という文字列型の日付がDate型に変換されます。
todayプロパティはには@DateToString(pattern = "yyyy/MM/dd")アノテーションが付与されています。これは@StringToDateとは逆で、Date型のデータをpatternで指定されたフォーマットでString型に変換します。変換後のString型の値がTargetBeanのtodayにバインドされます。上記例ではDate型のシステム日付がフォーマットに従って文字列に変換されます。
beforeプロパティには@ReplaceAll(regexp = "input", replacement="output")アノテーションが付与されています。これはregexpで指定された正規表現に一致する文字列(文字)をreplacementで指定された文字列に変換します。変換後の値がTargetBeanのafterにバインドされます。上記例ではbeforeの「This is an input data.」が「This is an output data.」と変換されます。
contentプロパティには@ReplaceFirst(regexp = "the", replacement="The")アノテーションが付与されています。これはregexpで指定した正規表現に一致する文字(文字)をreplacementで指定した文字列に変換します。@ReplaceAllとの違いは、@ReplaceAllが文字列内のすべての一致するパターンに対し変換を行うのに対して、@ReplaceFirstは最初に一致した文字列(文字)に対して変換を行います。変換後の値がTargetBeanのcontentにバインドされます。上記例ではcontentの「the output message is converted by the converter.」の最初の"the"が"The"に変換され「The output message is converted by the converter.」という値がTargetBean側にバインドされています。
以上がmaru-dogが標準で提供するコンバータの概要です。
最後に、すでにお気付きかもしれませんが前回説明したシンプルコンバータとは設定の方法が異なっています。標準で提供するコンバータはすべて拡張コンバータとして提供しています。
拡張コンバータについては次回以降で別途説明したいと思います。
サイトページ:http://maru.sourceforge.jp/
ドキュメントページ:http://maru.sourceforge.jp/document_dog.html