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.krad.datadictionary.validation.constraint;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.config.property.ConfigurationService;
020import org.kuali.rice.krad.service.KRADServiceLocator;
021import org.kuali.rice.krad.uif.UifConstants;
022
023import java.util.ArrayList;
024import java.util.List;
025import java.util.regex.Pattern;
026
027/**
028 * Pattern for matching any character in the given list (String)
029 * 
030 * 
031 */
032public class CharsetPatternConstraint extends ValidCharactersPatternConstraint {
033    protected String validChars;
034
035    /**
036     * @return String containing all valid chars for this charset
037     */
038    public String getValidChars() {
039        return validChars;
040    }
041
042    /**
043     * @param validChars for this charset
044     */
045    public void setValidChars(String validChars) {
046        if (StringUtils.isEmpty(validChars)) {
047            throw new IllegalArgumentException("invalid (empty) validChars");
048        }
049
050        this.validChars = validChars;
051    }
052
053
054    /**
055     * Escapes every special character I could think of, to limit potential misuse of this pattern.
056     * 
057     * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#getRegexString()
058     */
059    protected String getRegexString() {
060        if (StringUtils.isEmpty(validChars)) {
061            throw new IllegalStateException("validChars is empty");
062        }
063
064        // filter out and escape chars which would confuse the pattern-matcher
065        Pattern filteringChars = Pattern.compile("([\\-\\[\\]\\{\\}\\$\\.\\^\\(\\)\\*\\&\\|])");
066        String filteredChars = filteringChars.matcher(validChars).replaceAll("\\\\$1");
067
068        StringBuffer regexString = new StringBuffer("[");
069        regexString.append(filteredChars);
070        if (filteredChars.endsWith("\\")) {
071            regexString.append("\\");
072        }
073        regexString.append("]");
074
075        return regexString.toString();
076    }
077
078        /**
079         * 
080         * @see org.kuali.rice.krad.datadictionary.validation.constraint.BaseConstraint#getLabelKey()
081         */
082        @Override
083        public String getLabelKey() {
084                String labelKey = super.getLabelKey();
085                if (StringUtils.isNotEmpty(labelKey)) {
086                        return labelKey;
087                }
088                return (UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "charsetPattern");
089        }
090
091    /**
092     * Parameters to be used in the string retrieved by this constraint's labelKey
093     * @return the validationMessageParams
094     */
095    public List<String> getValidationMessageParams() {
096        if(validationMessageParams == null){
097            validationMessageParams = new ArrayList<String>();
098            if (StringUtils.isNotBlank(validChars)) {
099                validationMessageParams.add(validChars);
100            }
101            
102        }
103        return this.validationMessageParams;
104    }
105
106}