001/*
002 *  Copyright 2021 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 */
016
017package org.ametys.cms.data;
018
019import java.util.Optional;
020
021import org.apache.cocoon.xml.AttributesImpl;
022import org.apache.excalibur.xml.sax.ContentHandlerProxy;
023import org.xml.sax.Attributes;
024import org.xml.sax.ContentHandler;
025import org.xml.sax.SAXException;
026
027import org.ametys.plugins.repository.data.ametysobject.DataAwareAmetysObject;
028import org.ametys.plugins.repository.model.RepositoryDataContext;
029import org.ametys.plugins.repository.version.VersionAwareAmetysObject;
030import org.ametys.runtime.model.type.DataContext;
031
032/**
033 * Proxy handler to enhance the rich text attachments.
034 * Add the content id and the data path to the local attachment's fileref attribute 
035 */
036public class HTMLLocalMediaObjectHandler extends ContentHandlerProxy
037{
038    private static final String __ATTACHMENT_IMAGE_TAG_NAME = "img";
039    private static final String __ATTACHMENT_VIDEO_AUDIO_TAG_NAME = "media";
040    private static final String __ATTACHMENT_TYPE_ATTRIBUTE_NAME = "data-ametys-type";
041    private static final String __ATTACHMENT_TYPE_ATTRIBUTE_LOCAL_VALUE = "local";
042
043    private DataContext _context;
044    
045    /**
046     * Constructor
047     * @param contentHandler the contentHandler to pass SAX events to
048     * @param context the context of the rich text to SAX
049     */
050    public HTMLLocalMediaObjectHandler(ContentHandler contentHandler, DataContext context)
051    {
052        super(contentHandler);
053        _context = context;
054    }
055
056    @Override
057    public void startElement(String uri, String loc, String raw, Attributes attrs) throws SAXException
058    {
059        // A new attachment starts being saxed
060        boolean isAttachment = _isAttachment(loc);
061        String type = attrs.getValue(__ATTACHMENT_TYPE_ATTRIBUTE_NAME);
062        if (isAttachment && __ATTACHMENT_TYPE_ATTRIBUTE_LOCAL_VALUE.equals(type))
063        {
064            Attributes newAttrs = _processAttachment(attrs);
065            super.startElement(uri, loc, raw, newAttrs);
066        }
067        else
068        {
069            super.startElement(uri, loc, raw, attrs);
070        }
071    }
072
073    private boolean _isAttachment(String loc)
074    {
075        return __ATTACHMENT_IMAGE_TAG_NAME.equals(loc) || __ATTACHMENT_VIDEO_AUDIO_TAG_NAME.equals(loc);
076    }
077
078    private Attributes _processAttachment(Attributes attrs) throws SAXException
079    {
080        RepositoryDataContext repoContext = _context instanceof RepositoryDataContext rc ? rc : RepositoryDataContext.newInstance(_context);
081        
082        DataAwareAmetysObject content = repoContext.getObject()
083                                                   .orElseThrow(() -> new SAXException("The object is required in the data context in order to set the href"));
084
085        String contentId = content.getId();
086        
087        Optional<String> contentVersion = Optional.of(content)
088                                                  .filter(VersionAwareAmetysObject.class::isInstance)
089                                                  .map(VersionAwareAmetysObject.class::cast)
090                                                  .map(VersionAwareAmetysObject::getRevision);
091
092        String dataPath = _context.getDataPath();
093        String fileName = attrs.getValue("href");
094
095        AttributesImpl newAttrs = new AttributesImpl();
096        _copyAttributes(attrs, newAttrs);
097        
098        StringBuilder newHrefAttribute = new StringBuilder();
099        newHrefAttribute.append(contentId);
100        contentVersion.ifPresent(v -> newHrefAttribute.append("|").append(v));
101        newHrefAttribute.append("@").append(dataPath)
102                        .append(";").append(fileName);
103        
104        newAttrs.addCDATAAttribute("href", newHrefAttribute.toString());
105
106        return newAttrs;
107    }
108
109    /**
110     * Copy the attributes except the fileref attribute
111     * @param attrs the attributes to copy.
112     * @param newAttrs the attributes to copy to.
113     */
114    private void _copyAttributes(Attributes attrs, AttributesImpl newAttrs)
115    {
116        for (int i = 0; i < attrs.getLength(); i++)
117        {
118            String name = attrs.getQName(i);
119
120            if (!"href".equals(name))
121            {
122                newAttrs.addAttribute(attrs.getURI(i), attrs.getLocalName(i), name, attrs.getType(i), attrs.getValue(i));
123            }
124        }
125    }
126}