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 org.apache.cocoon.xml.AttributesImpl;
020import org.apache.excalibur.xml.sax.ContentHandlerProxy;
021import org.xml.sax.Attributes;
022import org.xml.sax.ContentHandler;
023import org.xml.sax.SAXException;
024
025import org.ametys.runtime.model.type.DataContext;
026
027/**
028 * Proxy handler to enhance the rich text attachments.
029 * Add the content id and the data path to the local attachment's fileref attribute 
030 */
031public class HTMLLocalMediaObjectHandler extends ContentHandlerProxy
032{
033    private static final String __ATTACHMENT_IMAGE_TAG_NAME = "img";
034    private static final String __ATTACHMENT_VIDEO_AUDIO_TAG_NAME = "media";
035    private static final String __ATTACHMENT_TYPE_ATTRIBUTE_NAME = "data-ametys-type";
036    private static final String __ATTACHMENT_TYPE_ATTRIBUTE_LOCAL_VALUE = "local";
037
038    private DataContext _context;
039    
040    /**
041     * Constructor
042     * @param contentHandler the contentHandler to pass SAX events to
043     * @param context the context of the rich text to SAX
044     */
045    public HTMLLocalMediaObjectHandler(ContentHandler contentHandler, DataContext context)
046    {
047        super(contentHandler);
048        _context = context;
049    }
050
051    @Override
052    public void startElement(String uri, String loc, String raw, Attributes attrs) throws SAXException
053    {
054        // A new attachment starts being saxed
055        boolean isAttachment = _isAttachment(loc);
056        String type = attrs.getValue(__ATTACHMENT_TYPE_ATTRIBUTE_NAME);
057        if (isAttachment && __ATTACHMENT_TYPE_ATTRIBUTE_LOCAL_VALUE.equals(type))
058        {
059            Attributes newAttrs = _processAttachment(attrs);
060            super.startElement(uri, loc, raw, newAttrs);
061        }
062        else
063        {
064            super.startElement(uri, loc, raw, attrs);
065        }
066    }
067
068    private boolean _isAttachment(String loc)
069    {
070        return __ATTACHMENT_IMAGE_TAG_NAME.equals(loc) || __ATTACHMENT_VIDEO_AUDIO_TAG_NAME.equals(loc);
071    }
072
073    private Attributes _processAttachment(Attributes attrs) throws SAXException
074    {
075        String contentId = _context.getObjectId()
076                .orElseThrow(() -> new SAXException("The object id is required in the data context in order to set the href"));
077
078        String dataPath = _context.getDataPath();
079        String fileName = attrs.getValue("href");
080
081        AttributesImpl newAttrs = new AttributesImpl();
082        _copyAttributes(attrs, newAttrs);
083
084        newAttrs.addCDATAAttribute("href", contentId + "@" + dataPath + ";" + fileName);
085
086        return newAttrs;
087    }
088
089    /**
090     * Copy the attributes except the fileref attribute
091     * @param attrs the attributes to copy.
092     * @param newAttrs the attributes to copy to.
093     */
094    private void _copyAttributes(Attributes attrs, AttributesImpl newAttrs)
095    {
096        for (int i = 0; i < attrs.getLength(); i++)
097        {
098            String name = attrs.getQName(i);
099
100            if (!"href".equals(name))
101            {
102                newAttrs.addAttribute(attrs.getURI(i), attrs.getLocalName(i), name, attrs.getType(i), attrs.getValue(i));
103            }
104        }
105    }
106}