001/*
002 *  Copyright 2017 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.plugins.extraction.execution;
017
018import java.util.Collection;
019import java.util.HashMap;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025import org.apache.excalibur.source.SourceResolver;
026import org.apache.excalibur.source.TraversableSource;
027
028import org.ametys.plugins.extraction.ExtractionConstants;
029import org.ametys.runtime.i18n.I18nizableText;
030import org.ametys.runtime.parameter.Enumerator;
031
032/**
033 * Enumerator for extraction definition files
034 */
035public class ExtractionDefinitionFilesEnumerator implements Enumerator, Serviceable
036{
037    /** The source resolver */
038    private SourceResolver _srcResolver;
039    
040    public void service(ServiceManager serviceManager) throws ServiceException
041    {
042        _srcResolver = (SourceResolver) serviceManager.lookup(SourceResolver.ROLE);
043    }
044    
045    public Map<Object, I18nizableText> getEntries() throws Exception
046    {
047        TraversableSource extractionDir = (TraversableSource) _srcResolver.resolveURI(ExtractionConstants.DEFINITIONS_DIR);
048        
049        Map<Object, I18nizableText> files = new HashMap<>();
050        
051        if (extractionDir.exists())
052        {
053            for (TraversableSource file : (Collection<TraversableSource>) extractionDir.getChildren())
054            {
055                String fileName = file.getName();
056                if (fileName.endsWith(".xml"))
057                {
058                    files.put(fileName, new I18nizableText(fileName));
059                }
060            }
061        }
062        return files;
063    }
064    
065    public I18nizableText getEntry(String value) throws Exception
066    {
067        return new I18nizableText(value);
068    }
069}