001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2001-2006 Janne Jalkanen (Janne.Jalkanen@iki.fi)
005:
006: This program is free software; you can redistribute it and/or modify
007: it under the terms of the GNU Lesser General Public License as published by
008: the Free Software Foundation; either version 2.1 of the License, or
009: (at your option) any later version.
010:
011: This program is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public License
017: along with this program; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: package com.ecyrd.jspwiki.event;
022:
023: import com.ecyrd.jspwiki.filters.BasicPageFilter;
024: import com.ecyrd.jspwiki.filters.FilterException;
025:
026: import com.ecyrd.jspwiki.WikiContext;
027: import java.util.Properties;
028:
029: /**
030: * Fires WikiPageEvents for page events.
031: * <p>
032: * Adding a PageEventFilter to the FilterManager will automatically
033: * attach an event delegate with the WikiEventManager to provide for
034: * firing and listener management. All that remains is then adding
035: * the listener to the filter via the WikiEventManager. This is quite
036: * simple:
037: * </p>
038: * <pre>
039: * PageEventFilter filter = new PageEventFilter();
040: * engine.getFilterManager().addPageFilter(filter,5000);
041: * // attach listener to filter
042: * WikiEventManager.addWikiEventListener(filter,listener);
043: * </pre>
044: * <p>
045: * This class provides convenience methods for adding and removing
046: * WikiEventListeners.
047: * </p>
048: *
049: * @see com.ecyrd.jspwiki.event.WikiEventManager
050: * @author Murray Altheim
051: */
052: public class PageEventFilter extends BasicPageFilter {
053:
054: /**
055: * Called whenever a new PageFilter is instantiated and reset.
056: */
057: public void initialize(Properties properties)
058: throws FilterException {
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: fireEvent(WikiPageEvent.PRE_TRANSLATE, wikiContext);
073: return content;
074: }
075:
076: /**
077: * This method is called after a page has been fed through the TranslatorReader,
078: * so anything you are seeing here is translated content. If you want to
079: * do any of your own WikiMarkup2HTML translation, do it here.
080: */
081: public String postTranslate(WikiContext wikiContext,
082: String htmlContent) throws FilterException {
083: fireEvent(WikiPageEvent.POST_TRANSLATE, wikiContext);
084: return htmlContent;
085: }
086:
087: /**
088: * This method is called before the page has been saved to the PageProvider.
089: */
090: public String preSave(WikiContext wikiContext, String content)
091: throws FilterException {
092: fireEvent(WikiPageEvent.PRE_SAVE, wikiContext);
093: return content;
094: }
095:
096: /**
097: * This method is called after the page has been successfully saved.
098: * If the saving fails for any reason, then this method will not
099: * be called.
100: * <p>
101: * Since the result is discarded from this method, this is only useful
102: * for things like counters, etc.
103: */
104: public void postSave(WikiContext wikiContext, String content)
105: throws FilterException {
106: fireEvent(WikiPageEvent.POST_SAVE, wikiContext);
107: }
108:
109: // events processing .......................................................
110:
111: /**
112: * Registers a WikiEventListener with this instance.
113: * This is a convenience method.
114: *
115: * @param listener the event listener
116: */
117: public final synchronized void addWikiEventListener(
118: WikiEventListener listener) {
119: WikiEventManager.addWikiEventListener(this , listener);
120: }
121:
122: /**
123: * Un-registers a WikiEventListener with this instance.
124: * This is a convenience method.
125: *
126: * @param listener the event listener
127: */
128: public final synchronized void removeWikiEventListener(
129: WikiEventListener listener) {
130: WikiEventManager.removeWikiEventListener(this , listener);
131: }
132:
133: /**
134: * Fires a WikiPageEvent of the provided type and page name
135: * to all registered listeners. Only <tt>PAGE_LOCK</tt> and
136: * <tt>PAGE_UNLOCK</tt> event types will fire an event; other
137: * event types are ignored.
138: *
139: * @see com.ecyrd.jspwiki.event.WikiPageEvent
140: * @param type the WikiPageEvent type to be fired.
141: * @param context the WikiContext of the event.
142: */
143: protected final void fireEvent(int type, WikiContext context) {
144: if (WikiEventManager.isListening(this )
145: && WikiPageEvent.isValidType(type)) {
146: WikiPageEvent event = new WikiPageEvent(
147: context.getEngine(), type, context.getPage()
148: .getName());
149: WikiEventManager.fireEvent(this , event);
150: }
151: }
152:
153: } // end com.ecyrd.jspwiki.event.PageEventFilter
|