001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.gwt.client.ui.input;
029
030import org.opencms.gwt.client.validation.I_CmsValidationController;
031import org.opencms.gwt.client.validation.I_CmsValidator;
032import org.opencms.gwt.shared.CmsValidationResult;
033import org.opencms.util.CmsStringUtil;
034
035/**
036 * Basic regular expression validator for widgets of field type string.<p>
037 *
038 * @since 8.0.0
039 */
040public class CmsRegexValidator implements I_CmsValidator {
041
042    /** If true, the validator always allows empty input. */
043    private boolean m_alwaysAllowEmpty;
044
045    /** The message to be displayed when the validation fails. */
046    private String m_message;
047
048    /** The regex used for validation. */
049    private String m_regex;
050
051    /**
052     * Creates a new regex-based validator.
053     *
054     * The regular expression passed as a parameter is used to match complete strings, not parts of strings.
055     * For example, a regex of "AAA" will only match the string "AAA", and not "BAAA".
056     * If the regex starts with an exclamation mark ("!"), the match will be inverted, i.e. only strings that don't
057     * match the rest of the regular expression will be interpreted as valid.
058     *
059     * @param regex a regular expression
060     * @param message an error message
061     * @param alwaysAllowEmpty if true, the validation will always allow the empty string
062     */
063    public CmsRegexValidator(String regex, String message, boolean alwaysAllowEmpty) {
064
065        assert message != null;
066        m_regex = regex;
067        m_message = message;
068        m_alwaysAllowEmpty = alwaysAllowEmpty;
069    }
070
071    /**
072     * Matches a string against a regex, and inverts the match if the regex starts with a '!'.<p>
073     *
074     * @param regex the regular expression
075     * @param value the string to be matched
076     *
077     * @return true if the validation succeeded
078     */
079    private static boolean matchRuleRegex(String regex, String value) {
080
081        if (value == null) {
082            value = "";
083        }
084
085        if (regex == null) {
086            return true;
087        }
088        if ((regex.length() > 0) && (regex.charAt(0) == '!')) {
089            return !value.matches(regex.substring(1));
090        } else {
091            return value.matches(regex);
092        }
093    }
094
095    /**
096     * @see org.opencms.gwt.client.validation.I_CmsValidator#validate(org.opencms.gwt.client.ui.input.I_CmsFormField, org.opencms.gwt.client.validation.I_CmsValidationController)
097     */
098    public void validate(I_CmsFormField field, I_CmsValidationController controller) {
099
100        I_CmsFormWidget widget = field.getWidget();
101        if (widget.getFieldType() == I_CmsFormWidget.FieldType.STRING) {
102            String value = widget.getFormValueAsString();
103            CmsValidationResult result;
104            if (CmsStringUtil.isEmpty(value) && m_alwaysAllowEmpty) {
105                result = CmsValidationResult.VALIDATION_OK;
106            } else if (!matchRuleRegex(m_regex, value)) {
107                result = new CmsValidationResult(m_message);
108            } else {
109                result = CmsValidationResult.VALIDATION_OK;
110            }
111            controller.provideValidationResult(field.getId(), result);
112        }
113    }
114}