001/**
002 * Copyright 2005-2018 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.krad.datadictionary.parse.BeanTag;
020import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
021import org.kuali.rice.krad.datadictionary.validator.ValidationTrace;
022import org.kuali.rice.krad.uif.UifConstants;
023
024import java.util.ArrayList;
025import java.util.List;
026import java.util.regex.Pattern;
027
028/**
029 * Pattern for matching any character in the given list (String)
030 *
031 * @author Kuali Rice Team (rice.collab@kuali.org)
032 */
033@BeanTag(name = "charsetPatternConstraint", parent = "CharsetPatternConstraint")
034public class CharsetPatternConstraint extends ValidCharactersPatternConstraint {
035    protected String validChars;
036
037    /**
038     * @return String containing all valid chars for this charset
039     */
040    @BeanTagAttribute(name = "validChars")
041    public String getValidChars() {
042        return validChars;
043    }
044
045    /**
046     * @param validChars for this charset
047     */
048    public void setValidChars(String validChars) {
049        if (StringUtils.isEmpty(validChars)) {
050            throw new IllegalArgumentException("invalid (empty) validChars");
051        }
052
053        this.validChars = validChars;
054    }
055
056    /**
057     * Escapes every special character I could think of, to limit potential misuse of this pattern.
058     *
059     * {@inheritDoc}
060     */
061    @Override
062    protected String getRegexString() {
063        if (StringUtils.isEmpty(validChars)) {
064            throw new IllegalStateException("validChars is empty");
065        }
066
067        // filter out and escape chars which would confuse the pattern-matcher
068        Pattern filteringChars = Pattern.compile("([\\-\\[\\]\\{\\}\\$\\.\\^\\(\\)\\*\\&\\|])");
069        String filteredChars = filteringChars.matcher(validChars).replaceAll("\\\\$1");
070
071        StringBuffer regexString = new StringBuffer("[");
072        regexString.append(filteredChars);
073        if (filteredChars.endsWith("\\")) {
074            regexString.append("\\");
075        }
076        regexString.append("]");
077
078        return regexString.toString();
079    }
080
081    /**
082     * @see org.kuali.rice.krad.datadictionary.validation.constraint.BaseConstraint#getMessageKey()
083     */
084    @Override
085    public String getMessageKey() {
086        String messageKey = super.getMessageKey();
087        if (StringUtils.isNotEmpty(messageKey)) {
088            return messageKey;
089        }
090
091        return (UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "charsetPattern");
092    }
093
094    /**
095     * Parameters to be used in the string retrieved by this constraint's messageKey
096     *
097     * @return the validationMessageParams
098     */
099    public List<String> getValidationMessageParams() {
100        if (validationMessageParams == null) {
101            validationMessageParams = new ArrayList<String>();
102            if (StringUtils.isNotBlank(validChars)) {
103                validationMessageParams.add(validChars);
104            }
105
106        }
107        return this.validationMessageParams;
108    }
109
110    /**
111     * Validates different requirements of component compiling a series of reports detailing information on errors
112     * found in the component.  Used by the RiceDictionaryValidator.
113     *
114     * @param tracer Record of component's location
115     */
116    @Override
117    public void completeValidation(ValidationTrace tracer) {
118        tracer.addBean("CharsetPatternConstraint", getMessageKey());
119
120        if (getValidChars() == null) {
121            String currentValues[] = {"validChars =" + getValidChars()};
122            tracer.createError("ValidChars must be set", currentValues);
123        }
124
125        super.completeValidation(tracer.getCopy());
126    }
127}