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