Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
RegexpValidator |
|
| 2.4;2.4 |
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.Matcher; | |
19 | import java.util.regex.Pattern; | |
20 | ||
21 | import org.seasar.cubby.internal.util.StringUtils; | |
22 | import org.seasar.cubby.validator.MessageHelper; | |
23 | import org.seasar.cubby.validator.ScalarFieldValidator; | |
24 | import org.seasar.cubby.validator.ValidationContext; | |
25 | ||
26 | /** | |
27 | * 指定された正規表現にマッチするか検証します。 | |
28 | * <p> | |
29 | * 正規表現についての詳細は {@link Pattern}を参照してください。 | |
30 | * </p> | |
31 | * <p> | |
32 | * デフォルトエラーメッセージキー:valid.regexp | |
33 | * </p> | |
34 | * | |
35 | * @author baba | |
36 | * @see Pattern | |
37 | * @see Matcher | |
38 | * @since 1.0.0 | |
39 | */ | |
40 | public class RegexpValidator implements ScalarFieldValidator { | |
41 | ||
42 | /** | |
43 | * メッセージヘルパ。 | |
44 | */ | |
45 | private final MessageHelper messageHelper; | |
46 | ||
47 | /** | |
48 | * 正規表現パターン | |
49 | */ | |
50 | private final Pattern pattern; | |
51 | ||
52 | /** | |
53 | * コンストラクタ | |
54 | * | |
55 | * @param regex | |
56 | * 正規表現(例:".+\\.(?i)(png|jpg)")・・・「(?i)」は大文字小文字を区別しないフラグ | |
57 | */ | |
58 | public RegexpValidator(final String regex) { | |
59 | 1 | this(regex, "valid.regexp"); |
60 | 1 | } |
61 | ||
62 | /** | |
63 | * エラーメッセージキーを指定するコンストラクタ | |
64 | * | |
65 | * @param regex | |
66 | * 正規表現(例:".+\\.(?i)(png|jpg)")・・・「(?i)」は大文字小文字を区別しないフラグ | |
67 | * @param messageKey | |
68 | * エラーメッセージキー | |
69 | */ | |
70 | public RegexpValidator(final String regex, final String messageKey) { | |
71 | 1 | this(Pattern.compile(regex), messageKey); |
72 | 1 | } |
73 | ||
74 | /** | |
75 | * コンストラクタ | |
76 | * | |
77 | * @param pattern | |
78 | * 正規表現パターン | |
79 | */ | |
80 | public RegexpValidator(final Pattern pattern) { | |
81 | 1 | this(pattern, "valid.regexp"); |
82 | 1 | } |
83 | ||
84 | /** | |
85 | * エラーメッセージキーを指定するコンストラクタ | |
86 | * | |
87 | * @param pattern | |
88 | * 正規表現パターン | |
89 | * @param messageKey | |
90 | * エラーメッセージキー | |
91 | */ | |
92 | 2 | public RegexpValidator(final Pattern pattern, final String messageKey) { |
93 | 2 | this.pattern = pattern; |
94 | 2 | this.messageHelper = new MessageHelper(messageKey); |
95 | 2 | } |
96 | ||
97 | /** | |
98 | * {@inheritDoc} | |
99 | */ | |
100 | public void validate(final ValidationContext context, final Object value) { | |
101 | 11 | if (value == null) { |
102 | 3 | return; |
103 | } | |
104 | 8 | if (value instanceof String) { |
105 | 8 | final String stringValue = (String) value; |
106 | 8 | if (StringUtils.isEmpty(stringValue)) { |
107 | 3 | return; |
108 | } | |
109 | 5 | final Matcher matcher = pattern.matcher(stringValue); |
110 | 5 | if (matcher.matches()) { |
111 | 3 | return; |
112 | } | |
113 | } | |
114 | 2 | context.addMessageInfo(this.messageHelper.createMessageInfo()); |
115 | 2 | } |
116 | ||
117 | } |