View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.validator;
17  
18  import org.seasar.cubby.util.Messages;
19  
20  /**
21   * メッセージ情報です。
22   * 
23   * @author baba
24   * @since 1.0.0
25   */
26  public class MessageInfo {
27  
28  	/** {@link Messages}からメッセージを取得するためのキー。 */
29  	private String key;
30  
31  	/** メッセージの置換パターンを置き換えるオブジェクトからなる配列。 */
32  	private Object[] arguments;
33  
34  	/**
35  	 * {@link Messages}からメッセージを取得するためのキーを取得します。
36  	 * 
37  	 * @return キー
38  	 */
39  	public String getKey() {
40  		return key;
41  	}
42  
43  	/**
44  	 * {@link Messages}からメッセージを取得するためのキーを設定します。
45  	 * 
46  	 * @param key
47  	 *            キー
48  	 */
49  	public void setKey(final String key) {
50  		this.key = key;
51  	}
52  
53  	/**
54  	 * メッセージの置換パターンを置き換えるオブジェクトからなる配列を取得します。
55  	 * 
56  	 * @return 置換文字列の配列
57  	 */
58  	public Object[] getArguments() {
59  		if (arguments == null) {
60  			return null;
61  		}
62  		return arguments.clone();
63  	}
64  
65  	/**
66  	 * メッセージの置換パターンを置き換えるオブジェクトからなる配列を取得します。
67  	 * 
68  	 * @param arguments
69  	 *            置換文字列
70  	 */
71  	public void setArguments(final Object... arguments) {
72  		this.arguments = arguments;
73  	}
74  
75  	/**
76  	 * このメッセージ情報から文字列へ変換するためのビルダを取得します。
77  	 * 
78  	 * @return メッセージのビルダ
79  	 */
80  	public MessageBuilder builder() {
81  		final MessageBuilder builder = new MessageBuilder(key, arguments);
82  		return builder;
83  	}
84  
85  	/**
86  	 * {@link Messages}を使用したメッセージを構築するためのビルダ。
87  	 * 
88  	 * @see Messages
89  	 * @author baba
90  	 */
91  	public class MessageBuilder {
92  
93  		/** メッセージキー。 */
94  		private final String messageKey;
95  
96  		/** 置換文字列。 */
97  		private final Object[] arguments;
98  
99  		/** フィールド名のキー。 */
100 		private String fieldNameKey;
101 
102 		/**
103 		 * 指定された情報からインスタンス化します。
104 		 * 
105 		 * @param messageKey
106 		 *            メッセージキー
107 		 * @param arguments
108 		 *            置換文字列
109 		 */
110 		private MessageBuilder(final String messageKey, final Object[] arguments) {
111 			this.messageKey = messageKey;
112 			this.arguments = arguments;
113 		}
114 
115 		/**
116 		 * フィールド名のキーを設定します。
117 		 * 
118 		 * @param fieldNameKey
119 		 *            フィールド名のキー
120 		 * @return このオブジェクト
121 		 */
122 		public MessageBuilder fieldNameKey(final String fieldNameKey) {
123 			this.fieldNameKey = fieldNameKey;
124 			return this;
125 		}
126 
127 		/**
128 		 * {@inheritDoc}
129 		 * <p>
130 		 * {@link Messages}から取得したメッセージをフォーマットした文字列を返します。
131 		 * </p>
132 		 */
133 		@Override
134 		public String toString() {
135 			final Object[] args;
136 			if (fieldNameKey != null) {
137 				args = new Object[this.arguments.length + 1];
138 				final String paramNameText = Messages.getText(fieldNameKey);
139 				args[0] = paramNameText;
140 				System.arraycopy(this.arguments, 0, args, 1,
141 						this.arguments.length);
142 			} else {
143 				args = this.arguments;
144 			}
145 			return Messages.getText(messageKey, args);
146 		}
147 	}
148 
149 }