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.util.HashMap;
020import java.util.Map;
021
022import org.apache.avalon.framework.parameters.Parameters;
023import org.apache.cocoon.acting.ServiceableAction;
024import org.apache.cocoon.environment.Redirector;
025import org.apache.cocoon.environment.SourceResolver;
026import org.apache.commons.lang3.StringUtils;
027import org.apache.excalibur.source.Source;
028
029import org.ametys.plugins.extraction.ExtractionConstants;
030
031/**
032 * Get for extraction parameters from query
033 */
034public class GetExtractionParametersAction extends ServiceableAction
035{
036    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
037    {
038        Map<String, String> result = new HashMap<>();
039        
040        String xslt = parameters.getParameter("xslt", "");
041        String xsltFile = StringUtils.defaultString(getXsltLocation(resolver, xslt));
042        result.put("xsltFile", xsltFile);
043        
044        return result;
045    }
046    
047    /**
048     * Get the XSLT location.
049     * @param resolver the source resolver.
050     * @param xslt the requested xslt.
051     * @return the XSLT file location.
052     * @throws IOException if an error occurs resolving the XSLT.
053     */
054    protected String getXsltLocation(SourceResolver resolver, String xslt) throws IOException
055    {
056        Source xsltSource = null;
057        try
058        {
059            if (StringUtils.isNotBlank(xslt))
060            {
061                String location = ExtractionConstants.XSLT_DIR + "/" + xslt;
062                xsltSource = resolver.resolveURI(location);
063                
064                if (xsltSource.exists())
065                {
066                    return location;
067                }
068            }
069        }
070        finally
071        {
072            if (xsltSource != null)
073            {
074                resolver.release(xsltSource);
075            }
076        }
077        
078        return null;
079    }
080
081}