1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kns.datadictionary.validation; |
17 | |
|
18 | |
import java.util.regex.Pattern; |
19 | |
|
20 | |
import org.kuali.rice.kns.datadictionary.exporter.ExportMap; |
21 | |
import org.kuali.rice.kns.util.KNSConstants; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | 0 | abstract public class CharacterLevelValidationPattern extends ValidationPattern { |
29 | |
protected Pattern regexPattern; |
30 | |
|
31 | 0 | protected int maxLength = -1; |
32 | 0 | protected int exactLength = -1; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public void setMaxLength(int maxLength) { |
40 | 0 | if (this.exactLength != -1) { |
41 | 0 | throw new IllegalStateException("illegal attempt to set maxLength after mutually-exclusive exactLength has been set"); |
42 | |
} |
43 | |
|
44 | 0 | this.maxLength = maxLength; |
45 | 0 | } |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
public int getMaxLength() { |
51 | 0 | return maxLength; |
52 | |
} |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
public void setExactLength(int exactLength) { |
61 | 0 | if (this.maxLength != -1) { |
62 | 0 | throw new IllegalStateException("illegal attempt to set exactLength after mutually-exclusive maxLength has been set"); |
63 | |
} |
64 | |
|
65 | 0 | this.exactLength = exactLength; |
66 | 0 | } |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
public int getExactLength() { |
72 | 0 | return exactLength; |
73 | |
} |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
final public Pattern getRegexPattern() { |
80 | 0 | if ( regexPattern == null ) { |
81 | 0 | String regexString = getRegexString(); |
82 | |
|
83 | 0 | StringBuffer completeRegex = new StringBuffer("^"); |
84 | 0 | completeRegex.append(getRegexString()); |
85 | |
|
86 | 0 | if (maxLength != -1) { |
87 | 0 | completeRegex.append("{0," + maxLength + "}"); |
88 | |
} |
89 | 0 | else if (exactLength != -1) { |
90 | 0 | completeRegex.append("{" + exactLength + "}"); |
91 | |
} |
92 | |
else { |
93 | 0 | completeRegex.append("*"); |
94 | |
} |
95 | |
|
96 | 0 | completeRegex.append("$"); |
97 | |
|
98 | 0 | regexPattern = Pattern.compile(completeRegex.toString()); |
99 | |
} |
100 | 0 | return regexPattern; |
101 | |
} |
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
final public ExportMap buildExportMap(String exportKey) { |
108 | 0 | ExportMap exportMap = new ExportMap(exportKey); |
109 | |
|
110 | 0 | if (getMaxLength() != -1) { |
111 | 0 | exportMap.set("maxLength", Integer.toString(getMaxLength())); |
112 | |
} |
113 | 0 | else if (getExactLength() != -1) { |
114 | 0 | exportMap.set("exactLength", Integer.toString(getExactLength())); |
115 | |
} |
116 | |
|
117 | 0 | extendExportMap(exportMap); |
118 | |
|
119 | 0 | return exportMap; |
120 | |
} |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
abstract public void extendExportMap(ExportMap exportMap); |
128 | |
|
129 | |
@Override |
130 | |
public String[] getValidationErrorMessageParameters(String attributeLabel) { |
131 | 0 | if (getMaxLength() != -1) { |
132 | 0 | return new String[] {attributeLabel, String.valueOf(getMaxLength())}; |
133 | |
} |
134 | 0 | if (getExactLength() != -1) { |
135 | 0 | return new String[] {attributeLabel, String.valueOf(getExactLength())}; |
136 | |
} |
137 | 0 | return new String[] {attributeLabel}; |
138 | |
} |
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
@Override |
146 | |
public String getValidationErrorMessageKey() { |
147 | 0 | StringBuilder buf = new StringBuilder(); |
148 | 0 | buf.append("error.format.").append(getClass().getName()).append(getValidationErrorMessageKeyOptions()); |
149 | 0 | if (getMaxLength() != -1) { |
150 | 0 | buf.append(".maxLength"); |
151 | |
} |
152 | 0 | if (getExactLength() != -1) { |
153 | 0 | buf.append(".exactLength"); |
154 | |
} |
155 | 0 | return buf.toString(); |
156 | |
} |
157 | |
|
158 | |
protected String getValidationErrorMessageKeyOptions() { |
159 | 0 | return KNSConstants.EMPTY_STRING; |
160 | |
} |
161 | |
} |