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