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.File;
019import java.io.IOException;
020import java.net.MalformedURLException;
021import java.util.Collection;
022import java.util.HashMap;
023import java.util.Map;
024
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.avalon.framework.service.Serviceable;
028import org.apache.commons.lang3.StringUtils;
029import org.apache.excalibur.source.SourceException;
030import org.apache.excalibur.source.SourceResolver;
031import org.apache.excalibur.source.TraversableSource;
032
033import org.ametys.plugins.extraction.ExtractionConstants;
034import org.ametys.runtime.i18n.I18nizableText;
035import org.ametys.runtime.model.Enumerator;
036
037/**
038 * Enumerator for extraction definition files
039 */
040public class ExtractionDefinitionFilesEnumerator implements Enumerator<String>, Serviceable
041{
042    /** The source resolver */
043    private SourceResolver _srcResolver;
044    
045    public void service(ServiceManager serviceManager) throws ServiceException
046    {
047        _srcResolver = (SourceResolver) serviceManager.lookup(SourceResolver.ROLE);
048    }
049    
050    public Map<String, I18nizableText> getTypedEntries() throws MalformedURLException, IOException
051    {
052        TraversableSource extractionDir = (TraversableSource) _srcResolver.resolveURI(ExtractionConstants.DEFINITIONS_DIR);
053        
054        if (extractionDir.exists())
055        {
056            return _getDefinitionFiles(extractionDir, StringUtils.EMPTY);
057        }
058        else
059        {
060            return Map.of();
061        }
062    }
063    
064    private Map<String, I18nizableText> _getDefinitionFiles(TraversableSource file, String prefix) throws SourceException
065    {
066        Map<String, I18nizableText> files = new HashMap<>();
067        
068        for (TraversableSource child : (Collection<TraversableSource>) file.getChildren())
069        {
070            String childName = child.getName();
071
072            if (child.isCollection())
073            {
074                files.putAll(_getDefinitionFiles(child, childName + File.pathSeparator));
075            }
076            else if (childName.endsWith(".xml"))
077            {
078                String filePath = prefix + childName;
079                files.put(filePath, new I18nizableText(filePath));
080            }
081        }
082        
083        return files;
084    }
085
086    public I18nizableText getEntry(String value) throws Exception
087    {
088        return new I18nizableText(value);
089    }
090}