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  public class MessageInfo {
21  
22  	private String key;
23  
24  	private Object[] arguments;
25  
26  	public String getKey() {
27  		return key;
28  	}
29  
30  	public void setKey(String key) {
31  		this.key = key;
32  	}
33  
34  	public Object[] getArguments() {
35  		if (arguments == null) {
36  			return null;
37  		}
38  		return arguments.clone();
39  	}
40  
41  	public void setArguments(Object... arguments) {
42  		this.arguments = arguments;
43  	}
44  
45  	public MessageBuilder builder() {
46  		MessageBuilder builder = new MessageBuilder(key, arguments);
47  		return builder;
48  	}
49  
50  	public class MessageBuilder {
51  
52  		private final String messageKey;
53  
54  		private String fieldNameKey;
55  
56  		public final Object[] arguments;
57  
58  		private MessageBuilder(final String messageKey, final Object[] arguments) {
59  			this.messageKey = messageKey;
60  			this.arguments = arguments;
61  		}
62  
63  		public MessageBuilder fieldNameKey(final String fieldNameKey) {
64  			this.fieldNameKey = fieldNameKey;
65  			return this;
66  		}
67  
68  		public String toString() {
69  			final Object[] args;
70  			if (fieldNameKey != null) {
71  				args = new Object[this.arguments.length + 1];
72  				final String paramNameText = Messages.getText(fieldNameKey);
73  				args[0] = paramNameText;
74  				System.arraycopy(this.arguments, 0, args, 1, this.arguments.length);
75  			} else {
76  				args = this.arguments;
77  			}
78  			return Messages.getText(messageKey, args);
79  		}
80  	}
81  }