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 */
016
017package org.ametys.runtime.plugins.admin.plugins;
018
019import java.io.IOException;
020import java.io.InputStream;
021import java.util.Map;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.cocoon.ProcessingException;
025import org.apache.cocoon.environment.ObjectModelHelper;
026import org.apache.cocoon.generation.ServiceableGenerator;
027import org.apache.cocoon.xml.AttributesImpl;
028import org.apache.cocoon.xml.XMLUtils;
029import org.apache.excalibur.source.Source;
030import org.apache.excalibur.xml.sax.SAXParser;
031import org.xml.sax.InputSource;
032import org.xml.sax.SAXException;
033
034import org.ametys.core.util.IgnoreRootHandler;
035
036/**
037 * Generates the runtime file with the list of the modifications
038 */
039public class ChangeRuntime extends ServiceableGenerator
040{
041    @SuppressWarnings("unchecked")
042    @Override
043    public void generate() throws IOException, SAXException, ProcessingException
044    {
045        contentHandler.startDocument();
046        XMLUtils.startElement(contentHandler, "CMS");
047        
048        Source src = resolver.resolveURI("context://WEB-INF/param/runtime.xml");
049
050        SAXParser saxParser = null;
051        try (InputStream is = src.getInputStream())
052        {
053            saxParser = (SAXParser) manager.lookup(SAXParser.ROLE);
054            saxParser.parse(new InputSource(is), new IgnoreRootHandler(contentHandler));
055        }
056        catch (ServiceException e)
057        {
058            throw new ProcessingException("Unable to get a SAX parser", e);
059        }
060        finally
061        {
062            resolver.release(src);
063            manager.release(saxParser);
064        }
065        
066        Map parentContextParameters = (Map) objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
067        if (parentContextParameters != null)
068        {
069            XMLUtils.startElement(contentHandler, "transformations");
070            
071            Map<String, Boolean> extensionPoints = (Map<String, Boolean>) parentContextParameters.get("EP");
072            XMLUtils.startElement(contentHandler, "features");
073            
074            for (String ep : extensionPoints.keySet())
075            {
076                AttributesImpl attrs = new AttributesImpl();
077                attrs.addCDATAAttribute("name", ep);
078                XMLUtils.createElement(contentHandler, "feature", attrs, String.valueOf(extensionPoints.get(ep)));
079            }
080            
081            XMLUtils.endElement(contentHandler, "features");
082
083            Map<String, String> components = (Map<String, String>) parentContextParameters.get("CMP");
084            XMLUtils.startElement(contentHandler, "components");
085            
086            for (String cmp : components.keySet())
087            {
088                XMLUtils.createElement(contentHandler, cmp, String.valueOf(components.get(cmp)));
089            }
090            
091            XMLUtils.endElement(contentHandler, "components");
092            XMLUtils.endElement(contentHandler, "transformations");
093        }
094        
095        XMLUtils.endElement(contentHandler, "CMS");
096        contentHandler.endDocument();
097    }
098}