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 */
019package org.apache.wiki.filters;
020
021import org.apache.log4j.Logger;
022import org.apache.wiki.api.core.Context;
023import org.apache.wiki.api.core.Engine;
024import org.apache.wiki.api.exceptions.FilterException;
025import org.apache.wiki.api.filters.BasePageFilter;
026import org.apache.wiki.parser.CreoleToJSPWikiTranslator;
027
028import java.util.Properties;
029
030/**
031 * <p>Provides the Implementation for mixed mode creole: If you activate this filter, it will translate all markup that was saved as creole
032 * markup to JSPWiki markup. Therefore the files will be saved with mixed markup.
033 * <p>
034 * <b>WARNING</b>: There's no turning back after installing this filter. Since your wiki pages are saved in Creole markup you can
035 * not deactivate it afterwards.
036 * <p>
037 * <b>WARNING</b>: This feature is completely experimental, and is known to be broken.  Use at your own risk.
038 * <p>
039 * <b>WARNING</b>: The CreoleFilter feature is deprecated.  JSPWiki is likely to implement a non-mixed mode Creole at some point, since
040 * turning on Creole will make new pages obsolete.
041 *
042 * @see <a href="http://www.wikicreole.org/wiki/MixedMode">[[WikiCreole:MixedMode]]</a> 
043 */
044public class CreoleFilter extends BasePageFilter {
045    
046    private static final Logger log = Logger.getLogger(CreoleFilter.class);
047    
048    /**
049     *  {@inheritDoc}
050     */
051    @Override
052    public void initialize( final Engine engine, final Properties props ) throws FilterException {
053        super.initialize( engine, props );
054    }
055
056    /**
057     *  {@inheritDoc}
058     */
059    @Override
060    public String preSave( final Context wikiContext, final String content ) {
061        try {
062            final String username = wikiContext.getCurrentUser().getName();
063            final Properties prop = m_engine.getWikiProperties();
064            return new CreoleToJSPWikiTranslator().translateSignature(prop, content,username);
065        } catch( final Exception e ) {
066            log.error( e.getMessage(), e );
067            return e.getMessage();
068        }
069    }
070
071    /**
072     *  {@inheritDoc}
073     */
074    @Override
075    public String preTranslate( final Context wikiContext, final String content ) {
076        try {
077            final Properties prop = m_engine.getWikiProperties();
078            return new CreoleToJSPWikiTranslator().translate(prop ,content);
079        } catch( final Exception e ) {
080            log.error( e.getMessage(), e );
081            return content
082                   + "\n \n %%error \n"
083                   + "[CreoleFilterError]: This page was not translated by the CreoleFilter due to "
084                   + "the following error: " + e.getMessage() + "\n \n"
085                   + "%%\n \n";
086        }
087    }
088
089}