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.sitemap;
017
018import java.io.IOException;
019import java.util.List;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.ProcessingException;
024import org.apache.cocoon.environment.ObjectModelHelper;
025import org.apache.cocoon.environment.Request;
026import org.apache.cocoon.generation.ServiceableGenerator;
027import org.apache.cocoon.xml.AttributesImpl;
028import org.apache.cocoon.xml.XMLUtils;
029import org.apache.commons.lang.StringUtils;
030import org.xml.sax.SAXException;
031
032import org.ametys.web.repository.site.Site;
033import org.ametys.web.repository.site.SiteManager;
034
035/**
036 *  SAX sitemap decorators
037 *
038 */
039public class SitemapDecoratorsGenerator extends ServiceableGenerator
040{
041
042    private SitemapDecoratorsHandler _sitemapHandler;
043    private SiteManager _siteManager;
044    
045    @Override
046    public void service(ServiceManager smanager) throws ServiceException
047    {
048        super.service(smanager);
049        _sitemapHandler = (SitemapDecoratorsHandler) smanager.lookup(SitemapDecoratorsHandler.ROLE);
050        _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE);
051    }
052    
053    @Override
054    public void generate() throws IOException, SAXException, ProcessingException
055    {
056        Request request = ObjectModelHelper.getRequest(objectModel);
057        String siteName = (String) request.getAttribute("siteName");
058        Site site = _siteManager.getSite(siteName);
059        
060        contentHandler.startDocument();
061        
062        XMLUtils.startElement(contentHandler, "decorators");
063        
064        List<SitemapDecorator> decorators = _sitemapHandler.getDecorators(site);
065        for (SitemapDecorator decorator : decorators)
066        {
067            AttributesImpl attr = new AttributesImpl();
068            attr.addCDATAAttribute("id", decorator.getId());
069            if (StringUtils.isNotBlank(decorator.getIcon()))
070            {
071                attr.addCDATAAttribute("icon", decorator.getIcon());
072            }
073            if (StringUtils.isNotBlank(decorator.getIconGlyph()))
074            {
075                attr.addCDATAAttribute("iconGlyph", decorator.getIconGlyph());
076            }
077            XMLUtils.startElement(contentHandler, "decorator", attr);
078            decorator.getLabel().toSAX(contentHandler);
079            XMLUtils.endElement(contentHandler, "decorator");
080        }
081        XMLUtils.endElement(contentHandler, "decorators");
082        contentHandler.endDocument();
083
084    }
085
086}