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.transformation.htmledition;
018
019import java.util.Map;
020
021import org.apache.cocoon.components.ContextHelper;
022import org.apache.cocoon.environment.ObjectModelHelper;
023import org.apache.cocoon.xml.AttributesImpl;
024import org.xml.sax.Attributes;
025import org.xml.sax.SAXException;
026
027import org.ametys.runtime.model.type.DataContext;
028
029/**
030 * Handler to enhance the rich text attachments.
031 * Add the content id and the data path to the local attachment's attribute
032 */
033public class HTMLLocalMediaObjectEditionHandler extends AbstractHTMLEditionHandler
034{
035    private static final String __ATTACHMENT_IMAGE_TAG_NAME = "img";
036    private static final String __ATTACHMENT_VIDEO_AUDIO_TAG_NAME = "media";
037    private static final String __ATTACHMENT_TYPE_ATTRIBUTE_NAME = "data-ametys-type";
038    private static final String __ATTACHMENT_TYPE_ATTRIBUTE_LOCAL_VALUE = "local";
039
040    private DataContext _dataContext;
041    
042    @Override
043    public void startDocument() throws SAXException
044    {
045        Map objectModel = ContextHelper.getObjectModel(_context);
046        Map parentContextParameters = (Map) objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
047        _dataContext = (DataContext) parentContextParameters.get("dataContext");
048        
049        super.startDocument();
050    }
051
052    @Override
053    public void startElement(String uri, String loc, String raw, Attributes attrs) throws SAXException
054    {
055        // A new attachment starts being saxed
056        boolean isAttachment = _isAttachment(loc);
057        String type = attrs.getValue(__ATTACHMENT_TYPE_ATTRIBUTE_NAME);
058        if (isAttachment && __ATTACHMENT_TYPE_ATTRIBUTE_LOCAL_VALUE.equals(type))
059        {
060            Attributes newAttrs = _processAttachment(attrs);
061            super.startElement(uri, loc, raw, newAttrs);
062        }
063        else
064        {
065            AttributesImpl newAttrs = new AttributesImpl(attrs);
066            newAttrs.addCDATAAttribute("absolutehref", attrs.getValue("href"));
067            super.startElement(uri, loc, raw, newAttrs);
068        }
069    }
070
071    private boolean _isAttachment(String loc)
072    {
073        return __ATTACHMENT_IMAGE_TAG_NAME.equals(loc) || __ATTACHMENT_VIDEO_AUDIO_TAG_NAME.equals(loc);
074    }
075
076    private Attributes _processAttachment(Attributes attrs) throws SAXException
077    {
078        String contentId = _dataContext.getObjectId()
079                .orElseThrow(() -> new SAXException("The object id is required in the data context in order to set the href"));
080
081        String dataPath = _dataContext.getDataPath();
082        String fileName = attrs.getValue("href");
083
084        AttributesImpl newAttrs = new AttributesImpl(attrs);
085        newAttrs.addCDATAAttribute("absolutehref", contentId + "@" + dataPath + ";" + fileName);
086
087        return newAttrs;
088    }
089}