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