001/*
002 *  Copyright 2011 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.web.skin;
017
018import java.io.File;
019import java.io.IOException;
020import java.util.Map;
021
022import org.apache.avalon.framework.configuration.Configuration;
023import org.apache.avalon.framework.configuration.ConfigurationException;
024import org.apache.avalon.framework.configuration.DefaultConfiguration;
025import org.apache.avalon.framework.parameters.Parameters;
026import org.apache.cocoon.ProcessingException;
027import org.apache.cocoon.environment.SourceResolver;
028import org.apache.commons.lang.StringUtils;
029import org.xml.sax.SAXException;
030
031import org.ametys.core.cocoon.I18nTransformer;
032
033/**
034 * I18nTransformer that is configured at runtime to retrieve message catalogues from a specific folder.
035 */
036public class ExternalI18nTransformer extends I18nTransformer
037{
038    
039    private Configuration _baseConf;
040    
041    @Override
042    public void configure(Configuration conf) throws ConfigurationException
043    {
044        _baseConf = conf;
045    }
046    
047    @Override
048    public void setup(SourceResolver resolver, Map objModel, String source, Parameters parameters) throws ProcessingException, SAXException, IOException
049    {
050        String directoryPath = parameters.getParameter("directory", "");
051        String catalogueId = parameters.getParameter("catalogue-id", "");
052        
053        if (StringUtils.isNotEmpty(directoryPath))
054        {
055            File directory = new File(directoryPath);
056            
057            try
058            {
059                _configure(directory, catalogueId);
060            }
061            catch (ConfigurationException e)
062            {
063                getLogger().error("Error with i18n on work dir", e);
064                throw new SAXException("Error with i18n on work dir", e);
065            }
066        }
067        
068        super.setup(resolver, objModel, source, parameters);
069    }
070    
071    private void _configure(File i18nDir, String catalogueId) throws ConfigurationException
072    {
073        // Modification de la configuration pour faire apparaitre les sites
074        DefaultConfiguration newConf = new DefaultConfiguration("i18n");
075        
076        Configuration cataloguesConf = _baseConf.getChild("catalogues", false);
077        
078//        if (StringUtils.isEmpty(cataloguesConf.getAttribute("default", "")))
079//        {
080//            cataloguesConf.set
081//        }
082        
083        DefaultConfiguration catalogues = new DefaultConfiguration("catalogues");
084        catalogues.setAttribute("default", catalogueId);
085        newConf.addChild(catalogues);
086        
087        if (cataloguesConf != null)
088        {
089            catalogues.addAll(cataloguesConf);
090        }
091        
092        _configureDirectory(catalogues, catalogueId, i18nDir);
093        
094        super.configure(newConf);
095    }
096    
097    private void _configureDirectory(DefaultConfiguration catalogues, String catalogueId, File i18nDir)
098    {
099        // On filtre les non-repertoires, non-plugins, et les plugins sans i18n
100        if (i18nDir.isDirectory() && new File(i18nDir, "messages.xml").exists())
101        {
102            DefaultConfiguration catalogue = new DefaultConfiguration("catalogue");
103            catalogue.setAttribute("id", catalogueId);
104            catalogue.setAttribute("name", "messages");
105            catalogue.setAttribute("location", "file://" + i18nDir.getAbsolutePath());
106            
107            catalogues.addChild(catalogue);
108        }
109    }
110
111}