1 /** 2 * Copyright 2005-2014 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 101 * internally by 102 * getRegexPattern 103 */ 104 abstract protected String getRegexString(); 105 106 /** 107 * Determines if an input string matches the pattern. 108 * 109 * @param input input string 110 * @return true if the given String matches this pattern 111 */ 112 public boolean matches(String input) { 113 Pattern p = getRegexPattern(); 114 115 Matcher m = p.matcher(input); 116 117 return m.matches(); 118 } 119 120 /** 121 * Builds an export map describing the subclass instance. 122 * @param exportKey 123 * 124 * @return export map 125 */ 126 abstract public ExportMap buildExportMap(String exportKey); 127 128 abstract public String getValidationErrorMessageKey(); 129 130 public String[] getValidationErrorMessageParameters(String attributeLabel) { 131 return new String[]{attributeLabel}; 132 } 133 134 /** 135 * This method throws an exception if it is not configured properly 136 * @throws ValidationPatternException 137 */ 138 public void completeValidation() throws ValidationPatternException { 139 } 140 141 /** 142 * exception thrown when a ValidationPattern is in an incorrect state. 143 */ 144 public static class ValidationPatternException extends RiceRuntimeException { 145 146 private static final long serialVersionUID = 2012770642382150523L; 147 148 public ValidationPatternException(String message) { 149 super(message); 150 } 151 152 public ValidationPatternException() { 153 super(); 154 } 155 156 public ValidationPatternException(String message, Throwable cause) { 157 super(message, cause); 158 } 159 160 public ValidationPatternException(Throwable cause) { 161 super(cause); 162 } 163 } 164 }