1 /** 2 * Copyright 2005-2012 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 21 import java.io.Serializable; 22 import java.util.regex.Matcher; 23 import java.util.regex.Pattern; 24 25 /** 26 * Abstraction of the regular expressions used to validate attribute values. 27 * 28 The validationPattern element defines the allowable character-level 29 or field-level values for an attribute. 30 31 JSTL: validationPattern is a Map which is accessed using a key 32 of "validationPattern". Each entry may contain some of the keys 33 listed below. The keys that may be present for a given attribute 34 are dependent upon the type of validationPattern. 35 36 * maxLength (String) 37 * exactLength 38 * type 39 * allowWhitespace 40 * allowUnderscore 41 * allowPeriod 42 * validChars 43 * precision 44 * scale 45 * allowNegative 46 47 The allowable keys (in addition to type) for each type are: 48 ****Type**** ***Keys*** 49 alphanumeric exactLength 50 maxLength 51 allowWhitespace 52 allowUnderscore 53 allowPeriod 54 55 alpha exactLength 56 maxLength 57 allowWhitespace 58 59 anyCharacter exactLength 60 maxLength 61 allowWhitespace 62 63 charset validChars 64 65 numeric exactLength 66 maxLength 67 68 fixedPoint allowNegative 69 precision 70 scale 71 72 floatingPoint allowNegative 73 74 date n/a 75 emailAddress n/a 76 javaClass n/a 77 month n/a 78 phoneNumber n/a 79 timestamp n/a 80 year n/a 81 zipcode n/a 82 83 Note: maxLength and exactLength are mutually exclusive. 84 If one is entered, the other may not be entered. 85 86 Note: See ApplicationResources.properties for 87 exact regex patterns. 88 e.g. validationPatternRegex.date for regex used in date validation. 89 */ 90 @Deprecated 91 abstract public class ValidationPattern implements Serializable { 92 // TODO: UNIT TEST: compile all patterns to test 93 94 /** 95 * @return regular expression Pattern generated by the individual ValidationPattern subclass 96 */ 97 abstract public Pattern getRegexPattern(); 98 99 /** 100 * @return String version of regular expression base, suitable for modification with length-specifiers and used internally by 101 * getRegexPattern 102 */ 103 abstract protected String getRegexString(); 104 105 106 /** 107 * @return true if the given String matches this pattern 108 */ 109 public boolean matches(String input) { 110 Pattern p = getRegexPattern(); 111 112 Matcher m = p.matcher(input); 113 114 return m.matches(); 115 } 116 117 /** 118 * @return ExportMap describing the subclass instance 119 */ 120 abstract public ExportMap buildExportMap(String exportKey); 121 122 abstract public String getValidationErrorMessageKey(); 123 124 public String[] getValidationErrorMessageParameters(String attributeLabel) { 125 return new String[] {attributeLabel}; 126 } 127 128 /** 129 * This method throws an exception if it is not configured properly 130 * 131 */ 132 public void completeValidation() throws ValidationPatternException { 133 } 134 135 /** exception thrown when a ValidationPattern is in an incorrect state. */ 136 public static class ValidationPatternException extends RiceRuntimeException { 137 138 private static final long serialVersionUID = 2012770642382150523L; 139 140 public ValidationPatternException(String message) { 141 super(message); 142 } 143 144 public ValidationPatternException() { 145 super(); 146 } 147 148 public ValidationPatternException(String message, Throwable cause) { 149 super(message, cause); 150 } 151 152 public ValidationPatternException(Throwable cause) { 153 super(cause); 154 } 155 } 156 }