001/*
002 *  Copyright 2012 Anyware Services
003 *
004 *  Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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 */
016
017package org.ametys.plugins.core.parameter;
018
019import java.util.Map;
020import java.util.regex.Pattern;
021
022import org.apache.avalon.framework.configuration.Configuration;
023import org.apache.avalon.framework.configuration.ConfigurationException;
024
025import org.ametys.runtime.i18n.I18nizableText;
026import org.ametys.runtime.parameter.DefaultValidator;
027import org.ametys.runtime.parameter.Errors;
028
029/**
030 * Implements the same configuration as the DefaultValidator and also handle a <maxlength> parameter that allows a max count of chars
031 */
032public class TextValidator extends DefaultValidator
033{
034    /** Does the value has a max length */
035    protected Integer _maxLength;
036    
037    @Override
038    public void configure(Configuration configuration) throws ConfigurationException
039    {
040        Configuration validatorConfig = configuration.getChild("validation").getChild("custom-validator");
041        
042        _isMandatory = validatorConfig.getChild("mandatory", false) != null;
043
044        String regexp = validatorConfig.getChild("regexp").getValue(null);
045        if (regexp != null)
046        {
047            _regexp = Pattern.compile(regexp);
048        }
049
050        int maxLength = validatorConfig.getChild("maxlength").getValueAsInteger(0);
051        if (maxLength > 0)
052        {
053            _maxLength = maxLength;
054        }
055    }
056    
057    @Override
058    public Map<String, Object> getConfiguration()
059    {
060        Map<String, Object> configuration = super.getConfiguration();
061        
062        if (_maxLength != null)
063        {
064            configuration.put("maxlength", _maxLength);
065        }
066        
067        return configuration;
068    }
069    
070    @Override
071    protected void validateSingleValue (Object value, Errors errors)
072    {
073        super.validateSingleValue(value, errors);
074        
075        if (_maxLength != null && value != null && getText(value).length() > _maxLength)
076        {
077            if (getLogger().isDebugEnabled())
078            {
079                getLogger().debug("The validator refused a value for a parameter that should be smaller (max is " + _maxLength + " and length is " + value.toString().length() + ")");
080            }
081
082            errors.addError(new I18nizableText("plugin.core-ui", "PLUGINS_CORE_UI_VALIDATOR_TEXT_MAXLENGTH"));
083        }
084    }
085    
086    @Override
087    protected void validateArrayValues (Object[] values, Errors errors)
088    {
089        super.validateArrayValues(values, errors);
090        
091        if (_regexp != null && values != null)
092        {
093            for (Object value : values)
094            {
095                if (getText(value).length() > _maxLength)
096                {
097                    if (getLogger().isDebugEnabled())
098                    {
099                        getLogger().debug("The validator refused a value for a parameter that should be smaller (max is " + _maxLength + " and length is " + value.toString().length() + ")");
100                    }
101
102                    errors.addError(new I18nizableText("plugin.core-ui", "PLUGINS_CORE_UI_VALIDATOR_TEXT_MAXLENGTH"));
103                }
104            }
105        }
106    }
107    
108    /**
109     * Get the text
110     * @param value The value to convert to text
111     * @return the value converted
112     */
113    protected String getText(Object value)
114    {
115        return value != null ? value.toString() : "";
116    }
117}