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.repository.site;
017
018import java.io.IOException;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.cocoon.ProcessingException;
023import org.apache.cocoon.environment.ObjectModelHelper;
024import org.apache.cocoon.environment.Request;
025import org.apache.cocoon.generation.ServiceableGenerator;
026import org.apache.cocoon.xml.XMLUtils;
027import org.apache.commons.lang.StringUtils;
028import org.xml.sax.SAXException;
029import org.xml.sax.helpers.AttributesImpl;
030
031import org.ametys.plugins.repository.AmetysObjectIterable;
032import org.ametys.plugins.repository.AmetysObjectResolver;
033import org.ametys.plugins.repository.metadata.CompositeMetadata;
034import org.ametys.web.repository.sitemap.Sitemap;
035
036/**
037 * Generate sax events representing a {@link Site}.
038 */
039public class SiteGenerator extends ServiceableGenerator
040{
041    /** The site manager. */
042    protected SiteManager _siteManager;
043    
044    private AmetysObjectResolver _resolver;
045    
046    @Override
047    public void service(ServiceManager sManager) throws ServiceException
048    {
049        super.service(sManager);
050        _resolver = (AmetysObjectResolver) sManager.lookup(AmetysObjectResolver.ROLE);
051        _siteManager = (SiteManager) sManager.lookup(SiteManager.ROLE);
052    }
053    
054    @Override
055    public void generate() throws IOException, SAXException, ProcessingException
056    {
057        Request request = ObjectModelHelper.getRequest(objectModel);
058        String id = request.getParameter("id");
059        String siteName = parameters.getParameter("name", request.getParameter("name"));
060        
061        Site site = null;
062        if (StringUtils.isBlank(id) && StringUtils.isNotBlank(siteName))
063        {
064            site = _siteManager.getSite(siteName);
065        }
066        else
067        {
068            site = _resolver.resolveById(id);
069        }
070        
071        contentHandler.startDocument();
072        
073        AttributesImpl attr = new AttributesImpl();
074        attr.addAttribute("", "id", "id", "CDATA", site.getId());
075        attr.addAttribute("", "name", "name", "CDATA", site.getName());
076        attr.addAttribute("", "title", "title", "CDATA", StringUtils.defaultString(site.getTitle()));
077        attr.addAttribute("", "path", "path", "CDATA", site.getSitePath());
078        XMLUtils.startElement(contentHandler, "site", attr);
079        _saxSite (site);
080        XMLUtils.endElement(contentHandler, "site");
081        
082        contentHandler.endDocument();
083    }
084    
085    private void _saxSite (Site site) throws SAXException
086    {
087        CompositeMetadata metaHolder = site.getMetadataHolder();
088
089        XMLUtils.startElement(contentHandler, "metadata");
090        String[] metadataNames = metaHolder.getMetadataNames();
091        for (int i = 0; i < metadataNames.length; i++)
092        {
093            XMLUtils.createElement(contentHandler, metadataNames[i],  metaHolder.getString(metadataNames[i]));
094        }
095        
096        AmetysObjectIterable<Sitemap> sitemaps = site.getSitemaps();
097        for (Sitemap sitemap : sitemaps)
098        {
099            XMLUtils.createElement(contentHandler, "lang", sitemap.getName());
100        }
101        
102        XMLUtils.endElement(contentHandler, "metadata");
103    }
104
105}