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.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.Generator; 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.web.WebConstants; 033import org.ametys.web.cache.PageHelper; 034import org.ametys.web.repository.content.SharedContent; 035import org.ametys.web.repository.content.WebContent; 036import org.ametys.web.repository.page.Page; 037 038/** 039 * {@link Generator} for rendering raw content data. 040 */ 041public class ContentGenerator extends org.ametys.cms.content.ContentGenerator 042{ 043 private PageHelper _pageHelper; 044 045 @Override 046 public void service(ServiceManager serviceManager) throws ServiceException 047 { 048 super.service(serviceManager); 049 _pageHelper = (PageHelper) serviceManager.lookup(PageHelper.ROLE); 050 _contentSaxer = (ContentSaxer) serviceManager.lookup(ContentSaxer.WEB_CONTENT_SAXER_ROLE); 051 } 052 053 @Override 054 protected Locale getDefaultLocale(Request request) 055 { 056 String sitemapLanguage = (String) request.getAttribute(WebConstants.REQUEST_ATTR_SITEMAP_NAME); 057 if (sitemapLanguage != null) 058 { 059 return new Locale(sitemapLanguage); 060 } 061 return super.getDefaultLocale(request); 062 } 063 064 @Override 065 protected void _saxOtherData(Content content) throws SAXException, ProcessingException 066 { 067 if (content instanceof WebContent) 068 { 069 AttributesImpl attr = new AttributesImpl(); 070 071 Request request = ObjectModelHelper.getRequest(objectModel); 072 Page page = (Page) request.getAttribute(WebConstants.REQUEST_ATTR_PAGE); 073 074 boolean captchaRequiredOnComments = true; 075 076 if (page != null && content.getMetadataHolder().getBoolean("comment", false)) 077 { 078 Collection<Page> referencingPages = ((WebContent) content).getReferencingPages(); 079 for (Page refPage : referencingPages) 080 { 081 if (refPage.equals(page)) 082 { 083 attr.addCDATAAttribute("pageId", page.getId()); 084 captchaRequiredOnComments = _pageHelper.isCaptchaRequired(page); 085 break; 086 } 087 } 088 } 089 090 XMLUtils.createElement(contentHandler, "comments-captcha-required", attr, String.valueOf(captchaRequiredOnComments)); 091 } 092 093 if (content instanceof SharedContent) 094 { 095 XMLUtils.createElement(contentHandler, "sharedContent", "true"); 096 } 097 } 098}