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