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.kns.datadictionary.validation.charlevel;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.krad.datadictionary.exporter.ExportMap;
020import org.kuali.rice.krad.datadictionary.validation.CharacterLevelValidationPattern;
021
022import java.util.regex.Pattern;
023
024/**
025 * Pattern for matching any character in the given list (String)
026 * 
027 * @deprecated Use {@link org.kuali.rice.krad.datadictionary.validation.constraint.CharsetPatternConstraint}.
028 */
029@Deprecated
030public class CharsetValidationPattern extends CharacterLevelValidationPattern {
031    protected String validChars;
032
033    /**
034     * @return String containing all valid chars for this charset
035     */
036    public String getValidChars() {
037        return validChars;
038    }
039
040    /**
041     * @param validChars for this charset
042     */
043    public void setValidChars(String validChars) {
044        if (StringUtils.isEmpty(validChars)) {
045            throw new IllegalArgumentException("invalid (empty) validChars");
046        }
047
048        this.validChars = validChars;
049    }
050
051
052    /**
053     * Escapes every special character I could think of, to limit potential misuse of this pattern.
054     * 
055     * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#getRegexString()
056     */
057    protected String getRegexString() {
058        if (StringUtils.isEmpty(validChars)) {
059            throw new IllegalStateException("validChars is empty");
060        }
061
062        // filter out and escape chars which would confuse the pattern-matcher
063        Pattern filteringChars = Pattern.compile("([\\-\\[\\]\\{\\}\\$\\.\\^\\(\\)\\*\\&\\|])");
064        String filteredChars = filteringChars.matcher(validChars).replaceAll("\\\\$1");
065
066        StringBuffer regexString = new StringBuffer("[");
067        regexString.append(filteredChars);
068        if (filteredChars.endsWith("\\")) {
069            regexString.append("\\");
070        }
071        regexString.append("]");
072
073        return regexString.toString();
074    }
075
076
077    /**
078     * @see org.kuali.rice.krad.datadictionary.validation.CharacterLevelValidationPattern#extendExportMap(org.kuali.bo.datadictionary.exporter.ExportMap)
079     */
080    public void extendExportMap(ExportMap exportMap) {
081        exportMap.set("type", "charset");
082
083        exportMap.set("validChars", getValidChars());
084    }
085
086        /**
087         * This overridden method ...
088         * 
089         * @see org.kuali.rice.krad.datadictionary.validation.CharacterLevelValidationPattern#getValidationErrorMessageParameters(java.lang.String, java.lang.String)
090         */
091        @Override
092        public String[] getValidationErrorMessageParameters(String attributeLabel) {
093                // build character list
094                StringBuilder buf = new StringBuilder();
095                for (int i = 0; i < validChars.length(); i++) {
096                        buf.append(validChars.charAt(i));
097                        if (i != validChars.length() - 1) {
098                                buf.append(", ");
099                        }
100                }
101                String characterList = buf.toString();
102                
103                if (getMaxLength() != -1) {
104                        return new String[] {attributeLabel, String.valueOf(getMaxLength()), characterList};
105                }
106                if (getExactLength() != -1) {
107                        return new String[] {attributeLabel, String.valueOf(getExactLength()), characterList};
108                }
109                return new String[] {attributeLabel, "0", characterList};
110        }
111}