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.skinfactory.generators;
017
018import java.io.IOException;
019import java.io.InputStream;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.cocoon.ProcessingException;
024import org.apache.cocoon.environment.ObjectModelHelper;
025import org.apache.cocoon.generation.ServiceableGenerator;
026import org.apache.cocoon.xml.AttributesImpl;
027import org.apache.cocoon.xml.XMLUtils;
028import org.apache.excalibur.source.Source;
029import org.apache.excalibur.xml.sax.SAXParser;
030import org.xml.sax.InputSource;
031import org.xml.sax.SAXException;
032
033import org.ametys.core.util.IgnoreRootHandler;
034import org.ametys.skinfactory.parameters.ImageParameter.FileValue;
035
036/**
037 * Generates the model file with the modifications
038 */
039public class ChangeParameters extends ServiceableGenerator
040{
041    @Override
042    public void generate() throws IOException, SAXException, ProcessingException
043    {
044        contentHandler.startDocument();
045        XMLUtils.startElement(contentHandler, "CMS");
046        
047        Map parentContextParameters = (Map) objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
048        if (parentContextParameters != null)
049        {
050            Source src = resolver.resolveURI((String) parentContextParameters.get("modelUri"));
051            SAXParser saxParser = null;
052            try (InputStream is = src.getInputStream())
053            {
054                saxParser = (SAXParser) manager.lookup(SAXParser.ROLE);
055                saxParser.parse(new InputSource(is), new IgnoreRootHandler(contentHandler));
056            }
057            catch (ServiceException e)
058            {
059                throw new ProcessingException("Unable to get a SAX parser.", e);
060            }
061            finally
062            {
063                resolver.release(src);
064                manager.release(saxParser);
065            }
066            
067            XMLUtils.startElement(contentHandler, "settings");
068            
069            @SuppressWarnings("unchecked")
070            Map<String, Object> params = (Map<String, Object>) parentContextParameters.get("skinParameters");
071            
072            for (String paramId : params.keySet())
073            {
074                AttributesImpl attrs = new AttributesImpl();
075                attrs.addCDATAAttribute("id", paramId);
076                
077                Object value = params.get(paramId);
078                if (value instanceof String)
079                {
080                    XMLUtils.createElement(contentHandler, "param", attrs, (String) value);
081                }
082                else if (value instanceof FileValue)
083                {
084                    attrs.addCDATAAttribute("uploaded", String.valueOf(((FileValue) value).isUploaded()));
085                    XMLUtils.createElement(contentHandler, "param", attrs, ((FileValue) value).getPath());
086                }
087                else
088                {
089                    @SuppressWarnings("unchecked")
090                    Map<String, String> mapValues = (Map<String, String>) value;
091                    XMLUtils.startElement(contentHandler, "param", attrs);
092                    
093                    for (String lang : mapValues.keySet())
094                    {
095                        XMLUtils.createElement(contentHandler, lang, mapValues.get(lang));
096                    }
097                    XMLUtils.endElement(contentHandler, "param");
098                }
099                
100            }
101            XMLUtils.endElement(contentHandler, "settings");
102        }
103        
104        XMLUtils.endElement(contentHandler, "CMS");
105        contentHandler.endDocument();
106        
107    }
108    
109}