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;
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.AttributesImpl;
027import org.apache.cocoon.xml.XMLUtils;
028import org.xml.sax.SAXException;
029
030import org.ametys.plugins.repository.AmetysObject;
031import org.ametys.plugins.repository.AmetysObjectIterable;
032import org.ametys.plugins.repository.AmetysObjectResolver;
033import org.ametys.plugins.repository.TraversableAmetysObject;
034import org.ametys.web.repository.page.Page;
035import org.ametys.web.repository.page.PagesContainer;
036import org.ametys.web.repository.site.Site;
037import org.ametys.web.repository.sitemap.Sitemap;
038
039/**
040 * SAX events for sitemaps' nodes
041 *
042 */
043public class SitemapNodeGenerator extends ServiceableGenerator
044{
045    /** The {@link AmetysObjectResolver} */
046    protected AmetysObjectResolver _resolver;
047
048    @Override
049    public void service(ServiceManager smanager) throws ServiceException
050    {
051        super.service(smanager);
052        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
053    }
054    
055    @Override
056    public void generate() throws IOException, SAXException, ProcessingException
057    {
058        Request request = ObjectModelHelper.getRequest(objectModel);
059        String node = request.getParameter("node");
060       
061        contentHandler.startDocument();
062
063        AmetysObject object = _resolver.resolveById(node);
064        if (object instanceof Site)
065        {
066            // SAX the sitemaps
067            XMLUtils.startElement(contentHandler, "sitemaps");
068            AmetysObjectIterable< ? extends Sitemap> sitemaps =  ((Site) object).getSitemaps();
069            for (Sitemap sitemap : sitemaps)
070            {
071                _saxSitemap(sitemap);
072            }
073            XMLUtils.endElement(contentHandler, "sitemaps");
074        }
075        else if (object instanceof PagesContainer)
076        {
077            // SAX child pages
078            XMLUtils.startElement(contentHandler, "pages");
079            AmetysObjectIterable< ? extends Page> childPages = ((PagesContainer) object).getChildrenPages();
080            for (Page page : childPages)
081            {
082                _saxPage(page);
083                
084            }
085            XMLUtils.endElement(contentHandler, "pages");
086        }
087        else
088        {
089            // SAX sites
090            XMLUtils.startElement(contentHandler, "sites");
091            AmetysObjectIterable< ? extends Site> sites = ((TraversableAmetysObject) object).getChildren();
092            for (Site site : sites)
093            {
094                _saxSite(site);
095            }
096            XMLUtils.endElement(contentHandler, "sites");
097        }
098
099        contentHandler.endDocument();
100    }
101    
102    /**
103     * SAX a page
104     * @param page The page to SAX
105     * @throws SAXException If an error occurs while SAXing
106     */
107    protected void _saxPage (Page page) throws SAXException
108    {
109        AttributesImpl attrs = new AttributesImpl();
110        attrs.addAttribute("", "title", "title", "CDATA", page.getTitle());
111        attrs.addAttribute("", "long-title", "long-title", "CDATA", page.getLongTitle());
112        //attr.addAttribute("", "url", "url", "CDATA", page.getURL());
113        attrs.addAttribute("", "name", "name", "CDATA", page.getName());
114        attrs.addAttribute("", "id", "id", "CDATA", page.getId());
115        attrs.addAttribute("", "path", "path", "CDATA", page.getPathInSitemap());
116
117        if (page.getChildrenPages().iterator().hasNext())
118        {
119            attrs.addAttribute("", "hasChild", "hasChild", "CDATA", "true");
120        }
121
122        XMLUtils.createElement(contentHandler, "page", attrs);
123    }
124    
125    /**
126     * SAX a {@link Site}
127     * @param site The site to SAX
128     * @throws SAXException If an error occurs while SAXing
129     */
130    protected void _saxSite (Site site) throws SAXException
131    {
132        AttributesImpl attrs = new AttributesImpl();
133        attrs.addAttribute("", "id", "id", "CDATA", site.getId());
134        attrs.addAttribute("", "name", "name", "CDATA", site.getName());
135        XMLUtils.createElement(contentHandler, "site", attrs);
136    }
137
138    /**
139     * SAX a {@link Sitemap}
140     * @param sitemap The sitemap to SAX
141     * @throws SAXException If an error occurs while SAXing
142     */
143    protected void _saxSitemap (Sitemap sitemap) throws SAXException
144    {
145        AttributesImpl attrs = new AttributesImpl();
146        attrs.addAttribute("", "id", "id", "CDATA", sitemap.getId());
147        attrs.addAttribute("", "name", "name", "CDATA", sitemap.getName());
148        XMLUtils.createElement(contentHandler, "sitemap", attrs);
149    }
150}