View Javadoc

1   /*
2    * Copyright 2004-2009 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  		final Object[] copyArguments = new Object[arguments.length];
73  		System.arraycopy(arguments, 0, copyArguments, 0, arguments.length);
74  		this.arguments = copyArguments;
75  	}
76  
77  	/**
78  	 * このメッセージ情報から文字列へ変換するためのビルダを取得します。
79  	 * 
80  	 * @return メッセージのビルダ
81  	 */
82  	public MessageBuilder builder() {
83  		final MessageBuilder builder = new MessageBuilder(key, arguments);
84  		return builder;
85  	}
86  
87  	/**
88  	 * {@link Messages}を使用したメッセージを構築するためのビルダ。
89  	 * 
90  	 * @see Messages
91  	 * @author baba
92  	 */
93  	public static class MessageBuilder {
94  
95  		/** メッセージキー。 */
96  		private final String messageKey;
97  
98  		/** 置換文字列。 */
99  		private final Object[] arguments;
100 
101 		/** フィールド名のキー。 */
102 		private String fieldNameKey;
103 
104 		/**
105 		 * 指定された情報からインスタンス化します。
106 		 * 
107 		 * @param messageKey
108 		 *            メッセージキー
109 		 * @param arguments
110 		 *            置換文字列
111 		 */
112 		private MessageBuilder(final String messageKey, final Object[] arguments) {
113 			this.messageKey = messageKey;
114 			this.arguments = arguments;
115 		}
116 
117 		/**
118 		 * フィールド名のキーを設定します。
119 		 * 
120 		 * @param fieldNameKey
121 		 *            フィールド名のキー
122 		 * @return このオブジェクト
123 		 */
124 		public MessageBuilder fieldNameKey(final String fieldNameKey) {
125 			this.fieldNameKey = fieldNameKey;
126 			return this;
127 		}
128 
129 		/**
130 		 * {@inheritDoc}
131 		 * <p>
132 		 * {@link Messages}から取得したメッセージをフォーマットした文字列を返します。
133 		 * </p>
134 		 */
135 		@Override
136 		public String toString() {
137 			final Object[] args;
138 			if (fieldNameKey != null) {
139 				if (this.arguments != null) {
140 					args = new Object[this.arguments.length + 1];
141 					final String paramNameText = Messages.getText(fieldNameKey);
142 					args[0] = paramNameText;
143 					System.arraycopy(this.arguments, 0, args, 1,
144 							this.arguments.length);
145 				} else {
146 					args = new Object[] { Messages.getText(fieldNameKey) };
147 				}
148 			} else {
149 				args = this.arguments;
150 			}
151 			return Messages.getText(messageKey, args);
152 		}
153 	}
154 
155 }