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 @Deprecated
30 public class CharsetValidationPattern extends CharacterLevelValidationPattern {
31 protected String validChars;
32
33
34
35
36 public String getValidChars() {
37 return validChars;
38 }
39
40
41
42
43 public void setValidChars(String validChars) {
44 if (StringUtils.isEmpty(validChars)) {
45 throw new IllegalArgumentException("invalid (empty) validChars");
46 }
47
48 this.validChars = validChars;
49 }
50
51
52
53
54
55
56
57 protected String getRegexString() {
58 if (StringUtils.isEmpty(validChars)) {
59 throw new IllegalStateException("validChars is empty");
60 }
61
62
63 Pattern filteringChars = Pattern.compile("([\\-\\[\\]\\{\\}\\$\\.\\^\\(\\)\\*\\&\\|])");
64 String filteredChars = filteringChars.matcher(validChars).replaceAll("\\\\$1");
65
66 StringBuffer regexString = new StringBuffer("[");
67 regexString.append(filteredChars);
68 if (filteredChars.endsWith("\\")) {
69 regexString.append("\\");
70 }
71 regexString.append("]");
72
73 return regexString.toString();
74 }
75
76
77
78
79
80 public void extendExportMap(ExportMap exportMap) {
81 exportMap.set("type", "charset");
82
83 exportMap.set("validChars", getValidChars());
84 }
85
86
87
88
89
90
91 @Override
92 public String[] getValidationErrorMessageParameters(String attributeLabel) {
93
94 StringBuilder buf = new StringBuilder();
95 for (int i = 0; i < validChars.length(); i++) {
96 buf.append(validChars.charAt(i));
97 if (i != validChars.length() - 1) {
98 buf.append(", ");
99 }
100 }
101 String characterList = buf.toString();
102
103 if (getMaxLength() != -1) {
104 return new String[] {attributeLabel, String.valueOf(getMaxLength()), characterList};
105 }
106 if (getExactLength() != -1) {
107 return new String[] {attributeLabel, String.valueOf(getExactLength()), characterList};
108 }
109 return new String[] {attributeLabel, "0", characterList};
110 }
111 }