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 abstract public class CharacterLevelValidationPattern extends ValidationPattern {
29 protected Pattern regexPattern;
30
31 protected int maxLength = -1;
32 protected int exactLength = -1;
33
34
35
36
37
38
39 public void setMaxLength(int maxLength) {
40 if (this.exactLength != -1) {
41 throw new IllegalStateException("illegal attempt to set maxLength after mutually-exclusive exactLength has been set");
42 }
43
44 this.maxLength = maxLength;
45 }
46
47
48
49
50 public int getMaxLength() {
51 return maxLength;
52 }
53
54
55
56
57
58
59
60 public void setExactLength(int exactLength) {
61 if (this.maxLength != -1) {
62 throw new IllegalStateException("illegal attempt to set exactLength after mutually-exclusive maxLength has been set");
63 }
64
65 this.exactLength = exactLength;
66 }
67
68
69
70
71 public int getExactLength() {
72 return exactLength;
73 }
74
75
76
77
78
79 final public Pattern getRegexPattern() {
80 if ( regexPattern == null ) {
81 String regexString = getRegexString();
82
83 StringBuffer completeRegex = new StringBuffer("^");
84 completeRegex.append(getRegexString());
85
86 if (maxLength != -1) {
87 completeRegex.append("{0," + maxLength + "}");
88 }
89 else if (exactLength != -1) {
90 completeRegex.append("{" + exactLength + "}");
91 }
92 else {
93 completeRegex.append("*");
94 }
95
96 completeRegex.append("$");
97
98 regexPattern = Pattern.compile(completeRegex.toString());
99 }
100 return regexPattern;
101 }
102
103
104
105
106
107 final public ExportMap buildExportMap(String exportKey) {
108 ExportMap exportMap = new ExportMap(exportKey);
109
110 if (getMaxLength() != -1) {
111 exportMap.set("maxLength", Integer.toString(getMaxLength()));
112 }
113 else if (getExactLength() != -1) {
114 exportMap.set("exactLength", Integer.toString(getExactLength()));
115 }
116
117 extendExportMap(exportMap);
118
119 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 if (getMaxLength() != -1) {
132 return new String[] {attributeLabel, String.valueOf(getMaxLength())};
133 }
134 if (getExactLength() != -1) {
135 return new String[] {attributeLabel, String.valueOf(getExactLength())};
136 }
137 return new String[] {attributeLabel};
138 }
139
140
141
142
143
144
145 @Override
146 public String getValidationErrorMessageKey() {
147 StringBuilder buf = new StringBuilder();
148 buf.append("error.format.").append(getClass().getName()).append(getValidationErrorMessageKeyOptions());
149 if (getMaxLength() != -1) {
150 buf.append(".maxLength");
151 }
152 if (getExactLength() != -1) {
153 buf.append(".exactLength");
154 }
155 return buf.toString();
156 }
157
158 protected String getValidationErrorMessageKeyOptions() {
159 return KNSConstants.EMPTY_STRING;
160 }
161 }