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