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.page.generators;
017
018import java.io.IOException;
019import java.util.Locale;
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.xml.sax.SAXException;
030
031import org.ametys.cms.repository.Content;
032import org.ametys.plugins.repository.AmetysObjectIterable;
033import org.ametys.plugins.repository.AmetysObjectResolver;
034import org.ametys.web.repository.page.Page;
035import org.ametys.web.repository.page.Zone;
036import org.ametys.web.repository.page.ZoneItem;
037
038/**
039 * SAX the {@link Content} of a {@link Page} 
040 */
041public class PageContentsGenerator extends ServiceableGenerator
042{
043    /** The {@link AmetysObjectResolver} for Ametys objects*/
044    protected AmetysObjectResolver _resolver;
045
046    @Override
047    public void service(ServiceManager serviceManager) throws ServiceException
048    {
049        super.service(serviceManager);
050        _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
051    }
052    
053    @Override
054    public void generate() throws IOException, SAXException, ProcessingException
055    {
056        Request request = ObjectModelHelper.getRequest(objectModel);
057        contentHandler.startDocument();
058        
059        XMLUtils.startElement(contentHandler, "contents");
060        
061        String pageId = request.getParameter("pageId");
062        if (pageId != null)
063        {
064            Page page = _resolver.resolveById(pageId);
065            
066            if (page.getType() == Page.PageType.CONTAINER)
067            {
068                for (Zone zone : page.getZones())
069                {
070                    AmetysObjectIterable<? extends ZoneItem> zoneItems = zone.getZoneItems();
071                    for (ZoneItem zoneItem : zoneItems)
072                    {
073                        if (zoneItem.getType() == ZoneItem.ZoneType.CONTENT)
074                        {
075                            Content content = zoneItem.getContent();
076                            
077                            AttributesImpl atts = new AttributesImpl();
078                            atts.addCDATAAttribute("id", content.getId());
079                            atts.addCDATAAttribute("name", content.getName());
080                            atts.addCDATAAttribute("title", content.getTitle(new Locale(page.getSitemapName())));
081                            XMLUtils.createElement(contentHandler, "content", atts);
082                        }
083                    }
084                }
085            }
086        }
087        
088        XMLUtils.endElement(contentHandler, "contents");
089        contentHandler.endDocument();
090    }
091}