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-bean", 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 protected String getRegexString() {
62 if (StringUtils.isEmpty(validChars)) {
63 throw new IllegalStateException("validChars is empty");
64 }
65
66
67 Pattern filteringChars = Pattern.compile("([\\-\\[\\]\\{\\}\\$\\.\\^\\(\\)\\*\\&\\|])");
68 String filteredChars = filteringChars.matcher(validChars).replaceAll("\\\\$1");
69
70 StringBuffer regexString = new StringBuffer("[");
71 regexString.append(filteredChars);
72 if (filteredChars.endsWith("\\")) {
73 regexString.append("\\");
74 }
75 regexString.append("]");
76
77 return regexString.toString();
78 }
79
80
81
82
83 @Override
84 public String getMessageKey() {
85 String messageKey = super.getMessageKey();
86 if (StringUtils.isNotEmpty(messageKey)) {
87 return messageKey;
88 }
89
90 return (UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "charsetPattern");
91 }
92
93
94
95
96
97
98 public List<String> getValidationMessageParams() {
99 if (validationMessageParams == null) {
100 validationMessageParams = new ArrayList<String>();
101 if (StringUtils.isNotBlank(validChars)) {
102 validationMessageParams.add(validChars);
103 }
104
105 }
106 return this.validationMessageParams;
107 }
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 }