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.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.cocoon.xml.AttributesImpl;
026import org.apache.cocoon.xml.XMLUtils;
027import org.xml.sax.ContentHandler;
028import org.xml.sax.SAXException;
029
030import org.ametys.cms.repository.Content;
031import org.ametys.cms.tag.Tag;
032import org.ametys.cms.tag.TagProviderExtensionPoint;
033import org.ametys.plugins.repository.tag.TagAwareAmetysObject;
034import org.ametys.runtime.model.View;
035import org.ametys.web.repository.content.WebContent;
036
037/**
038 * Generates SAX events for Content, including tags for {@link WebContent}.
039 */
040public class ContentSaxer extends org.ametys.cms.content.ContentSaxer
041{
042    /** Avalon role. */
043    public static final String WEB_CONTENT_SAXER_ROLE = ContentSaxer.class.getName();
044    
045    private TagProviderExtensionPoint _tagProviderEP;
046
047    @Override
048    public void service(ServiceManager manager) throws ServiceException
049    {
050        super.service(manager);
051        _tagProviderEP = (TagProviderExtensionPoint) manager.lookup(TagProviderExtensionPoint.ROLE);
052    }
053    
054    @Override
055    protected void saxBody(Content content, ContentHandler contentHandler, Locale locale, View view, String tagName, boolean saxWorkflowStep, boolean saxWorkflowInfo, boolean saxLanguageInfo, String attributesTagName, boolean isEdition) throws SAXException
056    {
057        super.saxBody(content, contentHandler, locale, view, tagName, saxWorkflowStep, saxWorkflowInfo, saxLanguageInfo, attributesTagName, isEdition);
058        
059        if (content instanceof WebContent)
060        {
061            saxSiteName((WebContent) content, contentHandler);
062            saxTags((WebContent) content, contentHandler);
063        }
064    }
065    
066    /**
067     * Generates SAX events for the site name.
068     * @param content the {@link WebContent}.
069     * @param contentHandler the ContentHandler receving SAX events.
070     * @throws SAXException if an error occurs during the SAX events generation.
071     */
072    protected void saxSiteName(WebContent content, ContentHandler contentHandler) throws SAXException
073    {
074        AttributesImpl attrs = new AttributesImpl();
075        attrs.addCDATAAttribute("name", content.getSiteName());
076        XMLUtils.createElement(contentHandler, "site", attrs);
077    }
078    
079    /**
080     * Generates SAX events for tags.
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 saxTags(WebContent content, ContentHandler contentHandler) throws SAXException
086    {
087        XMLUtils.startElement(contentHandler, "tags");
088
089        String siteName = content.getSiteName();
090
091        Set<String> tags = ((TagAwareAmetysObject) content).getTags();
092        for (String tagName : tags)
093        {
094            Map<String, Object> contextParameters = new HashMap<>();
095            contextParameters.put("siteName", siteName);
096            Tag tag = _tagProviderEP.getTag(tagName, contextParameters);
097
098            if (tag != null)
099            {
100                AttributesImpl attrs = new AttributesImpl();
101                if (tag.getParentName() != null)
102                {
103                    attrs.addCDATAAttribute("parent", tag.getParentName());
104                }
105
106                XMLUtils.startElement(contentHandler, tagName, attrs);
107                tag.getTitle().toSAX(contentHandler);
108                XMLUtils.endElement(contentHandler, tagName);
109            }
110        }
111
112        XMLUtils.endElement(contentHandler, "tags");
113    }
114}