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 java.util.regex.Matcher;
19  import java.util.regex.Pattern;
20  
21  import org.seasar.cubby.validator.MessageHelper;
22  import org.seasar.cubby.validator.ScalarFieldValidator;
23  import org.seasar.cubby.validator.ValidationContext;
24  import org.seasar.framework.util.StringUtil;
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   */
39  public class RegexpValidator implements ScalarFieldValidator {
40  
41  	/**
42  	 * メッセージヘルパ。
43  	 */
44  	private final MessageHelper messageHelper;
45  
46  	/**
47  	 * 正規表現パターン
48  	 */
49  	private final Pattern pattern;
50  
51  	/**
52  	 * コンストラクタ
53  	 * 
54  	 * @param regex
55  	 *            正規表現(例:".+\\.(?i)(png|jpg)")・・・「(?i)」は大文字小文字を区別しないフラグ
56  	 */
57  	public RegexpValidator(final String regex) {
58  		this(regex, "valid.regexp");
59  	}
60  
61  	/**
62  	 * エラーメッセージキーを指定するコンストラクタ
63  	 * 
64  	 * @param regex
65  	 *            正規表現(例:".+\\.(?i)(png|jpg)")・・・「(?i)」は大文字小文字を区別しないフラグ
66  	 * @param messageKey
67  	 *            エラーメッセージキー
68  	 */
69  	public RegexpValidator(final String regex, final String messageKey) {
70  		this(Pattern.compile(regex), messageKey);
71  	}
72  
73  	/**
74  	 * コンストラクタ
75  	 * 
76  	 * @param pattern 正規表現パターン
77  	 */
78  	public RegexpValidator(final Pattern pattern) {
79  		this(pattern, "valid.regexp");
80  	}
81  
82  	/**
83  	 * エラーメッセージキーを指定するコンストラクタ
84  	 * 
85  	 * @param pattern 正規表現パターン
86  	 * @param messageKey
87  	 *            エラーメッセージキー
88  	 */
89  	public RegexpValidator(Pattern pattern, String messageKey) {
90  		this.pattern = pattern;
91  		this.messageHelper = new MessageHelper(messageKey);
92  	}
93  
94  	public void validate(final ValidationContext context, final Object value) {
95  		if (value == null) {
96  			return;
97  		}
98  		if (value instanceof String) {
99  			final String stringValue = (String) value;
100 			if (StringUtil.isEmpty(stringValue)) {
101 				return;
102 			}
103 			final Matcher matcher = pattern.matcher(stringValue);
104 			if (matcher.matches()) {
105 				return;
106 			}
107 		}
108 		context.addMessageInfo(this.messageHelper.createMessageInfo());
109 	}
110 
111 }