Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
RequiredValidator |
|
| 2.6666666666666665;2.667 |
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 | */ | |
35 | public class RequiredValidator implements ScalarFieldValidator { | |
36 | ||
37 | /** | |
38 | * メッセージヘルパ。 | |
39 | */ | |
40 | private final MessageHelper messageHelper; | |
41 | ||
42 | /** | |
43 | * コンストラクタ | |
44 | */ | |
45 | public RequiredValidator() { | |
46 | 15 | this("valid.required"); |
47 | 15 | } |
48 | ||
49 | /** | |
50 | * エラーメッセージキーを指定するコンストラクタ | |
51 | * | |
52 | * @param messageKey | |
53 | * エラーメッセージキー | |
54 | */ | |
55 | 15 | public RequiredValidator(final String messageKey) { |
56 | 15 | this.messageHelper = new MessageHelper(messageKey); |
57 | 15 | } |
58 | ||
59 | public void validate(final ValidationContext context, final Object value) { | |
60 | 10 | if (value instanceof String) { |
61 | 10 | final String str = (String) value; |
62 | 10 | if (!StringUtil.isEmpty(str)) { |
63 | 6 | return; |
64 | } | |
65 | 4 | } else if (value != null) { |
66 | 0 | return; |
67 | } | |
68 | 4 | context.addMessageInfo(this.messageHelper.createMessageInfo()); |
69 | 4 | } |
70 | ||
71 | } |