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