001/* 002 * Copyright 2011 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.sitemap; 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.AmetysObjectResolver; 033import org.ametys.web.repository.page.Page; 034 035/** 036 * Page icon and decorators 037 * 038 */ 039public class PageDecoratorsGenerator extends ServiceableGenerator 040{ 041 private SitemapDecoratorsHandler _sitemapHandler; 042 private AmetysObjectResolver _resolver; 043 044 @Override 045 public void service(ServiceManager smanager) throws ServiceException 046 { 047 super.service(smanager); 048 _sitemapHandler = (SitemapDecoratorsHandler) smanager.lookup(SitemapDecoratorsHandler.ROLE); 049 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 050 } 051 052 @Override 053 public void generate() throws IOException, SAXException, ProcessingException 054 { 055 Request request = ObjectModelHelper.getRequest(objectModel); 056 String pageId = request.getParameter("id"); 057 058 Page page = _resolver.resolveById(pageId); 059 060 contentHandler.startDocument(); 061 062 AttributesImpl attrs = new AttributesImpl(); 063 attrs.addAttribute("", "id", "id", "CDATA", page.getId()); 064 attrs.addAttribute("", "path", "path", "CDATA", page.getPathInSitemap()); 065 attrs.addAttribute("", "title", "title", "CDATA", page.getTitle()); 066 XMLUtils.startElement(contentHandler, "page", attrs); 067 068 SitemapIcon icon = _sitemapHandler.getIcon(page); 069 if (icon != null) 070 { 071 XMLUtils.createElement(contentHandler, "icon", icon.getIcon()); 072 } 073 List<SitemapDecorator> decorators = _sitemapHandler.getDecorators(page); 074 for (SitemapDecorator decorator : decorators) 075 { 076 AttributesImpl attr = new AttributesImpl(); 077 if (StringUtils.isNotBlank(decorator.getIcon())) 078 { 079 attr.addCDATAAttribute("icon", decorator.getIcon()); 080 } 081 if (StringUtils.isNotBlank(decorator.getIconGlyph())) 082 { 083 attr.addCDATAAttribute("iconGlyph", decorator.getIconGlyph()); 084 } 085 attr.addCDATAAttribute("id", decorator.getId()); 086 XMLUtils.startElement(contentHandler, "decorator", attr); 087 decorator.getLabel().toSAX(contentHandler); 088 XMLUtils.endElement(contentHandler, "decorator"); 089 } 090 091 XMLUtils.endElement(contentHandler, "page"); 092 contentHandler.endDocument(); 093 094 } 095 096}