View Javadoc

1   /*
2    * Copyright 2004-2010 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  
17  package org.seasar.cubby.action;
18  
19  import java.util.Arrays;
20  
21  import org.seasar.cubby.util.Messages;
22  
23  /**
24   * メッセージ情報です。
25   * 
26   * @author baba
27   */
28  public class MessageInfo {
29  
30  	/** {@link Messages} からメッセージを取得するためのキー。 */
31  	private String key;
32  
33  	/** メッセージの置換パターンを置き換えるオブジェクトからなる配列。 */
34  	private Object[] arguments;
35  
36  	/**
37  	 * {@link Messages} からメッセージを取得するためのキーを取得します。
38  	 * 
39  	 * @return キー
40  	 */
41  	public String getKey() {
42  		return key;
43  	}
44  
45  	/**
46  	 * {@link Messages} からメッセージを取得するためのキーを設定します。
47  	 * 
48  	 * @param key
49  	 *            キー
50  	 */
51  	public void setKey(final String key) {
52  		this.key = key;
53  	}
54  
55  	/**
56  	 * メッセージの置換パターンを置き換えるオブジェクトからなる配列を取得します。
57  	 * 
58  	 * @return 置換文字列の配列
59  	 */
60  	public Object[] getArguments() {
61  		if (arguments == null) {
62  			return null;
63  		}
64  		return arguments.clone();
65  	}
66  
67  	/**
68  	 * メッセージの置換パターンを置き換えるオブジェクトからなる配列を取得します。
69  	 * 
70  	 * @param arguments
71  	 *            置換文字列
72  	 */
73  	public void setArguments(final Object... arguments) {
74  		final Object[] copyArguments = new Object[arguments.length];
75  		System.arraycopy(arguments, 0, copyArguments, 0, arguments.length);
76  		this.arguments = copyArguments;
77  	}
78  
79  	/**
80  	 * メッセージ文字列に変換します。
81  	 * 
82  	 * @param fieldNameKey
83  	 *            フィールド名のリソースキー
84  	 * @return メッセージ文字列
85  	 */
86  	public String toMessage(final String fieldNameKey) {
87  		final Object[] args;
88  		if (fieldNameKey != null) {
89  			if (this.arguments != null) {
90  				args = new Object[this.arguments.length + 1];
91  				final String paramNameText = Messages.getText(fieldNameKey);
92  				args[0] = paramNameText;
93  				System.arraycopy(this.arguments, 0, args, 1,
94  						this.arguments.length);
95  			} else {
96  				args = new Object[]{Messages.getText(fieldNameKey)};
97  			}
98  		} else {
99  			args = this.arguments;
100 		}
101 		return Messages.getText(key, args);
102 	}
103 
104 	/**
105 	 * {@inheritDoc}
106 	 */
107 	@Override
108 	public String toString() {
109 		final StringBuilder builder = new StringBuilder();
110 		builder.append(super.toString());
111 		builder.append("[key=");
112 		builder.append(key);
113 		builder.append(",arguments=");
114 		builder.append(Arrays.deepToString(arguments));
115 		builder.append("]");
116 		return builder.toString();
117 	}
118 
119 }