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.content; 017 018import java.util.Collection; 019import java.util.HashMap; 020import java.util.Map; 021import java.util.Set; 022 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.cocoon.ProcessingException; 026import org.apache.cocoon.environment.ObjectModelHelper; 027import org.apache.cocoon.environment.Request; 028import org.apache.cocoon.generation.Generator; 029import org.apache.cocoon.xml.AttributesImpl; 030import org.apache.cocoon.xml.XMLUtils; 031import org.xml.sax.SAXException; 032 033import org.ametys.cms.repository.Content; 034import org.ametys.cms.repository.TagAwareAmetysObject; 035import org.ametys.cms.tag.Tag; 036import org.ametys.cms.tag.TagProviderExtensionPoint; 037import org.ametys.web.cache.PageHelper; 038import org.ametys.web.repository.content.SharedContent; 039import org.ametys.web.repository.content.WebContent; 040import org.ametys.web.repository.page.Page; 041 042/** 043 * {@link Generator} for rendering raw content data. 044 */ 045public class ContentGenerator extends org.ametys.cms.content.ContentGenerator 046{ 047 private TagProviderExtensionPoint _tagProviderEP; 048 private PageHelper _pageHelper; 049 050 @Override 051 public void service(ServiceManager serviceManager) throws ServiceException 052 { 053 super.service(serviceManager); 054 _tagProviderEP = (TagProviderExtensionPoint) serviceManager.lookup(TagProviderExtensionPoint.ROLE); 055 _pageHelper = (PageHelper) serviceManager.lookup(PageHelper.ROLE); 056 } 057 058 @Override 059 protected void _saxOtherData(Content content) throws SAXException, ProcessingException 060 { 061 if (content instanceof WebContent) 062 { 063 XMLUtils.startElement(contentHandler, "tags"); 064 065 String siteName = ((WebContent) content).getSiteName(); 066 067 Set<String> tags = ((TagAwareAmetysObject) content).getTags(); 068 for (String tagName : tags) 069 { 070 Map<String, Object> contextParameters = new HashMap<>(); 071 contextParameters.put("siteName", siteName); 072 Tag tag = _tagProviderEP.getTag(tagName, contextParameters); 073 074 if (tag != null) 075 { 076 AttributesImpl attrs = new AttributesImpl(); 077 if (tag.getParentName() != null) 078 { 079 attrs.addCDATAAttribute("parent", tag.getParentName()); 080 } 081 082 XMLUtils.startElement(contentHandler, tagName, attrs); 083 tag.getTitle().toSAX(contentHandler); 084 XMLUtils.endElement(contentHandler, tagName); 085 } 086 } 087 088 XMLUtils.endElement(contentHandler, "tags"); 089 090 AttributesImpl attr = new AttributesImpl(); 091 092 Request request = ObjectModelHelper.getRequest(objectModel); 093 Page page = (Page) request.getAttribute(Page.class.getName()); 094 095 boolean captchaRequiredOnComments = true; 096 097 if (page != null && content.getMetadataHolder().getBoolean("comment", false)) 098 { 099 Collection<Page> referencingPages = ((WebContent) content).getReferencingPages(); 100 for (Page refPage : referencingPages) 101 { 102 if (refPage.equals(page)) 103 { 104 attr.addCDATAAttribute("pageId", page.getId()); 105 captchaRequiredOnComments = _pageHelper.isCaptchaRequired(page); 106 break; 107 } 108 } 109 } 110 111 XMLUtils.createElement(contentHandler, "comments-captcha-required", attr, String.valueOf(captchaRequiredOnComments)); 112 } 113 114 if (content instanceof SharedContent) 115 { 116 XMLUtils.createElement(contentHandler, "sharedContent", "true"); 117 } 118 } 119}