001    /* 
002        Licensed to the Apache Software Foundation (ASF) under one
003        or more contributor license agreements.  See the NOTICE file
004        distributed with this work for additional information
005        regarding copyright ownership.  The ASF licenses this file
006        to you under the Apache License, Version 2.0 (the
007        "License"); you may not use this file except in compliance
008        with the License.  You may obtain a copy of the License at
009    
010           http://www.apache.org/licenses/LICENSE-2.0
011    
012        Unless required by applicable law or agreed to in writing,
013        software distributed under the License is distributed on an
014        "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015        KIND, either express or implied.  See the License for the
016        specific language governing permissions and limitations
017        under the License.  
018     */
019    package org.apache.wiki.filters;
020    
021    import java.util.Properties;
022    
023    import org.apache.log4j.Logger;
024    import org.apache.wiki.WikiContext;
025    import org.apache.wiki.WikiEngine;
026    import org.apache.wiki.api.filters.BasicPageFilter;
027    import org.apache.wiki.api.exceptions.FilterException;
028    import org.apache.wiki.parser.CreoleToJSPWikiTranslator;
029    
030    /**
031     * <p>Provides the Implementation for mixed mode creole: If you activate
032     * this filter, it will translate all markup that was saved as creole
033     * markup to JSPWiki markup. Therefore the files will be saved 
034     * with mixed markup.
035     * <p>
036     * <b>WARNING</b>: There's no turning back after insalling this
037     * filter. Since your wiki pages are saved in Creole markup you can
038     * not deactivate it afterwards.
039     * <p>
040     * <b>WARNING</b>: This feature is completely experimental, and is known to be
041     * broken.  Use at your own risk.
042     * <p>
043     * <b>WARNING</b>: The CreoleFilter feature is deprecated.  JSPWiki is likely
044     * to implement a non-mixed mode Creole at some point, since turning on
045     * Creole will make new pages obsolete.
046     * 
047     * 
048     * @see <a href="http://www.wikicreole.org/wiki/MixedMode">[[WikiCreole:MixedMode]]</a> 
049     */
050    
051    public class CreoleFilter extends BasicPageFilter 
052    {
053        
054        private static final Logger log = Logger.getLogger(CreoleFilter.class);
055        
056        /**
057         *  {@inheritDoc}
058         */
059        public void initialize(WikiEngine engine, Properties props) throws FilterException 
060        {
061        }
062    
063        /**
064         *  {@inheritDoc}
065         */
066        public String preSave( WikiContext wikiContext, String content )
067        throws FilterException
068        {
069            try 
070            {
071                String username=wikiContext.getCurrentUser().getName();
072                Properties prop = wikiContext.getEngine().getWikiProperties();
073                return new CreoleToJSPWikiTranslator().translateSignature(prop, content,username);
074            }
075            catch(Exception e )
076            {
077                log.error( e.getMessage(), e );
078                return e.getMessage();
079            }
080        }
081    
082        /**
083         *  {@inheritDoc}
084         */
085        public String preTranslate(WikiContext wikiContext, String content)
086            throws FilterException 
087        {
088            try
089            {
090                Properties prop = wikiContext.getEngine().getWikiProperties();
091                return new CreoleToJSPWikiTranslator().translate(prop ,content);
092            } 
093            catch (Exception e) 
094            {
095                log.error( e.getMessage(), e );
096                return content
097                       + "\n \n %%error \n"
098                       + "[CreoleFilterError]: This page was not translated by the CreoleFilter due to "
099                       + "the following error: " + e.getMessage() + "\n \n"
100                       + "%%\n \n";
101            }
102        }
103    
104    }