001/*
002 *  Copyright 2011 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 */
016package org.ametys.cms.contenttype;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.avalon.framework.service.Serviceable;
023import org.xml.sax.ContentHandler;
024import org.xml.sax.ext.LexicalHandler;
025
026import org.ametys.cms.repository.Content;
027import org.ametys.plugins.repository.TraversableAmetysObject;
028
029/**
030 * HTML implementation of {@link RichTextUpdater} 
031 * Use the {@link HTMLUpdateHandlerExtensionPoint} extension points to update rich text fields based upon a serie of {@link HTMLUpdateHandler}
032 */
033public class HTMLRichTextUpdater implements RichTextUpdater, Serviceable
034{
035    /** Avalon role. */
036    public static final String ROLE = HTMLRichTextUpdater.class.getName();
037
038    private HTMLUpdateHandlerExtensionPoint _htmlUpdaterEP;
039    
040    @Override
041    public void service(ServiceManager smanager) throws ServiceException
042    {
043        _htmlUpdaterEP = (HTMLUpdateHandlerExtensionPoint) smanager.lookup(HTMLUpdateHandlerExtensionPoint.ROLE);
044    }
045    
046    @Override
047    public ContentHandler getContentHandler(ContentHandler ch, LexicalHandler lh, Map<String, Object> params)
048    {
049        ContentHandler contentHandler = ch;
050        LexicalHandler lexicalHandler = lh;
051        
052        for (String handlerRole : _htmlUpdaterEP.getExtensionsIds())
053        {
054            HTMLUpdateHandler handler = _htmlUpdaterEP.getExtension(handlerRole);
055            
056            handler.setCreatedContent((Content) params.get("createdContent"));
057            handler.setInitialContent((Content) params.get("initialContent"));
058            
059            TraversableAmetysObject createdAO = (TraversableAmetysObject) params.get("createdAO");
060            if (createdAO != null)
061            {
062                handler.setCreatedObject((TraversableAmetysObject) params.get("createdAO"));
063            }
064            
065            TraversableAmetysObject initialAO = (TraversableAmetysObject) params.get("initialAO");
066            if (initialAO != null)
067            {
068                handler.setInitialObject((TraversableAmetysObject) params.get("initialAO"));
069            }
070            
071            handler.setContentHandler(contentHandler);
072            handler.setLexicalHandler(lexicalHandler);
073            
074            contentHandler = handler;
075            lexicalHandler = handler;
076        }
077
078        return contentHandler;
079    }
080}