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;
017
018import java.util.Collection;
019import java.util.HashMap;
020import java.util.Map;
021
022import org.apache.avalon.framework.component.Component;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026import org.apache.excalibur.source.SourceResolver;
027import org.apache.excalibur.source.TraversableSource;
028
029import org.ametys.runtime.i18n.I18nizableText;
030
031/**
032 * Helper for extraction module
033 */
034public class ExtractionHelper implements Component, Serviceable
035{
036    /** The avalon role */
037    public static final String ROLE = ExtractionHelper.class.getName();
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    /**
048     * Retrieve stylesheets in extraction folder
049     * @return the extraction stylesheets
050     * @throws Exception if an error occurs
051     */
052    public Map<Object, I18nizableText> getExtractionStylesheets() throws Exception
053    {
054        TraversableSource extractionDir = (TraversableSource) _srcResolver.resolveURI(ExtractionConstants.XSLT_DIR);
055        
056        Map<Object, I18nizableText> files = new HashMap<>();
057        
058        if (extractionDir.exists())
059        {
060            for (TraversableSource file : (Collection<TraversableSource>) extractionDir.getChildren())
061            {
062                String fileName = file.getName();
063                if (fileName.endsWith(".xsl") || fileName.endsWith(".xslt"))
064                {
065                    files.put(fileName, new I18nizableText(fileName));
066                }
067            }
068        }
069        return files;
070    }
071}