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.validator.MessageHelper;
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 public class FileRegexpValidator implements ScalarFieldValidator {
41
42
43
44
45 private final MessageHelper messageHelper;
46
47
48
49
50 private final Pattern pattern;
51
52
53
54
55
56
57
58 public FileRegexpValidator(final String regex) {
59 this(regex, "valid.fileRegexp");
60 }
61
62
63
64
65
66
67
68
69
70 public FileRegexpValidator(final String regex, final String messageKey) {
71 this(Pattern.compile(regex), messageKey);
72 }
73
74
75
76
77
78
79
80 public FileRegexpValidator(final Pattern pattern) {
81 this(pattern, "valid.fileRegexp");
82 }
83
84
85
86
87
88
89
90
91
92 public FileRegexpValidator(final Pattern pattern, final String messageKey) {
93 this.pattern = pattern;
94 this.messageHelper = new MessageHelper(messageKey);
95 }
96
97
98
99
100 public void validate(final ValidationContext context, final Object value) {
101 if (value instanceof FileItem) {
102 final FileItem fileItem = (FileItem) value;
103 final Matcher matcher = pattern.matcher(fileItem.getName());
104 if (!matcher.matches()) {
105 context.addMessageInfo(this.messageHelper.createMessageInfo());
106 }
107 }
108 }
109 }