001/*
002 *  Copyright 2014 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 */
016
017package org.ametys.odf.catalog.source;
018
019import java.io.IOException;
020import java.util.ArrayList;
021import java.util.List;
022
023import org.apache.avalon.framework.logger.AbstractLogEnabled;
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.Source;
028import org.apache.excalibur.source.SourceResolver;
029
030/**
031 * Default implementation of {@link ODFViewSelector}.
032 * First looks the wanted file in context://WEB-INF/stylesheets/path_to_file
033 * If it's not present, it will look in the catalogue plugin.
034 */
035public class DefaultODFView extends AbstractLogEnabled implements ODFViewSelector, Serviceable
036{
037    
038    /** The source resolver */
039    protected SourceResolver _resolver;
040    
041    @Override
042    public void service(ServiceManager manager) throws ServiceException
043    {
044        _resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
045    }
046    
047    @Override
048    public Source getSource(String pluginName, String filePath) throws IOException
049    {
050        for (String location : getLocations(pluginName, filePath))
051        {
052            try
053            {
054                Source source = _resolver.resolveURI(location);
055                if (!source.exists())
056                {
057                    if (getLogger().isDebugEnabled())
058                    {
059                        getLogger().debug("Failed to find a stylesheet at '" + location + "'.");
060                    }
061                }
062                else
063                {
064                    if (getLogger().isDebugEnabled())
065                    {
066                        getLogger().debug("Using source located at '" + location + "'.");
067                    }
068                    return source;
069                }
070            }
071            catch (IOException e)
072            {
073                if (getLogger().isDebugEnabled())
074                {
075                    getLogger().debug("Resolving protocol failed for resolving '" + location + "'.");
076                }
077            }
078        }
079        
080        // Should never occur because of the default stylesheet.
081        throw new IOException("Can't find a stylesheet for: " + filePath);
082    }
083    
084    /**
085     * Returns the ordered list of URI to be tested to find the corresponding view.
086     * @param pluginName the plugin name
087     * @param filePath the requested location.
088     * @return a list of possible URIs for the resource.
089     */
090    protected List<String> getLocations(String pluginName, String filePath)
091    {
092        ArrayList<String> locations = new ArrayList<>();
093        
094        // First search in the WEB-INF folder.
095        locations.add("context://WEB-INF/" + filePath);
096        
097        // Then search in the catalog plugin.
098        locations.add("plugin:" + pluginName + "://" + filePath);
099        
100        return locations;
101    }
102}