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 LocalMediaObjectHandler extends ContentHandlerProxy 032{ 033 private static final String __ATTACHMENT_IMAGE_TAG_NAME = "imagedata"; 034 private static final String __ATTACHMENT_VIDEO_TAG_NAME = "videodata"; 035 private static final String __ATTACHMENT_AUDIO_TAG_NAME = "audiodata"; 036 private static final String __ATTACHMENT_TYPE_ATTRIBUTE_NAME = "type"; 037 private static final String __ATTACHMENT_TYPE_ATTRIBUTE_LOCAL_VALUE = "local"; 038 039 private DataContext _context; 040 041 /** 042 * Constructor 043 * @param contentHandler the contentHandler to pass SAX events to 044 * @param context the context of the rich text to SAX 045 */ 046 public LocalMediaObjectHandler(ContentHandler contentHandler, DataContext context) 047 { 048 super(contentHandler); 049 _context = context; 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 super.startElement(uri, loc, raw, attrs); 066 } 067 } 068 069 private boolean _isAttachment(String loc) 070 { 071 return __ATTACHMENT_IMAGE_TAG_NAME.equals(loc) || __ATTACHMENT_VIDEO_TAG_NAME.equals(loc) || __ATTACHMENT_AUDIO_TAG_NAME.equals(loc); 072 } 073 074 private Attributes _processAttachment(Attributes attrs) throws SAXException 075 { 076 String contentId = _context.getObjectId() 077 .orElseThrow(() -> new SAXException("The object id is required in the data context in order to set the fileref")); 078 079 String dataPath = _context.getDataPath(); 080 String fileName = attrs.getValue("fileref"); 081 082 AttributesImpl newAttrs = new AttributesImpl(); 083 _copyAttributes(attrs, newAttrs); 084 085 newAttrs.addCDATAAttribute("fileref", contentId + "@" + dataPath + ";" + fileName); 086 087 return newAttrs; 088 } 089 090 /** 091 * Copy the attributes except the fileref attribute 092 * @param attrs the attributes to copy. 093 * @param newAttrs the attributes to copy to. 094 */ 095 private void _copyAttributes(Attributes attrs, AttributesImpl newAttrs) 096 { 097 for (int i = 0; i < attrs.getLength(); i++) 098 { 099 String name = attrs.getQName(i); 100 101 if (!"fileref".equals(name)) 102 { 103 newAttrs.addAttribute(attrs.getURI(i), attrs.getLocalName(i), name, attrs.getType(i), attrs.getValue(i)); 104 } 105 } 106 } 107}