001/*
002 *  Copyright 2015 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;
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.apache.commons.lang3.StringUtils;
028import org.xml.sax.SAXException;
029
030import org.ametys.cms.content.AdditionalContentPropertiesGenerator;
031import org.ametys.cms.repository.Content;
032import org.ametys.plugins.repository.AmetysRepositoryException;
033import org.ametys.web.repository.content.SharedContent;
034import org.ametys.web.repository.content.WebContent;
035import org.ametys.web.repository.content.shared.SharedContentManager;
036import org.ametys.web.repository.page.Page;
037import org.ametys.web.repository.page.Zone;
038import org.ametys.web.repository.page.ZoneItem;
039
040/**
041 * Generates addition information on web content
042 */
043public class AdditionalWebContentPropertiesGenerator extends AdditionalContentPropertiesGenerator
044{
045
046    /** The shared content helper. */
047    protected SharedContentManager _sharedContentComponent;
048
049    @Override
050    public void service(ServiceManager serviceManager) throws ServiceException
051    {
052        super.service(serviceManager);
053        _sharedContentComponent = (SharedContentManager) serviceManager.lookup(SharedContentManager.ROLE);
054    }
055    
056    @Override
057    protected void _saxOtherProperties(Content content) throws SAXException
058    {
059        _saxContentPages(content);
060        _saxSharedContent(content);
061    }
062    
063    @Override
064    protected Map<String, Object> getContextualParameters(Content content)
065    {
066        Map<String, Object> contextualParameters = super.getContextualParameters(content);
067        if (content instanceof WebContent)
068        {
069            contextualParameters.put("siteName", ((WebContent) content).getSiteName());
070        }
071        return contextualParameters;
072    }
073
074    private void _saxSharedContent(Content content) throws SAXException
075    {
076        Locale locale = content.getLanguage() != null ? new Locale(content.getLanguage()) : null;
077        
078        Set<SharedContent> sharedContents = _sharedContentComponent.getSharedContents(content);
079
080        if (content instanceof SharedContent)
081        {
082            Content initialContent = ((SharedContent) content).getInitialContent();
083
084            if (initialContent != null)
085            {
086                AttributesImpl icAttrs = new AttributesImpl();
087
088                icAttrs.addCDATAAttribute("id", initialContent.getId());
089                icAttrs.addCDATAAttribute("title", initialContent.getTitle(locale));
090
091                XMLUtils.startElement(contentHandler, "initial-content", icAttrs);
092                if (initialContent instanceof WebContent)
093                {
094                    Collection<Page> pages = ((WebContent) initialContent).getReferencingPages();
095                    for (Page page : pages)
096                    {
097                        _saxPage(page, null, null);
098                    }
099                }
100                XMLUtils.endElement(contentHandler, "initial-content");
101            }
102        }
103
104        for (SharedContent sharedContent : sharedContents)
105        {
106            AttributesImpl sharedAttrs = new AttributesImpl();
107            sharedAttrs.addCDATAAttribute("contentId", sharedContent.getId());
108            sharedAttrs.addCDATAAttribute("title", sharedContent.getTitle(locale));
109
110            XMLUtils.startElement(contentHandler, "share", sharedAttrs);
111            for (Page page : sharedContent.getReferencingPages())
112            {
113                _saxPage(page, null, null);
114            }
115            XMLUtils.endElement(contentHandler, "share");
116        }
117    }
118
119    private void _saxContentPages(Content content) throws SAXException
120    {
121        XMLUtils.startElement(contentHandler, "pages");
122
123        if (content instanceof WebContent)
124        {
125            Collection<ZoneItem> zoneItems = ((WebContent) content).getReferencingZoneItems();
126
127            // For a shared content, we are mainly interrested in refereces of the initial content!
128            if (content instanceof SharedContent && ((SharedContent) content).getInitialContent() instanceof WebContent)
129            {
130                zoneItems.addAll(((WebContent) ((SharedContent) content).getInitialContent()).getReferencingZoneItems());
131            }
132
133            for (ZoneItem zoneItem : zoneItems)
134            {
135                Zone zone = zoneItem.getZone();
136                Page page = zone.getPage();
137
138                _saxPage(page, zone, zoneItem);
139            }
140        }
141
142        XMLUtils.endElement(contentHandler, "pages");
143    }
144
145    private String _escape(String target)
146    {
147        if (target == null)
148        {
149            return "";
150        }
151        return target.replaceAll("&", "&amp;").replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
152    }
153
154    /**
155     * SAX a content's page.
156     * @param page the page to SAX.
157     * @param zone the zone the content is in.
158     * @param zoneItem the the zone item the content belongs to.
159     * @throws SAXException if an error occurs.
160     */
161    protected void _saxPage(Page page, Zone zone, ZoneItem zoneItem) throws SAXException
162    {
163        AttributesImpl cAttsPage = new AttributesImpl();
164        cAttsPage.addCDATAAttribute("id", _escape(page.getId()));
165        cAttsPage.addCDATAAttribute("name", _escape(page.getName()));
166        cAttsPage.addCDATAAttribute("path", _escape(page.getPathInSitemap()));
167        cAttsPage.addCDATAAttribute("title", _escape(page.getTitle()));
168        cAttsPage.addCDATAAttribute("long-title", _escape(page.getLongTitle()));
169        cAttsPage.addCDATAAttribute("lang", _escape(page.getSitemap().getName()));
170        cAttsPage.addCDATAAttribute("site", _escape(page.getSite().getName()));
171        if (zone != null)
172        {
173            cAttsPage.addCDATAAttribute("zone", _escape(zone.getName()));
174        }
175        if (zoneItem != null)
176        {
177            cAttsPage.addCDATAAttribute("metadataSetName", _escape(StringUtils.defaultString(zoneItem.getViewName(), "main")));
178        }
179        _saxTags (page, cAttsPage);
180
181        XMLUtils.createElement(contentHandler, "page", cAttsPage);
182    }
183
184    /**
185     * SAX page's tags
186     * @param page the page  
187     * @param attrs the attributes to SAX into
188     * @throws AmetysRepositoryException if failed to get page's tags
189     */
190    protected void _saxTags(Page page, AttributesImpl attrs) throws AmetysRepositoryException
191    {
192        for (String tag : page.getTags())
193        {
194            // FIXME use nested element not this dirty XML
195            attrs.addCDATAAttribute("PLUGIN_TAGS_" + tag, "empty");
196        }
197    }
198}