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