001 /** 002 * Copyright 2005-2013 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.krad.datadictionary.validation; 017 018 import org.kuali.rice.core.api.exception.RiceRuntimeException; 019 import org.kuali.rice.krad.datadictionary.exporter.ExportMap; 020 021 import java.io.Serializable; 022 import java.util.regex.Matcher; 023 import java.util.regex.Pattern; 024 025 /** 026 * Abstraction of the regular expressions used to validate attribute values. 027 * 028 * The validationPattern element defines the allowable character-level 029 * or field-level values for an attribute. 030 * 031 * JSTL: validationPattern is a Map which is accessed using a key 032 * of "validationPattern". Each entry may contain some of the keys 033 * listed below. The keys that may be present for a given attribute 034 * are dependent upon the type of validationPattern. 035 * 036 * maxLength (String) 037 * exactLength 038 * type 039 * allowWhitespace 040 * allowUnderscore 041 * allowPeriod 042 * validChars 043 * precision 044 * scale 045 * allowNegative 046 * 047 * The allowable keys (in addition to type) for each type are: 048 * ***Type**** ***Keys*** 049 * alphanumeric exactLength 050 * maxLength 051 * allowWhitespace 052 * allowUnderscore 053 * allowPeriod 054 * 055 * alpha exactLength 056 * maxLength 057 * allowWhitespace 058 * 059 * anyCharacter exactLength 060 * maxLength 061 * allowWhitespace 062 * 063 * charset validChars 064 * 065 * numeric exactLength 066 * maxLength 067 * 068 * fixedPoint allowNegative 069 * precision 070 * scale 071 * 072 * floatingPoint allowNegative 073 * 074 * date n/a 075 * emailAddress n/a 076 * javaClass n/a 077 * month n/a 078 * phoneNumber n/a 079 * timestamp n/a 080 * year n/a 081 * zipcode n/a 082 * 083 * Note: maxLength and exactLength are mutually exclusive. 084 * If one is entered, the other may not be entered. 085 * 086 * Note: See ApplicationResources.properties for 087 * exact regex patterns. 088 * e.g. validationPatternRegex.date for regex used in date validation. 089 */ 090 @Deprecated 091 abstract public class ValidationPattern implements Serializable { 092 // TODO: UNIT TEST: compile all patterns to test 093 094 /** 095 * @return regular expression Pattern generated by the individual ValidationPattern subclass 096 */ 097 abstract public Pattern getRegexPattern(); 098 099 /** 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 * @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 public void completeValidation() throws ValidationPatternException { 132 } 133 134 /** 135 * exception thrown when a ValidationPattern is in an incorrect state. 136 */ 137 public static class ValidationPatternException extends RiceRuntimeException { 138 139 private static final long serialVersionUID = 2012770642382150523L; 140 141 public ValidationPatternException(String message) { 142 super(message); 143 } 144 145 public ValidationPatternException() { 146 super(); 147 } 148 149 public ValidationPatternException(String message, Throwable cause) { 150 super(message, cause); 151 } 152 153 public ValidationPatternException(Throwable cause) { 154 super(cause); 155 } 156 } 157 }