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