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.validators;
17  
18  import org.seasar.cubby.validator.MessageHelper;
19  import org.seasar.cubby.validator.ScalarFieldValidator;
20  import org.seasar.cubby.validator.ValidationContext;
21  import org.seasar.framework.util.StringUtil;
22  
23  /**
24   * 必須検証します。
25   * <p>
26   * 文字列の長さが0の場合、検証エラーとなります。
27   * </p>
28   * <p>
29   * デフォルトエラーメッセージキー:valid.required
30   * </p>
31   * 
32   * @author agata
33   * @author baba
34   * @since 1.0.0
35   */
36  public class RequiredValidator implements ScalarFieldValidator {
37  
38  	/**
39  	 * メッセージヘルパ。
40  	 */
41  	private final MessageHelper messageHelper;
42  
43  	/**
44  	 * コンストラクタ
45  	 */
46  	public RequiredValidator() {
47  		this("valid.required");
48  	}
49  
50  	/**
51  	 * エラーメッセージキーを指定するコンストラクタ
52  	 * 
53  	 * @param messageKey
54  	 *            エラーメッセージキー
55  	 */
56  	public RequiredValidator(final String messageKey) {
57  		this.messageHelper = new MessageHelper(messageKey);
58  	}
59  
60  	/**
61  	 * {@inheritDoc}
62  	 */
63  	public void validate(final ValidationContext context, final Object value) {
64  		if (value instanceof String) {
65  			final String str = (String) value;
66  			if (!StringUtil.isEmpty(str)) {
67  				return;
68  			}
69  		} else if (value != null) {
70  			return;
71  		}
72  		context.addMessageInfo(this.messageHelper.createMessageInfo());
73  	}
74  
75  }