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.runtime.cocoon;
017
018import java.util.Collection;
019
020import org.apache.avalon.framework.component.ComponentManager;
021import org.apache.avalon.framework.configuration.Configuration;
022import org.apache.avalon.framework.configuration.ConfigurationException;
023import org.apache.avalon.framework.configuration.DefaultConfiguration;
024import org.apache.avalon.framework.configuration.MutableConfiguration;
025
026/**
027 * Entry point in the sitemap build process to dynamically insert the components brought by the SitemapConfigurationExtensionPoint
028 */
029public class SitemapLanguage extends org.apache.cocoon.components.treeprocessor.sitemap.SitemapLanguage
030{
031    @Override
032    protected ComponentManager createComponentManager(Configuration tree) throws Exception
033    {
034        if (processor.getWrappingProcessor() != processor.getRootProcessor())
035        {
036            // Not in the root sitemap, does nothing
037            return super.createComponentManager(tree);
038        }
039        
040        SitemapConfigurationExtensionPoint sitemapConfigurations = (SitemapConfigurationExtensionPoint) parentManager.lookup(SitemapConfigurationExtensionPoint.ROLE);
041        
042        DefaultConfiguration config = new DefaultConfiguration("sitemap");
043        config.addAll(tree);
044        
045        MutableConfiguration componentsConfig = config.getMutableChild("components");
046        
047        _addRuntimeComponents(componentsConfig, "actions", sitemapConfigurations);
048        _addRuntimeComponents(componentsConfig, "generators", sitemapConfigurations);
049        _addRuntimeComponents(componentsConfig, "transformers", sitemapConfigurations);
050        _addRuntimeComponents(componentsConfig, "serializers", sitemapConfigurations);
051        _addRuntimeComponents(componentsConfig, "readers", sitemapConfigurations);
052        _addRuntimeComponents(componentsConfig, "matchers", sitemapConfigurations);
053        _addRuntimeComponents(componentsConfig, "selectors", sitemapConfigurations);
054        _addRuntimeComponents(componentsConfig, "pipes", sitemapConfigurations);
055        
056        return super.createComponentManager(config);
057    }
058    
059    private void _addRuntimeComponents(MutableConfiguration componentsConfig, String componentName, SitemapConfigurationExtensionPoint sitemapConfigurations) throws ConfigurationException
060    {
061        Collection<Configuration> sitemapConfigs = sitemapConfigurations.getConfigurations(componentName);
062        
063        if (sitemapConfigs == null)
064        {
065            return;
066        }
067        
068        MutableConfiguration config = componentsConfig.getMutableChild(componentName);
069        
070        for (Configuration sitemapConfig : sitemapConfigs)
071        {
072            config.addChild(sitemapConfig);
073        }
074    }
075}