001/*
002 *  Copyright 2010 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.cocoon;
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.plugins.repository.AmetysObject;
033import org.ametys.web.repository.site.Site;
034import org.ametys.web.repository.site.SiteManager;
035import org.ametys.web.repository.sitemap.Sitemap;
036import org.ametys.web.sitemap.SitemapDecorator;
037import org.ametys.web.sitemap.SitemapDecoratorsHandler;
038
039/**
040 * SAX information on site or sitemap node. Use #siteName and/or #lang parameters.
041 * 
042 *
043 */
044public class SiteSitemapInformationGenerator extends ServiceableGenerator
045{
046    private SiteManager _siteManager;
047    private SitemapDecoratorsHandler _decoratorsManager;
048    
049    @Override
050    public void service(ServiceManager smanager) throws ServiceException
051    {
052        super.service(smanager);
053        _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE);
054        _decoratorsManager = (SitemapDecoratorsHandler) smanager.lookup(SitemapDecoratorsHandler.ROLE);
055    }
056    
057    @Override
058    public void generate() throws IOException, SAXException, ProcessingException
059    {
060        Request request = ObjectModelHelper.getRequest(objectModel);
061        String siteName = request.getParameter("siteName");
062        String lang = request.getParameter("lang");
063        
064        contentHandler.startDocument();
065        if (StringUtils.isEmpty(siteName))
066        {
067            // Get the root sites node
068            AmetysObject rootSites = _siteManager.getRoot();
069            _saxRootSitesInformation(rootSites);
070        }
071        else if (StringUtils.isEmpty(lang))
072        {
073            Site site = _siteManager.getSite(siteName);
074            _saxSiteInformation(site);
075        }
076        else
077        {
078            Site site = _siteManager.getSite(siteName);
079            Sitemap sitemap = site.getSitemap(lang);
080            _saxSitemapInformation(sitemap);
081        }
082        contentHandler.endDocument();
083    }
084    
085    private void _saxSitemapInformation (Sitemap sitemap) throws SAXException
086    {
087        XMLUtils.startElement(contentHandler, "sitemap");
088        XMLUtils.createElement(contentHandler, "siteName", sitemap.getSite().getName());
089        XMLUtils.createElement(contentHandler, "lang", sitemap.getName());
090        XMLUtils.createElement(contentHandler, "id", sitemap.getId());
091        XMLUtils.createElement(contentHandler, "name", sitemap.getName());
092        
093        List<SitemapDecorator> decorators = _decoratorsManager.getDecorators(sitemap);
094        for (SitemapDecorator decorator : decorators)
095        {
096            AttributesImpl attr = new AttributesImpl();
097            
098            if (StringUtils.isNotBlank(decorator.getIcon()))
099            {
100                attr.addCDATAAttribute("icon", decorator.getIcon());
101            }
102            if (StringUtils.isNotBlank(decorator.getIconGlyph()))
103            {
104                attr.addCDATAAttribute("iconGlyph", decorator.getIconGlyph());
105            }
106            attr.addCDATAAttribute("id", decorator.getId());
107            XMLUtils.startElement(contentHandler, "decorator", attr);
108            decorator.getLabel().toSAX(contentHandler);
109            XMLUtils.endElement(contentHandler, "decorator");
110        }
111        
112        XMLUtils.endElement(contentHandler, "sitemap");
113    }
114    
115    private void _saxSiteInformation (Site site) throws SAXException
116    {
117        XMLUtils.startElement(contentHandler, "site");
118        XMLUtils.createElement(contentHandler, "id", site.getId());
119        XMLUtils.createElement(contentHandler, "name", site.getName());
120        XMLUtils.endElement(contentHandler, "site");
121    }
122    
123    private void _saxRootSitesInformation (AmetysObject rootSite) throws SAXException
124    {
125        XMLUtils.startElement(contentHandler, "root-sites");
126        XMLUtils.createElement(contentHandler, "id", rootSite.getId());
127        XMLUtils.endElement(contentHandler, "root-sites");
128    }
129
130}