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