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.skin;
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.web.repository.page.Page;
031import org.ametys.web.repository.site.Site;
032import org.ametys.web.repository.site.SiteManager;
033
034/**
035 * Generates information about the current template if available
036 */
037public class TemplateInformationGenerator extends ServiceableGenerator
038{
039    /** The skins manager */
040    protected SkinsManager _skinsManager;
041    /** The sites manager */
042    protected SiteManager _siteManager;
043    
044    @Override
045    public void service(ServiceManager smanager) throws ServiceException
046    {
047        super.service(smanager);
048        _skinsManager = (SkinsManager) smanager.lookup(SkinsManager.ROLE);
049        _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE);
050    }
051    
052    @Override
053    public void generate() throws IOException, SAXException, ProcessingException
054    {
055        contentHandler.startDocument();
056        XMLUtils.startElement(contentHandler, "template-information");
057        
058        Request request = ObjectModelHelper.getRequest(objectModel);
059
060        String templateId = (String) request.getAttribute("template");
061        String siteName = (String) request.getAttribute("site");
062
063        if (templateId != null && siteName != null)
064        {
065            Site site = _siteManager.getSite(siteName);
066            String skinId = site.getSkinId();
067            
068            Skin skin = _skinsManager.getSkin(skinId);
069            SkinTemplate template = skin.getTemplate(templateId);
070            if (template != null)
071            {
072                AttributesImpl templateAttrs = new AttributesImpl();
073                templateAttrs.addCDATAAttribute("id", template.getId());
074                XMLUtils.startElement(contentHandler, "template", templateAttrs);
075                template.getLabel().toSAX(contentHandler, "label");
076                template.getDescription().toSAX(contentHandler, "description");
077                
078                XMLUtils.startElement(contentHandler, "images");
079                XMLUtils.createElement(contentHandler, "large", template.getLargeImage());
080                XMLUtils.createElement(contentHandler, "medium", template.getMediumImage());
081                XMLUtils.createElement(contentHandler, "small", template.getSmallImage());
082                XMLUtils.endElement(contentHandler, "images");
083                
084                for (SkinTemplateZone zone : template.getZones().values())
085                {
086                    AttributesImpl zoneAttrs = new AttributesImpl();
087                    zoneAttrs.addCDATAAttribute("id", zone.getId());
088                    XMLUtils.startElement(contentHandler, "zone", zoneAttrs);
089                    zone.getLabel().toSAX(contentHandler, "label");
090                    zone.getDescription().toSAX(contentHandler, "description");
091                    
092                    XMLUtils.startElement(contentHandler, "images");
093                    XMLUtils.createElement(contentHandler, "large", zone.getLargeImage());
094                    XMLUtils.createElement(contentHandler, "medium", zone.getMediumImage());
095                    XMLUtils.createElement(contentHandler, "small", zone.getSmallImage());
096                    XMLUtils.endElement(contentHandler, "images");
097    
098                    XMLUtils.endElement(contentHandler, "zone");
099                }
100    
101                XMLUtils.endElement(contentHandler, "template");
102            }
103            else
104            {
105                Page page = (Page) request.getAttribute(Page.class.getName());
106                if (page != null)
107                {
108                    getLogger().warn("Page " + page.getSiteName() + "/" + page.getSitemapName() + "/" + page.getPath() + " (" + page.getId() + ") use an unexisting template '" + templateId + "' in skin '" + skinId + "'");
109                }
110                else
111                {
112                    getLogger().warn("Unexisting template '" + templateId + "' in skin '" + skinId + "'");
113                }
114            }
115        }
116
117        XMLUtils.endElement(contentHandler, "template-information");
118        contentHandler.endDocument();
119    }
120
121}