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.krad.uif.UifConstants;
020
021/**
022 * Pattern for matching any printable character
023 */
024public class AnyCharacterPatternConstraint extends ValidCharactersPatternConstraint {
025    protected boolean allowWhitespace = false;
026    protected boolean omitNewline = false;
027
028    /**
029     * @return allowWhitespace
030     */
031    public boolean getAllowWhitespace() {
032        return allowWhitespace;
033    }
034
035    /**
036     * @param allowWhitespace
037     */
038    public void setAllowWhitespace(boolean allowWhitespace) {
039        this.allowWhitespace = allowWhitespace;
040    }
041
042    /**
043     * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#getRegexString()
044     */
045    protected String getRegexString() {
046        StringBuffer regexString = new StringBuffer("[");
047
048        regexString.append("\\x21-\\x7E");
049        if (allowWhitespace) {
050            regexString.append("\\t\\v\\040");
051            if (!omitNewline) {
052                regexString.append("\\f\\r\\n");
053            }
054        }
055
056        regexString.append("]");
057
058        return regexString.toString();
059    }
060
061    /**
062     * @see org.kuali.rice.krad.datadictionary.validation.constraint.BaseConstraint#getLabelKey()
063     */
064    @Override
065    public String getLabelKey() {
066        String labelKey = super.getLabelKey();
067        if (StringUtils.isNotEmpty(labelKey)) {
068            return labelKey;
069        }
070        if (!allowWhitespace) {
071            return UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "noWhitespace";
072        } else {
073            return UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "anyCharacterPattern";
074        }
075    }
076
077    public boolean isOmitNewline() {
078        return omitNewline;
079    }
080
081    /**
082     * When set to true, omit new line characters from the set of valid characters.  This flag
083     * will only have an effect if the allowWhitespace flag is true, otherwise all whitespace
084     * including new lines characters are omitted.
085     * @param omitNewline
086     */
087    public void setOmitNewline(boolean omitNewline) {
088        this.omitNewline = omitNewline;
089    }
090}