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