001/*
002 *  Copyright 2020 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.odf.enumeration;
017
018import java.io.IOException;
019import java.io.InputStream;
020import java.util.HashMap;
021import java.util.Map;
022
023import org.apache.avalon.framework.component.Component;
024import org.apache.avalon.framework.configuration.Configurable;
025import org.apache.avalon.framework.configuration.Configuration;
026import org.apache.avalon.framework.configuration.ConfigurationException;
027import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
028import org.apache.avalon.framework.service.ServiceException;
029import org.apache.avalon.framework.service.ServiceManager;
030import org.apache.avalon.framework.service.Serviceable;
031import org.apache.excalibur.source.Source;
032import org.apache.excalibur.source.SourceResolver;
033import org.xml.sax.SAXException;
034
035import org.ametys.runtime.i18n.I18nizableText;
036import org.ametys.runtime.plugin.component.PluginAware;
037
038
039/**
040 * Component for certification labels
041 *
042 */
043public class CertificationLabels implements Serviceable, Configurable, Component, PluginAware
044{
045    /** The Avalon role */
046    public static final String ROLE = CertificationLabels.class.getName();
047    
048    private Map<String, I18nizableText> _entries = new HashMap<>();
049    
050    private String _pluginName;
051    
052    private SourceResolver _srcResolver;
053    
054    public void setPluginInfo(String pluginName, String featureName, String id)
055    {
056        _pluginName = pluginName;
057    }
058    
059    public void service(ServiceManager smanager) throws ServiceException
060    {
061        _srcResolver = (SourceResolver) smanager.lookup(SourceResolver.ROLE);
062    }
063    
064    public void configure(Configuration configuration) throws ConfigurationException
065    {
066        Configuration[] children = configuration.getChildren("entry");
067        
068        if (children.length == 0)
069        {
070            try
071            {
072                Source pluginSource = _srcResolver.resolveURI("plugin:" + _pluginName + "://labels.xml");
073                
074                try (InputStream is = pluginSource.getInputStream())
075                {
076                    Configuration cfg = new DefaultConfigurationBuilder().build(is);
077                    children = cfg.getChildren("entry");
078                }
079            }
080            catch (SAXException | IOException e)
081            {
082                throw new ConfigurationException("Unable to configure the labels enumerator", e);
083            }
084        }
085        
086        for (Configuration child : children)
087        {
088            String value = child.getChild("value").getValue();
089            I18nizableText label = I18nizableText.parseI18nizableText(child.getChild("label"), "plugin." + _pluginName);
090            _entries.put(value, label);
091        }
092    }
093        
094    /**
095     * Get the title of a label
096     * @param value the code of label
097     * @return the label's title
098     */
099    public I18nizableText getLabelTitle(String value)
100    {
101        return _entries.get(value);
102    }
103    
104    /**
105     * Get the labels
106     * @return the labels
107     */
108    public Map<String, I18nizableText> getLabels()
109    {
110        return _entries;
111    }
112}