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 } 051 052 @Override 053 protected Locale getDefaultLocale(Request request) 054 { 055 String sitemapLanguage = (String) request.getAttribute(WebConstants.REQUEST_ATTR_SITEMAP_NAME); 056 if (sitemapLanguage != null) 057 { 058 return new Locale(sitemapLanguage); 059 } 060 return super.getDefaultLocale(request); 061 } 062 063 @Override 064 protected void _saxOtherData(Content content) throws SAXException, ProcessingException 065 { 066 if (content instanceof WebContent) 067 { 068 AttributesImpl attr = new AttributesImpl(); 069 070 Request request = ObjectModelHelper.getRequest(objectModel); 071 Page page = (Page) request.getAttribute(WebConstants.REQUEST_ATTR_PAGE); 072 073 boolean captchaRequiredOnComments = true; 074 075 if (page != null && content.getMetadataHolder().getBoolean("comment", false)) 076 { 077 Collection<Page> referencingPages = ((WebContent) content).getReferencingPages(); 078 for (Page refPage : referencingPages) 079 { 080 if (refPage.equals(page)) 081 { 082 attr.addCDATAAttribute("pageId", page.getId()); 083 captchaRequiredOnComments = _pageHelper.isCaptchaRequired(page); 084 break; 085 } 086 } 087 } 088 089 XMLUtils.createElement(contentHandler, "comments-captcha-required", attr, String.valueOf(captchaRequiredOnComments)); 090 } 091 092 if (content instanceof SharedContent) 093 { 094 XMLUtils.createElement(contentHandler, "sharedContent", "true"); 095 } 096 } 097}