1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.datadictionary.validation.constraint;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.krad.datadictionary.parse.BeanTag;
20 import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
21 import org.kuali.rice.krad.datadictionary.validator.ValidationTrace;
22 import org.kuali.rice.krad.uif.UifConstants;
23
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.regex.Pattern;
27
28
29
30
31
32
33 @BeanTag(name = "charsetPatternConstraint", parent = "CharsetPatternConstraint")
34 public class CharsetPatternConstraint extends ValidCharactersPatternConstraint {
35 protected String validChars;
36
37
38
39
40 @BeanTagAttribute(name = "validChars")
41 public String getValidChars() {
42 return validChars;
43 }
44
45
46
47
48 public void setValidChars(String validChars) {
49 if (StringUtils.isEmpty(validChars)) {
50 throw new IllegalArgumentException("invalid (empty) validChars");
51 }
52
53 this.validChars = validChars;
54 }
55
56
57
58
59
60
61 @Override
62 protected String getRegexString() {
63 if (StringUtils.isEmpty(validChars)) {
64 throw new IllegalStateException("validChars is empty");
65 }
66
67
68 Pattern filteringChars = Pattern.compile("([\\-\\[\\]\\{\\}\\$\\.\\^\\(\\)\\*\\&\\|])");
69 String filteredChars = filteringChars.matcher(validChars).replaceAll("\\\\$1");
70
71 StringBuffer regexString = new StringBuffer("[");
72 regexString.append(filteredChars);
73 if (filteredChars.endsWith("\\")) {
74 regexString.append("\\");
75 }
76 regexString.append("]");
77
78 return regexString.toString();
79 }
80
81
82
83
84 @Override
85 public String getMessageKey() {
86 String messageKey = super.getMessageKey();
87 if (StringUtils.isNotEmpty(messageKey)) {
88 return messageKey;
89 }
90
91 return (UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "charsetPattern");
92 }
93
94
95
96
97
98
99 public List<String> getValidationMessageParams() {
100 if (validationMessageParams == null) {
101 validationMessageParams = new ArrayList<String>();
102 if (StringUtils.isNotBlank(validChars)) {
103 validationMessageParams.add(validChars);
104 }
105
106 }
107 return this.validationMessageParams;
108 }
109
110
111
112
113
114
115
116 @Override
117 public void completeValidation(ValidationTrace tracer) {
118 tracer.addBean("CharsetPatternConstraint", getMessageKey());
119
120 if (getValidChars() == null) {
121 String currentValues[] = {"validChars =" + getValidChars()};
122 tracer.createError("ValidChars must be set", currentValues);
123 }
124
125 super.completeValidation(tracer.getCopy());
126 }
127 }