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.ade.postupload.client.ui;
029
030import org.opencms.gwt.client.ui.input.I_CmsFormField;
031import org.opencms.gwt.client.validation.I_CmsValidationController;
032import org.opencms.gwt.client.validation.I_CmsValidator;
033import org.opencms.gwt.shared.CmsValidationResult;
034import org.opencms.util.CmsUUID;
035import org.opencms.xml.content.CmsXmlContentProperty;
036
037/**
038 * Validates modified file names for uploaded files.
039 */
040public class CmsFilenameValidator implements I_CmsValidator {
041
042    /** The property definition for the file name. */
043    private CmsXmlContentProperty m_propDef;
044
045    /** The structure id of the uploaded file. */
046    private CmsUUID m_structureId;
047
048    /**
049     * Creates a new instance.
050     *
051     * @param structureId the structure id of the uploaded file
052     * @param propDef the property definition for the file name
053     */
054    public CmsFilenameValidator(CmsUUID structureId, CmsXmlContentProperty propDef) {
055
056        m_propDef = propDef;
057        m_structureId = structureId;
058
059    }
060
061    /**
062     * Matches a string against a regex, and inverts the match if the regex starts with a '!'.<p>
063     *
064     * @param regex the regular expression
065     * @param value the string to be matched
066     *
067     * @return true if the validation succeeded
068     */
069    private static boolean matchRuleRegex(String regex, String value) {
070
071        if (value == null) {
072            value = "";
073        }
074
075        if (regex == null) {
076            return true;
077        }
078        if ((regex.length() > 0) && (regex.charAt(0) == '!')) {
079            return !value.matches(regex.substring(1));
080        } else {
081            return value.matches(regex);
082        }
083    }
084
085    /**
086     * @see org.opencms.gwt.client.validation.I_CmsValidator#validate(org.opencms.gwt.client.ui.input.I_CmsFormField, org.opencms.gwt.client.validation.I_CmsValidationController)
087     */
088    @Override
089    public void validate(I_CmsFormField field, I_CmsValidationController controller) {
090
091        if (!matchRuleRegex(m_propDef.getRuleRegex(), field.getWidget().getFormValueAsString())) {
092            controller.provideValidationResult(field.getId(), new CmsValidationResult(m_propDef.getError()));
093        } else {
094            controller.validateAsync(
095                field.getId(),
096                field.getWidget().getFormValueAsString(),
097                "org.opencms.ade.postupload.CmsServerFilenameValidator",
098                "" + m_structureId);
099        }
100
101    }
102
103}