001/*
002 *  Copyright 2017 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.web.site;
018
019import java.io.IOException;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.cocoon.ProcessingException;
025import org.apache.cocoon.generation.ServiceableGenerator;
026import org.apache.cocoon.xml.AttributesImpl;
027import org.apache.cocoon.xml.XMLUtils;
028import org.xml.sax.SAXException;
029
030/**
031 * Generates the site colors from the component
032 */
033public class SiteColorsGenerator extends ServiceableGenerator
034{
035    private SiteColorsComponent _siteColorsComponent;
036
037    @Override
038    public void service(ServiceManager smanager) throws ServiceException
039    {
040        super.service(smanager);
041        
042        _siteColorsComponent = (SiteColorsComponent) smanager.lookup(SiteColorsComponent.ROLE);
043    }
044
045    public void generate() throws IOException, SAXException, ProcessingException
046    {
047        contentHandler.startDocument();
048        
049        AttributesImpl attrs = new AttributesImpl();
050        attrs.addCDATAAttribute("default", _siteColorsComponent.getDefaultKey());
051        XMLUtils.startElement(contentHandler, "colors", attrs);
052
053        for (String id : _siteColorsComponent.getColors().keySet())
054        {
055            AttributesImpl attrs2 = new AttributesImpl();
056            attrs2.addCDATAAttribute("id", id);
057            XMLUtils.startElement(contentHandler, "color", attrs2);
058            
059            Map<String, String> map = _siteColorsComponent.getColors().get(id);
060            for (String key : map.keySet())
061            {
062                XMLUtils.createElement(contentHandler, key, map.get(key));
063            }
064            
065            XMLUtils.endElement(contentHandler, "color");
066        }
067        
068        XMLUtils.endElement(contentHandler, "colors");
069        contentHandler.endDocument();
070    }
071}