Coverage Report - org.seasar.cubby.validator.validators.FileRegexpValidator
 
Classes in this File Line Coverage Branch Coverage Complexity
FileRegexpValidator
100%
19/19
100%
4/4
1.6
 
 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.apache.commons.fileupload.FileItem;
 23  
 import org.seasar.cubby.action.MessageInfo;
 24  
 import org.seasar.cubby.validator.ScalarFieldValidator;
 25  
 import org.seasar.cubby.validator.ValidationContext;
 26  
 
 27  
 /**
 28  
  * アップロードされたファイル({@link FileItem})のファイル名が指定された正規表現にマッチするか検証します。
 29  
  * <p>
 30  
  * <table>
 31  
  * <caption>検証エラー時に設定するエラーメッセージ</caption> <tbody>
 32  
  * <tr>
 33  
  * <th scope="row">デフォルトのキー</th>
 34  
  * <td>valid.fileRegexp</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 FileRegexpValidator 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 FileRegexpValidator(final String regex) {
 69  1
                 this(regex, "valid.fileRegexp");
 70  1
         }
 71  
 
 72  
         /**
 73  
          * エラーメッセージキーを指定するコンストラクタ
 74  
          * 
 75  
          * @param regex
 76  
          *            正規表現(例:".+\\.(?i)(png|jpg)")・・・「(?i)」は大文字小文字を区別しないフラグ
 77  
          * @param messageKey
 78  
          *            エラーメッセージキー
 79  
          */
 80  
         public FileRegexpValidator(final String regex, final String messageKey) {
 81  1
                 this(Pattern.compile(regex), messageKey);
 82  1
         }
 83  
 
 84  
         /**
 85  
          * コンストラクタ
 86  
          * 
 87  
          * @param pattern
 88  
          *            正規表現パターン
 89  
          */
 90  
         public FileRegexpValidator(final Pattern pattern) {
 91  1
                 this(pattern, "valid.fileRegexp");
 92  1
         }
 93  
 
 94  
         /**
 95  
          * エラーメッセージキーを指定するコンストラクタ
 96  
          * 
 97  
          * @param pattern
 98  
          *            正規表現パターン
 99  
          * @param messageKey
 100  
          *            エラーメッセージキー
 101  
          */
 102  2
         public FileRegexpValidator(final Pattern pattern, final String messageKey) {
 103  2
                 this.pattern = pattern;
 104  2
                 this.messageKey = messageKey;
 105  2
         }
 106  
 
 107  
         /**
 108  
          * {@inheritDoc}
 109  
          */
 110  
         public void validate(final ValidationContext context, final Object value) {
 111  11
                 if (value instanceof FileItem) {
 112  5
                         final FileItem fileItem = (FileItem) value;
 113  5
                         final Matcher matcher = pattern.matcher(fileItem.getName());
 114  5
                         if (matcher.matches()) {
 115  3
                                 return;
 116  
                         }
 117  
 
 118  2
                         final MessageInfo messageInfo = new MessageInfo();
 119  2
                         messageInfo.setKey(this.messageKey);
 120  2
                         context.addMessageInfo(messageInfo);
 121  
                 }
 122  8
         }
 123  
 }