001/**
002 * Copyright 2005-2014 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 */
016package org.kuali.rice.krad.datadictionary.validation;
017
018import org.kuali.rice.core.api.exception.RiceRuntimeException;
019import org.kuali.rice.krad.datadictionary.exporter.ExportMap;
020
021import java.io.Serializable;
022import java.util.regex.Matcher;
023import 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
091abstract 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     * 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}