001/*
002 *  Copyright 2010 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 org.apache.avalon.framework.context.Context;
020import org.apache.avalon.framework.context.ContextException;
021import org.apache.avalon.framework.context.Contextualizable;
022import org.apache.avalon.framework.logger.AbstractLogEnabled;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026import org.xml.sax.Attributes;
027import org.xml.sax.ContentHandler;
028import org.xml.sax.Locator;
029import org.xml.sax.SAXException;
030import org.xml.sax.ext.LexicalHandler;
031
032/**
033 * This abstract class is for component that wants to be in the saving pipeline.
034 * As a pipeline you receive events (from the ContentHandler and LexicalHandler interfaces) 
035 * you may transform it and write it to the given contentHandler (setContentHandler) and 
036 * given lexicalHandler (setLexicalHandler)
037 * You receive the html extracted from a htmleditor and you may operate some changes. 
038 */
039public abstract class AbstractHTMLEditionHandler extends AbstractLogEnabled implements ContentHandler, LexicalHandler, Serviceable, Contextualizable
040{
041    /** The content handler where to write */
042    protected ContentHandler _contentHandler;
043    /** The lexical handler where to write */
044    protected LexicalHandler _lexicalHandler;
045    /** The avalon service manager */
046    protected ServiceManager _manager;
047    /** The avalon context */
048    protected Context _context;
049    
050    @Override
051    public void contextualize(Context context) throws ContextException
052    {
053        _context = context;
054    }
055    
056    @Override
057    public void service(ServiceManager manager) throws ServiceException
058    {
059        _manager = manager;
060    }
061    
062    /**
063     * Set the target content handler
064     * @param contentHandler the target content handler
065     */
066    public void setContentHandler(ContentHandler contentHandler)
067    {
068        _contentHandler = contentHandler;
069    }
070    
071    /**
072     * Set the target lexical handler
073     * @param lexicalHandler the target lexical handler
074     */
075    public void setLexicalHandler(LexicalHandler lexicalHandler)
076    {
077        _lexicalHandler = lexicalHandler;
078    }
079    @Override
080    public void startDocument() throws SAXException
081    {
082        _contentHandler.startDocument();
083    }
084    @Override
085    public void endDocument() throws SAXException
086    {
087        _contentHandler.endDocument();
088    }
089
090    @Override
091    public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException
092    {
093        _contentHandler.startElement(uri, localName, qName, atts);
094    }
095    @Override
096    public void endElement(String uri, String localName, String qName) throws SAXException
097    {
098        _contentHandler.endElement(uri, localName, qName);
099    }
100    
101    @Override
102    public void characters(char[] ch, int start, int length) throws SAXException
103    {
104        _contentHandler.characters(ch, start, length);
105    }
106    @Override
107    public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException
108    {
109        _contentHandler.ignorableWhitespace(ch, start, length);
110    }
111    
112    @Override
113    public void processingInstruction(String target, String data) throws SAXException
114    {
115        _contentHandler.processingInstruction(target, data);
116    }
117    @Override
118    public void setDocumentLocator(Locator locator)
119    {
120        _contentHandler.setDocumentLocator(locator);
121    }
122    @Override
123    public void skippedEntity(String name) throws SAXException
124    {
125        _contentHandler.skippedEntity(name);
126    }
127    
128    @Override
129    public void startPrefixMapping(String prefix, String uri) throws SAXException
130    {
131        _contentHandler.startPrefixMapping(prefix, uri);
132    }
133    @Override
134    public void endPrefixMapping(String prefix) throws SAXException
135    {
136        _contentHandler.endPrefixMapping(prefix);
137    }
138    
139    @Override
140    public void startEntity(String name) throws SAXException
141    {
142        _lexicalHandler.startEntity(name);
143    }
144    @Override
145    public void endEntity(String name) throws SAXException
146    {
147        _lexicalHandler.endEntity(name);
148    }
149
150    @Override
151    public void startCDATA() throws SAXException
152    {
153        _lexicalHandler.startCDATA();
154    }
155    @Override
156    public void endCDATA() throws SAXException
157    {
158        _lexicalHandler.endCDATA();
159    }
160
161    @Override
162    public void startDTD(String name, String publicId, String systemId) throws SAXException
163    {
164        _lexicalHandler.startDTD(name, publicId, systemId);
165    }
166    @Override
167    public void endDTD() throws SAXException
168    {
169        _lexicalHandler.endCDATA();
170    }
171
172    @Override
173    public void comment(char[] ch, int start, int length) throws SAXException
174    {
175        _lexicalHandler.comment(ch, start, length);
176    }
177}