1 /**
2 * Copyright 2005-2015 The Kuali Foundation
3 *
4 * Licensed under the Educational Community License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/ecl2.php
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.kuali.rice.krad.datadictionary.validation;
17
18 import org.kuali.rice.core.api.exception.RiceRuntimeException;
19 import org.kuali.rice.krad.datadictionary.exporter.ExportMap;
20 import org.kuali.rice.krad.datadictionary.validation.constraint.ConfigurationBasedRegexPatternConstraint;
21 import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint;
22
23 import java.io.Serializable;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26
27 /**
28 * Abstraction of the regular expressions used to validate attribute values.
29 *
30 The validationPattern element defines the allowable character-level
31 or field-level values for an attribute.
32
33 JSTL: validationPattern is a Map which is accessed using a key
34 of "validationPattern". Each entry may contain some of the keys
35 listed below. The keys that may be present for a given attribute
36 are dependent upon the type of validationPattern.
37
38 * maxLength (String)
39 * exactLength
40 * type
41 * allowWhitespace
42 * allowUnderscore
43 * allowPeriod
44 * validChars
45 * precision
46 * scale
47 * allowNegative
48
49 The allowable keys (in addition to type) for each type are:
50 ****Type**** ***Keys***
51 alphanumeric exactLength
52 maxLength
53 allowWhitespace
54 allowUnderscore
55 allowPeriod
56
57 alpha exactLength
58 maxLength
59 allowWhitespace
60
61 anyCharacter exactLength
62 maxLength
63 allowWhitespace
64
65 charset validChars
66
67 numeric exactLength
68 maxLength
69
70 fixedPoint allowNegative
71 precision
72 scale
73
74 floatingPoint allowNegative
75
76 date n/a
77 emailAddress n/a
78 javaClass n/a
79 month n/a
80 phoneNumber n/a
81 timestamp n/a
82 year n/a
83 zipcode n/a
84
85 Note: maxLength and exactLength are mutually exclusive.
86 If one is entered, the other may not be entered.
87
88 Note: See ApplicationResources.properties for
89 exact regex patterns.
90 e.g. validationPatternRegex.date for regex used in date validation.
91 */
92 @Deprecated
93 abstract public class ValidationPattern implements Serializable {
94 // TODO: UNIT TEST: compile all patterns to test
95
96 /**
97 * @return regular expression Pattern generated by the individual ValidationPattern subclass
98 */
99 abstract public Pattern getRegexPattern();
100
101 /**
102 * @return String version of regular expression base, suitable for modification with length-specifiers and used internally by
103 * getRegexPattern
104 */
105 abstract protected String getRegexString();
106
107
108 /**
109 * @return true if the given String matches this pattern
110 */
111 public boolean matches(String input) {
112 Pattern p = getRegexPattern();
113
114 Matcher m = p.matcher(input);
115
116 return m.matches();
117 }
118
119 /**
120 * @return ExportMap describing the subclass instance
121 */
122 abstract public ExportMap buildExportMap(String exportKey);
123
124 abstract public String getValidationErrorMessageKey();
125
126 public String[] getValidationErrorMessageParameters(String attributeLabel) {
127 return new String[] {attributeLabel};
128 }
129
130 /**
131 * This method throws an exception if it is not configured properly
132 *
133 */
134 public void completeValidation() throws ValidationPatternException {
135 }
136
137 /** exception thrown when a ValidationPattern is in an incorrect state. */
138 public static class ValidationPatternException extends RiceRuntimeException {
139
140 private static final long serialVersionUID = 2012770642382150523L;
141
142 public ValidationPatternException(String message) {
143 super(message);
144 }
145
146 public ValidationPatternException() {
147 super();
148 }
149
150 public ValidationPatternException(String message, Throwable cause) {
151 super(message, cause);
152 }
153
154 public ValidationPatternException(Throwable cause) {
155 super(cause);
156 }
157 }
158 }