Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
NumberValidator |
|
| 3.3333333333333335;3.333 |
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.validators; | |
17 | ||
18 | import java.util.regex.Pattern; | |
19 | ||
20 | import org.seasar.cubby.action.MessageInfo; | |
21 | import org.seasar.cubby.internal.util.StringUtils; | |
22 | import org.seasar.cubby.validator.ScalarFieldValidator; | |
23 | import org.seasar.cubby.validator.ValidationContext; | |
24 | ||
25 | /** | |
26 | * 数値であるかを検証します。 | |
27 | * <p> | |
28 | * <table> | |
29 | * <caption>検証エラー時に設定するエラーメッセージ</caption> <tbody> | |
30 | * <tr> | |
31 | * <th scope="row">デフォルトのキー</th> | |
32 | * <td>valid.number</td> | |
33 | * </tr> | |
34 | * <tr> | |
35 | * <th scope="row">置換文字列</th> | |
36 | * <td> | |
37 | * <ol start="0"> | |
38 | * <li>フィールド名</li> | |
39 | * </ol></td> | |
40 | * </tr> | |
41 | * </tbody> | |
42 | * </table> | |
43 | * </p> | |
44 | * | |
45 | * @author agata | |
46 | * @author baba | |
47 | */ | |
48 | public class NumberValidator implements ScalarFieldValidator { | |
49 | ||
50 | 1 | private static final Pattern NUMBER_PATTERN = Pattern |
51 | .compile("^[-+]?[0-9]+[.]?[0-9]*$"); | |
52 | ||
53 | /** | |
54 | * メッセージキー。 | |
55 | */ | |
56 | private final String messageKey; | |
57 | ||
58 | /** | |
59 | * コンストラクタ | |
60 | */ | |
61 | public NumberValidator() { | |
62 | 6 | this("valid.number"); |
63 | 6 | } |
64 | ||
65 | /** | |
66 | * エラーメッセージキーを指定するコンストラクタ | |
67 | * | |
68 | * @param messageKey | |
69 | * エラーメッセージキー | |
70 | */ | |
71 | 6 | public NumberValidator(final String messageKey) { |
72 | 6 | this.messageKey = messageKey; |
73 | 6 | } |
74 | ||
75 | /** | |
76 | * {@inheritDoc} | |
77 | */ | |
78 | public void validate(final ValidationContext context, final Object value) { | |
79 | 9 | if (value instanceof String) { |
80 | 9 | final String str = (String) value; |
81 | 9 | if (StringUtils.isEmpty(str)) { |
82 | 0 | return; |
83 | } | |
84 | 9 | if (NUMBER_PATTERN.matcher(str).find()) { |
85 | 4 | return; |
86 | } | |
87 | 5 | } else if (value == null) { |
88 | 0 | return; |
89 | } | |
90 | ||
91 | 5 | final MessageInfo messageInfo = new MessageInfo(); |
92 | 5 | messageInfo.setKey(this.messageKey); |
93 | 5 | context.addMessageInfo(messageInfo); |
94 | 5 | } |
95 | ||
96 | } |