1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.datadictionary.validation.charlevel;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.krad.datadictionary.exporter.ExportMap;
20 import org.kuali.rice.krad.datadictionary.validation.CharacterLevelValidationPattern;
21
22 import java.util.regex.Pattern;
23
24
25
26
27
28
29 public class CharsetValidationPattern extends CharacterLevelValidationPattern {
30 protected String validChars;
31
32
33
34
35 public String getValidChars() {
36 return validChars;
37 }
38
39
40
41
42 public void setValidChars(String validChars) {
43 if (StringUtils.isEmpty(validChars)) {
44 throw new IllegalArgumentException("invalid (empty) validChars");
45 }
46
47 this.validChars = validChars;
48 }
49
50
51
52
53
54
55
56 protected String getRegexString() {
57 if (StringUtils.isEmpty(validChars)) {
58 throw new IllegalStateException("validChars is empty");
59 }
60
61
62 Pattern filteringChars = Pattern.compile("([\\-\\[\\]\\{\\}\\$\\.\\^\\(\\)\\*\\&\\|])");
63 String filteredChars = filteringChars.matcher(validChars).replaceAll("\\\\$1");
64
65 StringBuffer regexString = new StringBuffer("[");
66 regexString.append(filteredChars);
67 if (filteredChars.endsWith("\\")) {
68 regexString.append("\\");
69 }
70 regexString.append("]");
71
72 return regexString.toString();
73 }
74
75
76
77
78
79 public void extendExportMap(ExportMap exportMap) {
80 exportMap.set("type", "charset");
81
82 exportMap.set("validChars", getValidChars());
83 }
84
85
86
87
88
89
90 @Override
91 public String[] getValidationErrorMessageParameters(String attributeLabel) {
92
93 StringBuilder buf = new StringBuilder();
94 for (int i = 0; i < validChars.length(); i++) {
95 buf.append(validChars.charAt(i));
96 if (i != validChars.length() - 1) {
97 buf.append(", ");
98 }
99 }
100 String characterList = buf.toString();
101
102 if (getMaxLength() != -1) {
103 return new String[] {attributeLabel, String.valueOf(getMaxLength()), characterList};
104 }
105 if (getExactLength() != -1) {
106 return new String[] {attributeLabel, String.valueOf(getExactLength()), characterList};
107 }
108 return new String[] {attributeLabel, "0", characterList};
109 }
110 }