001 /* 002 * Copyright 2005-2008 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.kns.datadictionary.validation; 017 018 import java.io.Serializable; 019 import java.util.regex.Matcher; 020 import java.util.regex.Pattern; 021 022 import org.kuali.rice.core.exception.RiceRuntimeException; 023 import org.kuali.rice.kns.datadictionary.exporter.ExportMap; 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 abstract public class ValidationPattern implements Serializable { 091 // TODO: UNIT TEST: compile all patterns to test 092 093 /** 094 * @return regular expression Pattern generated by the individual ValidationPattern subclass 095 */ 096 abstract public Pattern getRegexPattern(); 097 098 /** 099 * @return String version of regular expression base, suitable for modification with length-specifiers and used internally by 100 * getRegexPattern 101 */ 102 abstract protected String getRegexString(); 103 104 105 /** 106 * @return true if the given String matches this pattern 107 */ 108 public boolean matches(String input) { 109 Pattern p = getRegexPattern(); 110 111 Matcher m = p.matcher(input); 112 113 return m.matches(); 114 } 115 116 /** 117 * @return ExportMap describing the subclass instance 118 */ 119 abstract public ExportMap buildExportMap(String exportKey); 120 121 abstract public String getValidationErrorMessageKey(); 122 123 public String[] getValidationErrorMessageParameters(String attributeLabel) { 124 return new String[] {attributeLabel}; 125 } 126 127 /** 128 * This method throws an exception if it is not configured properly 129 * 130 */ 131 public void completeValidation() throws ValidationPatternException { 132 } 133 134 /** exception thrown when a ValidationPattern is in an incorrect state. */ 135 public static class ValidationPatternException extends RiceRuntimeException { 136 137 private static final long serialVersionUID = 2012770642382150523L; 138 139 public ValidationPatternException(String message) { 140 super(message); 141 } 142 143 public ValidationPatternException() { 144 super(); 145 } 146 147 public ValidationPatternException(String message, Throwable cause) { 148 super(message, cause); 149 } 150 151 public ValidationPatternException(Throwable cause) { 152 super(cause); 153 } 154 } 155 }