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.core.api.config.property.ConfigurationService;
20 import org.kuali.rice.krad.service.KRADServiceLocator;
21 import org.kuali.rice.krad.uif.UifConstants;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.regex.Pattern;
26
27
28
29
30
31
32 public class CharsetPatternConstraint extends ValidCharactersPatternConstraint {
33 protected String validChars;
34
35
36
37
38 public String getValidChars() {
39 return validChars;
40 }
41
42
43
44
45 public void setValidChars(String validChars) {
46 if (StringUtils.isEmpty(validChars)) {
47 throw new IllegalArgumentException("invalid (empty) validChars");
48 }
49
50 this.validChars = validChars;
51 }
52
53
54
55
56
57
58
59 protected String getRegexString() {
60 if (StringUtils.isEmpty(validChars)) {
61 throw new IllegalStateException("validChars is empty");
62 }
63
64
65 Pattern filteringChars = Pattern.compile("([\\-\\[\\]\\{\\}\\$\\.\\^\\(\\)\\*\\&\\|])");
66 String filteredChars = filteringChars.matcher(validChars).replaceAll("\\\\$1");
67
68 StringBuffer regexString = new StringBuffer("[");
69 regexString.append(filteredChars);
70 if (filteredChars.endsWith("\\")) {
71 regexString.append("\\");
72 }
73 regexString.append("]");
74
75 return regexString.toString();
76 }
77
78
79
80
81
82 @Override
83 public String getLabelKey() {
84 String labelKey = super.getLabelKey();
85 if (StringUtils.isNotEmpty(labelKey)) {
86 return labelKey;
87 }
88 return (UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "charsetPattern");
89 }
90
91
92
93
94
95 public List<String> getValidationMessageParams() {
96 if(validationMessageParams == null){
97 validationMessageParams = new ArrayList<String>();
98 if (StringUtils.isNotBlank(validChars)) {
99 validationMessageParams.add(validChars);
100 }
101
102 }
103 return this.validationMessageParams;
104 }
105
106 }