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.core.ui;
017
018import java.util.LinkedHashMap;
019import java.util.List;
020import java.util.Map;
021import java.util.regex.Pattern;
022
023import org.apache.avalon.framework.configuration.Configurable;
024import org.apache.avalon.framework.configuration.Configuration;
025import org.apache.avalon.framework.configuration.ConfigurationException;
026
027import org.ametys.plugins.core.ui.util.ConfigurationHelper;
028import org.ametys.runtime.plugin.component.AbstractLogEnabled;
029import org.ametys.runtime.plugin.component.PluginAware;
030
031/**
032 * Static implementation for the ribbon import manager.
033 * The expected configuration is one or more "workspace" with a match attribute (default to ".*"), and a list of files to include : 
034 *    <workspace match=".*">
035 *      <file plugin="core-ui">ribbon/ribbon.xml</file>
036 *      <file>ribbon/ribbon.xml</file>
037 *    </workspace> 
038 */
039public class StaticRibbonImportManager extends AbstractLogEnabled implements RibbonImport, Configurable, PluginAware
040{
041    private Map<List<String>, Pattern> _filesList;
042    private String _pluginName;
043    
044    @Override
045    public void setPluginInfo(String pluginName, String featureName, String id)
046    {
047        _pluginName = pluginName;
048    }
049    
050    public void configure(Configuration configuration) throws ConfigurationException
051    {
052        _filesList = new LinkedHashMap<>();
053        
054        for (Configuration workspaceConfig : configuration.getChildren("workspace"))
055        {
056            String pattern = workspaceConfig.getAttribute("match", ".*");
057            
058            _filesList.put(ConfigurationHelper.parsePluginResourceUri(workspaceConfig, _pluginName, getLogger()), Pattern.compile(pattern));
059        }
060    }
061
062    @Override
063    public Map<List<String>, Pattern> getImports()
064    {
065        return _filesList;
066    }
067}