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