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.plugins.repository.AmetysObjectIterable;
034import org.ametys.plugins.repository.AmetysObjectResolver;
035import org.ametys.plugins.repository.TraversableAmetysObject;
036import org.ametys.web.repository.page.MoveablePage;
037import org.ametys.web.repository.page.Page;
038import org.ametys.web.repository.page.SitemapElement;
039import org.ametys.web.repository.site.Site;
040import org.ametys.web.repository.sitemap.Sitemap;
041import org.ametys.web.sitemap.SitemapIndicatorsHandler;
042import org.ametys.web.sitemap.SitemapTreeIndicator;
043
044/**
045 * SAX child nodes of a node. The node can be a site, sitemap or page.<br>
046 * Waits for the id of the node to SAX in request parameters.
047 */
048public class SitemapGenerator extends ServiceableGenerator
049{
050    /** The {@link AmetysObjectResolver} */
051    protected AmetysObjectResolver _resolver;
052    /** The {@link SitemapIndicatorsHandler} */
053    protected SitemapIndicatorsHandler _decoratorsManager;
054    
055    @Override
056    public void service(ServiceManager smanager) throws ServiceException
057    {
058        super.service(smanager);
059        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
060        _decoratorsManager = (SitemapIndicatorsHandler) smanager.lookup(SitemapIndicatorsHandler.ROLE);
061    }
062
063    @Override
064    public void generate() throws IOException, SAXException, ProcessingException
065    {
066        Request request = ObjectModelHelper.getRequest(objectModel);
067        String node = request.getParameter("node");
068       
069        contentHandler.startDocument();
070
071        AmetysObject object = _resolver.resolveById(node);
072        if (object instanceof Site)
073        {
074            // SAX the sitemaps
075            XMLUtils.startElement(contentHandler, "sitemaps");
076            AmetysObjectIterable< ? extends Sitemap> sitemaps =  ((Site) object).getSitemaps();
077            for (Sitemap sitemap : sitemaps)
078            {
079                _saxSitemap(sitemap);
080            }
081            XMLUtils.endElement(contentHandler, "sitemaps");
082        }
083        else if (object instanceof SitemapElement)
084        {
085            // SAX child pages
086            XMLUtils.startElement(contentHandler, "pages");
087            AmetysObjectIterable< ? extends Page> childPages = ((SitemapElement) object).getChildrenPages();
088            for (Page page : childPages)
089            {
090                _saxPage(page);
091            }
092            XMLUtils.endElement(contentHandler, "pages");
093        }
094        else
095        {
096            // SAX sites
097            XMLUtils.startElement(contentHandler, "sites");
098            AmetysObjectIterable< ? extends Site> sites = ((TraversableAmetysObject) object).getChildren();
099            for (Site site : sites)
100            {
101                _saxSite(site);
102            }
103            XMLUtils.endElement(contentHandler, "sites");
104        }
105
106        contentHandler.endDocument();
107
108    }
109
110    /**
111     * SAX a page
112     * @param page The page to SAX
113     * @throws SAXException If an error occurs while SAXing
114     */
115    protected void _saxPage (Page page) throws SAXException
116    {
117        AttributesImpl attrs = new AttributesImpl();
118        attrs.addAttribute("", "title", "title", "CDATA", page.getTitle());
119        attrs.addAttribute("", "long-title", "long-title", "CDATA", page.getLongTitle());
120        //attr.addAttribute("", "url", "url", "CDATA", page.getURL());
121        attrs.addAttribute("", "name", "name", "CDATA", page.getName());
122        attrs.addAttribute("", "id", "id", "CDATA", page.getId());
123        attrs.addAttribute("", "path", "path", "CDATA", page.getPathInSitemap());
124        if (page instanceof MoveablePage)
125        {
126            attrs.addAttribute("", "moveable", "moveable", "CDATA", "true");
127        }
128
129        SitemapTreeIndicator icon = _decoratorsManager.getIcon(page);
130        if (icon != null)
131        {
132            attrs.addAttribute("", "icon", "icon", "CDATA", icon.getIcon());
133        }
134        if (page.getChildrenPages().iterator().hasNext())
135        {
136            attrs.addAttribute("", "hasChild", "hasChild", "CDATA", "true");
137        }
138
139        XMLUtils.startElement(contentHandler, "page", attrs);
140        
141        List<SitemapTreeIndicator> decorators = _decoratorsManager.getDecorators(page);
142        for (SitemapTreeIndicator decorator : decorators)
143        {
144            AttributesImpl attr = new AttributesImpl();
145            if (StringUtils.isNotBlank(decorator.getIcon()))
146            {
147                attr.addCDATAAttribute("icon", decorator.getIcon());
148            }
149            if (StringUtils.isNotBlank(decorator.getIconGlyph()))
150            {
151                attr.addCDATAAttribute("iconGlyph", decorator.getIconGlyph());
152            }
153            attr.addCDATAAttribute("id", decorator.getId());
154            XMLUtils.startElement(contentHandler, "decorator", attr);
155            decorator.getLabel().toSAX(contentHandler);
156            XMLUtils.endElement(contentHandler, "decorator");
157        }
158        
159        XMLUtils.endElement(contentHandler, "page");
160    }
161    
162    /**
163     * SAX a {@link Site}
164     * @param site The site to SAX
165     * @throws SAXException If an error occurs while SAXing
166     */
167    protected void _saxSite (Site site) throws SAXException
168    {
169        AttributesImpl attrs = new AttributesImpl();
170        attrs.addAttribute("", "id", "id", "CDATA", site.getId());
171        attrs.addAttribute("", "name", "name", "CDATA", site.getName());
172        attrs.addAttribute("", "title", "title", "CDATA", site.getTitle());
173        XMLUtils.createElement(contentHandler, "site", attrs);
174    }
175
176    /**
177     * SAX a {@link Sitemap}
178     * @param sitemap The sitemap to SAX
179     * @throws SAXException If an error occurs while SAXing
180     */
181    protected void _saxSitemap (Sitemap sitemap) throws SAXException
182    {
183        AttributesImpl attrs = new AttributesImpl();
184        attrs.addAttribute("", "id", "id", "CDATA", sitemap.getId());
185        attrs.addAttribute("", "name", "name", "CDATA", sitemap.getName());
186        XMLUtils.createElement(contentHandler, "sitemap", attrs);
187    }
188}