001/*
002 *  Copyright 2012 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.cms.source;
017
018import java.io.IOException;
019import java.net.MalformedURLException;
020import java.util.Map;
021import java.util.regex.Matcher;
022import java.util.regex.Pattern;
023
024import org.apache.avalon.framework.context.Context;
025import org.apache.avalon.framework.context.ContextException;
026import org.apache.avalon.framework.context.Contextualizable;
027import org.apache.avalon.framework.logger.AbstractLogEnabled;
028import org.apache.avalon.framework.service.ServiceException;
029import org.apache.avalon.framework.service.ServiceManager;
030import org.apache.avalon.framework.service.Serviceable;
031import org.apache.cocoon.components.ContextHelper;
032import org.apache.cocoon.environment.Request;
033import org.apache.excalibur.source.Source;
034import org.apache.excalibur.source.SourceFactory;
035
036/**
037 * This source factory defines an overridable way to retrieve resources (mainly XSL files).
038 * Use: view://path_to_file
039 */
040public class ViewSourceFactory extends AbstractLogEnabled implements SourceFactory, Serviceable, Contextualizable
041{
042    private static final Pattern __SOURCE_PATTERN = Pattern.compile("^[\\w]+:([^:]+:)?//(.*)$");
043    
044    /** The view selector. */
045    protected ViewSelector _viewSelector;
046    /** The context */
047    protected Context _context;
048    
049    @Override
050    public void service(ServiceManager manager) throws ServiceException
051    {
052        _viewSelector = (ViewSelector) manager.lookup(ViewSelector.ROLE);
053    }
054    
055    @Override
056    public void contextualize(Context context) throws ContextException
057    {
058        _context = context;
059    }
060    
061    @Override
062    public Source getSource(String location, Map parameters) throws IOException, MalformedURLException
063    {
064        Matcher m = __SOURCE_PATTERN.matcher(location);
065        if (!m.matches())
066        {
067            throw new MalformedURLException("URI must be like protocol://path/to/resource. Location was '" + location + "'");
068        }
069        
070        Request request = ContextHelper.getRequest(_context);
071        String pluginName = m.group(1);
072        if (pluginName == null)
073        {
074            pluginName = (String) request.getAttribute("pluginName");
075        }
076        else
077        {
078            pluginName = pluginName.substring(0, pluginName.length() - 1);
079        }
080        
081        String fileLocation = m.group(2);
082        
083        return _viewSelector.getSource(pluginName, fileLocation);
084    }
085    
086    @Override
087    public void release(Source source)
088    {
089        // Nothing to do.
090    }
091
092}