001/*
002 *  Copyright 2019 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.HashMap;
019import java.util.Locale;
020import java.util.Map;
021import java.util.Set;
022
023import org.apache.avalon.framework.context.Context;
024import org.apache.avalon.framework.context.ContextException;
025import org.apache.avalon.framework.context.Contextualizable;
026import org.apache.avalon.framework.service.ServiceException;
027import org.apache.avalon.framework.service.ServiceManager;
028import org.apache.cocoon.components.ContextHelper;
029import org.apache.cocoon.environment.Request;
030import org.apache.cocoon.xml.AttributesImpl;
031import org.apache.cocoon.xml.XMLUtils;
032import org.apache.commons.lang3.StringUtils;
033import org.xml.sax.ContentHandler;
034import org.xml.sax.SAXException;
035
036import org.ametys.cms.repository.Content;
037import org.ametys.cms.tag.Tag;
038import org.ametys.cms.tag.TagProviderExtensionPoint;
039import org.ametys.plugins.repository.tag.TagAwareAmetysObject;
040import org.ametys.runtime.model.View;
041import org.ametys.web.WebConstants;
042import org.ametys.web.WebHelper;
043import org.ametys.web.repository.content.WebContent;
044
045/**
046 * Generates SAX events for Content, including tags for {@link WebContent}.
047 */
048public class ContentSaxer extends org.ametys.cms.content.ContentSaxer implements Contextualizable
049{
050    private TagProviderExtensionPoint _tagProviderEP;
051
052    private Context _context;
053
054    @Override
055    public void service(ServiceManager manager) throws ServiceException
056    {
057        super.service(manager);
058        _tagProviderEP = (TagProviderExtensionPoint) manager.lookup(TagProviderExtensionPoint.ROLE);
059    }
060    
061    public void contextualize(Context context) throws ContextException
062    {
063        _context = context;
064    }
065    
066    @Override
067    protected void saxBody(Content content, ContentHandler contentHandler, Locale locale, View view, String tagName, boolean saxWorkflowStep, boolean saxWorkflowInfo, boolean saxLanguageInfo, String attributesTagName, boolean isEdition, boolean renderDisableValues) throws SAXException
068    {
069        super.saxBody(content, contentHandler, locale, view, tagName, saxWorkflowStep, saxWorkflowInfo, saxLanguageInfo, attributesTagName, isEdition, renderDisableValues);
070        
071        if (content instanceof WebContent)
072        {
073            saxSiteName((WebContent) content, contentHandler);
074        }
075        
076        saxTags(content, contentHandler);
077    }
078    
079    /**
080     * Generates SAX events for the site name.
081     * @param content the {@link WebContent}.
082     * @param contentHandler the ContentHandler receving SAX events.
083     * @throws SAXException if an error occurs during the SAX events generation.
084     */
085    protected void saxSiteName(WebContent content, ContentHandler contentHandler) throws SAXException
086    {
087        AttributesImpl attrs = new AttributesImpl();
088        attrs.addCDATAAttribute("name", content.getSiteName());
089        XMLUtils.createElement(contentHandler, "site", attrs);
090    }
091    
092    /**
093     * Generates SAX events for tags.
094     * @param content the content
095     * @param contentHandler the ContentHandler receving SAX events.
096     * @throws SAXException if an error occurs during the SAX events generation.
097     */
098    protected void saxTags(Content content, ContentHandler contentHandler) throws SAXException
099    {
100        XMLUtils.startElement(contentHandler, "tags");
101
102        Request request = ContextHelper.getRequest(_context);
103        String siteName = WebHelper.getSiteName(request, content);
104
105        Set<String> tags = ((TagAwareAmetysObject) content).getTags();
106        for (String tagName : tags)
107        {
108            Map<String, Object> contextParameters = new HashMap<>();
109            if (StringUtils.isNotEmpty(siteName))
110            {
111                contextParameters.put("siteName", siteName);
112            }
113            
114            Tag tag = _tagProviderEP.getTag(tagName, contextParameters);
115            if (tag != null)
116            {
117                AttributesImpl attrs = new AttributesImpl();
118                if (tag.getParentName() != null)
119                {
120                    attrs.addCDATAAttribute("parent", tag.getParentName());
121                }
122
123                XMLUtils.startElement(contentHandler, tagName, attrs);
124                tag.getTitle().toSAX(contentHandler);
125                XMLUtils.endElement(contentHandler, tagName);
126            }
127        }
128
129        XMLUtils.endElement(contentHandler, "tags");
130    }
131    
132    @SuppressWarnings("cast")
133    @Override
134    protected Map<String, Object> getContextualParameters(Content content)
135    {
136        Map<String, Object> ctxParams = new HashMap<>(super.getContextualParameters(content));
137        
138        Request request = ContextHelper.getRequest(_context);
139        
140        ctxParams.put("sitemapLanguage", (String) request.getAttribute(WebConstants.REQUEST_ATTR_SITEMAP_NAME));
141        ctxParams.put("siteName", WebHelper.getSiteName(request));
142        
143        return ctxParams;
144    }
145}