001/*
002 *  Copyright 2016 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 */
016package org.ametys.runtime.config;
017
018import org.apache.avalon.framework.configuration.Configuration;
019import org.apache.avalon.framework.configuration.ConfigurationException;
020
021import org.ametys.runtime.i18n.I18nizableText;
022import org.ametys.runtime.model.checker.ItemChecker;
023import org.ametys.runtime.model.checker.ItemCheckerParser;
024import org.ametys.runtime.plugin.component.ThreadSafeComponentManager;
025
026/**
027 * Parameter checker parser for configuration parameters
028 */
029public class ConfigParameterCheckerParser extends ItemCheckerParser
030{
031    /**
032     * Instantiate a configuration parameter checker parser
033     * @param paramCheckerManager the parameter checker.
034     */
035    public ConfigParameterCheckerParser(ThreadSafeComponentManager<ItemChecker> paramCheckerManager)
036    {
037        super(paramCheckerManager);
038    }
039    
040    /**
041     * Parse a configuration parameter checker from an XML configuration.
042     * @param pluginName the plugin's name declaring this parameter.
043     * @param paramCheckerConfig the XML configuration.
044     * @return the {@link ConfigParameterCheckerDescriptor} for the parsed configuration parameter checker
045     * @throws ConfigurationException if the configuration is not valid.
046     */
047    @Override
048    public ConfigParameterCheckerDescriptor parseParameterChecker(String pluginName, Configuration paramCheckerConfig) throws ConfigurationException
049    {
050        ConfigParameterCheckerDescriptor configParamChecker = (ConfigParameterCheckerDescriptor) super.parseParameterChecker(pluginName, paramCheckerConfig);
051        
052        Configuration uiRefConfig = paramCheckerConfig.getChild("ui-ref");
053        
054        String uiRefParamId = null;
055        Configuration uiRefParamConfig = uiRefConfig.getChild("param-ref", false);
056        if (uiRefParamConfig != null)
057        {
058            uiRefParamId = uiRefParamConfig.getAttribute("id");
059        }
060        
061        I18nizableText uiRefGroup = null;
062        Configuration uiRefGroupConfig = uiRefConfig.getChild("group", false);
063        if (uiRefGroupConfig != null)
064        {
065            uiRefGroup = _parseI18nizableText(uiRefConfig, pluginName, "group");
066        }
067        
068        I18nizableText  uiRefCategory = null;
069        Configuration uiRefCategoryConfig = uiRefConfig.getChild("category", false);
070        if (uiRefCategoryConfig != null)
071        {
072            uiRefCategory = _parseI18nizableText(uiRefConfig, pluginName, "category");
073        }
074        
075        configParamChecker.setUiRefParamPath(uiRefParamId);
076        configParamChecker.setUiRefGroup(uiRefGroup);
077        configParamChecker.setUiRefCategory(uiRefCategory);
078        
079        return configParamChecker;
080    }
081    
082    @Override
083    protected ConfigParameterCheckerDescriptor _getParameterCheckerDescriptorInstance()
084    {
085        return new ConfigParameterCheckerDescriptor();
086    }
087}