View Javadoc

1   /*
2    * Copyright 2004-2010 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  
17  package org.seasar.cubby.validator.validators;
18  
19  import java.util.regex.Matcher;
20  import java.util.regex.Pattern;
21  
22  import org.seasar.cubby.action.MessageInfo;
23  import org.seasar.cubby.internal.util.StringUtils;
24  import org.seasar.cubby.validator.ScalarFieldValidator;
25  import org.seasar.cubby.validator.ValidationContext;
26  
27  /**
28   * 指定された正規表現にマッチするかを検証します。
29   * <p>
30   * <table>
31   * <caption>検証エラー時に設定するエラーメッセージ</caption> <tbody>
32   * <tr>
33   * <th scope="row">デフォルトのキー</th>
34   * <td>valid.regexp</td>
35   * </tr>
36   * <tr>
37   * <th scope="row">置換文字列</th>
38   * <td>
39   * <ol start="0">
40   * <li>フィールド名</li>
41   * </ol></td>
42   * </tr>
43   * </tbody>
44   * </table>
45   * </p>
46   * 
47   * @see Pattern
48   * @author baba
49   */
50  public class RegexpValidator implements ScalarFieldValidator {
51  
52  	/**
53  	 * メッセージキー。
54  	 */
55  	private final String messageKey;
56  
57  	/**
58  	 * 正規表現パターン
59  	 */
60  	private final Pattern pattern;
61  
62  	/**
63  	 * コンストラクタ
64  	 * 
65  	 * @param regex
66  	 *            正規表現(例:".+\\.(?i)(png|jpg)")・・・「(?i)」は大文字小文字を区別しないフラグ
67  	 */
68  	public RegexpValidator(final String regex) {
69  		this(regex, "valid.regexp");
70  	}
71  
72  	/**
73  	 * エラーメッセージキーを指定するコンストラクタ
74  	 * 
75  	 * @param regex
76  	 *            正規表現(例:".+\\.(?i)(png|jpg)")・・・「(?i)」は大文字小文字を区別しないフラグ
77  	 * @param messageKey
78  	 *            エラーメッセージキー
79  	 */
80  	public RegexpValidator(final String regex, final String messageKey) {
81  		this(Pattern.compile(regex), messageKey);
82  	}
83  
84  	/**
85  	 * コンストラクタ
86  	 * 
87  	 * @param pattern
88  	 *            正規表現パターン
89  	 */
90  	public RegexpValidator(final Pattern pattern) {
91  		this(pattern, "valid.regexp");
92  	}
93  
94  	/**
95  	 * エラーメッセージキーを指定するコンストラクタ
96  	 * 
97  	 * @param pattern
98  	 *            正規表現パターン
99  	 * @param messageKey
100 	 *            エラーメッセージキー
101 	 */
102 	public RegexpValidator(final Pattern pattern, final String messageKey) {
103 		this.pattern = pattern;
104 		this.messageKey = messageKey;
105 	}
106 
107 	/**
108 	 * {@inheritDoc}
109 	 */
110 	public void validate(final ValidationContext context, final Object value) {
111 		if (value == null) {
112 			return;
113 		}
114 		if (value instanceof String) {
115 			final String stringValue = (String) value;
116 			if (StringUtils.isEmpty(stringValue)) {
117 				return;
118 			}
119 			final Matcher matcher = pattern.matcher(stringValue);
120 			if (matcher.matches()) {
121 				return;
122 			}
123 		}
124 
125 		final MessageInfo messageInfo = new MessageInfo();
126 		messageInfo.setKey(this.messageKey);
127 		context.addMessageInfo(messageInfo);
128 	}
129 
130 }