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