    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
    020package org.apache.wiki.filters;
    021
    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.event.WikiEventListener;
    027import org.apache.wiki.event.WikiEventManager;
    028import org.apache.wiki.event.WikiPageEvent;
    029
    030import java.util.Properties;
    031
    032/**
    033  * Fires WikiPageEvents for page events.
    034  * <p>
    035  * Adding a PageEventFilter to the FilterManager will automatically attach an event delegate with the WikiEventManager to provide for
    036  * firing and listener management. All that remains is then adding the listener to the filter via the WikiEventManager. This is quite
    037  * simple:
    038  * </p>
    039  * <pre>
    040  *    PageEventFilter filter = new PageEventFilter();
    041  *    engine.getFilterManager().addPageFilter(filter,5000);
    042  *    // attach listener to filter
    043  *    WikiEventManager.addWikiEventListener(filter,listener);
    044  * </pre>
    045  * <p>
    046  * This class provides convenience methods for adding and removing WikiEventListeners.
    047  * </p>
    048  *
    049  * @see org.apache.wiki.event.WikiEventManager
    050  */
    051public class PageEventFilter extends BasePageFilter {
    052
    053    /**
    054     * Called whenever a new PageFilter is instantiated and reset.
    055     */
    056    @Override
    057    public void initialize( final Engine engine, final Properties properties ) throws FilterException {
    058        super.initialize( engine, properties );
    059    }
    060
    061    /**
    062     * This method is called whenever a page has been loaded from the provider, but not yet been sent through the TranslatorReader.
    063     * Note that you cannot do HTML translation here, because TranslatorReader is likely to escape it.
    064     *
    065     * @param wikiContext The current wikicontext.
    066     * @param content WikiMarkup.
    067     */
    068    @Override
    069    public String preTranslate( final Context wikiContext, final String content ) {
    070        fireEvent( WikiPageEvent.PRE_TRANSLATE, wikiContext );
    071        return content;
    072    }
    073
    074
    075    /**
    076     * This method is called after a page has been fed through the TranslatorReader, so anything you are seeing here is translated content.
    077     * If you want to do any of your own WikiMarkup2HTML translation, do it here.
    078     */
    079    @Override
    080    public String postTranslate( final Context wikiContext, final String htmlContent ) {
    081        fireEvent( WikiPageEvent.POST_TRANSLATE, wikiContext );
    082        return htmlContent;
    083    }
    084
    085
    086    /**
    087      * This method is called before the page has been saved to the PageProvider.
    088      */
    089    @Override
    090    public String preSave( final Context wikiContext, final String content ) {
    091        fireEvent( WikiPageEvent.PRE_SAVE, wikiContext );
    092        return content;
    093    }
    094
    095
    096    /**
    097      * This method is called after the page has been successfully saved. If the saving fails for any reason, then this method will not
    098      * be called.
    099      * <p>
    100      * Since the result is discarded from this method, this is only useful for things like counters, etc.
    101      */
    102    @Override
    103    public void postSave( final Context wikiContext, final String content ) {
    104        fireEvent( WikiPageEvent.POST_SAVE, wikiContext );
    105    }
    106
    107
    108    // events processing .......................................................
    109
    110    /**
    111     *  Registers a WikiEventListener with this instance. This is a convenience method.
    112     *
    113     * @param listener the event listener
    114     */
    115    public final synchronized void addWikiEventListener( final WikiEventListener listener ) {
    116        WikiEventManager.addWikiEventListener( this, listener );
    117    }
    118
    119    /**
    120     *  Un-registers a WikiEventListener with this instance. This is a convenience method.
    121     *
    122     * @param listener the event listener
    123     */
    124    public final synchronized void removeWikiEventListener( final WikiEventListener listener ) {
    125        WikiEventManager.removeWikiEventListener( this, listener );
    126    }
    127
    128    /**
    129     *  Fires a WikiPageEvent of the provided type and page name to all registered listeners. Only <tt>PAGE_LOCK</tt> and
    130     *  <tt>PAGE_UNLOCK</tt> event types will fire an event; other event types are ignored.
    131     *
    132     * @see org.apache.wiki.event.WikiPageEvent
    133     * @param type      the WikiPageEvent type to be fired.
    134     * @param context   the WikiContext of the event.
    135     */
    136    protected final void fireEvent( final int type, final Context context ) {
    137        if( WikiEventManager.isListening(this ) && WikiPageEvent.isValidType( type ) ) {
    138            final WikiPageEvent event = new WikiPageEvent( context.getEngine(), type, context.getPage().getName() );
    139            WikiEventManager.fireEvent(this, event );
    140        }
    141    }
    142
    143}