1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
65
66
67 public FileRegexpValidator(final String regex) {
68 this(regex, "valid.fileRegexp");
69 }
70
71
72
73
74
75
76
77
78
79 public FileRegexpValidator(final String regex, final String messageKey) {
80 this(Pattern.compile(regex), messageKey);
81 }
82
83
84
85
86
87
88
89 public FileRegexpValidator(final Pattern pattern) {
90 this(pattern, "valid.fileRegexp");
91 }
92
93
94
95
96
97
98
99
100
101 public FileRegexpValidator(final Pattern pattern, final String messageKey) {
102 this.pattern = pattern;
103 this.messageKey = messageKey;
104 }
105
106
107
108
109 public void validate(final ValidationContext context, final Object value) {
110 if (value instanceof FileItem) {
111 final FileItem fileItem = (FileItem) value;
112 final Matcher matcher = pattern.matcher(fileItem.getName());
113 if (matcher.matches()) {
114 return;
115 }
116
117 final MessageInfo messageInfo = new MessageInfo();
118 messageInfo.setKey(this.messageKey);
119 context.addMessageInfo(messageInfo);
120 }
121 }
122 }