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