KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > plugin > markup > MarkupSelectionPlugin


1 /**
2  * Copyright (c) 2003-2006, David A. Czarnecki
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and the
9  * following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11  * following disclaimer in the documentation and/or other materials provided with the distribution.
12  * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
13  * endorse or promote products derived from this software without specific prior written permission.
14  * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
15  * without prior written permission of David A. Czarnecki.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */

31 package org.blojsom.plugin.markup;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.blojsom.blog.Entry;
36 import org.blojsom.blog.Blog;
37 import org.blojsom.event.EventBroadcaster;
38 import org.blojsom.event.Listener;
39 import org.blojsom.event.Event;
40 import org.blojsom.plugin.Plugin;
41 import org.blojsom.plugin.PluginException;
42 import org.blojsom.plugin.admin.event.ProcessEntryEvent;
43 import org.blojsom.util.BlojsomUtils;
44
45 import javax.servlet.ServletConfig JavaDoc;
46 import javax.servlet.http.HttpServletRequest JavaDoc;
47 import javax.servlet.http.HttpServletResponse JavaDoc;
48 import java.util.Collections JavaDoc;
49 import java.util.Iterator JavaDoc;
50 import java.util.Map JavaDoc;
51 import java.util.TreeMap JavaDoc;
52
53 /**
54  * Markup selection plugin allows an individual to select a markup filter to apply
55  * to their blog entry.
56  *
57  * @author David Czarnecki
58  * @version $Id: MarkupSelectionPlugin.java,v 1.1 2006/03/26 18:57:05 czarneckid Exp $
59  * @since blojsom 3.0
60  */

61 public class MarkupSelectionPlugin implements Plugin, Listener {
62
63     private Log _logger = LogFactory.getLog(MarkupSelectionPlugin.class);
64
65     private static final String JavaDoc PLUGIN_MARKUP_SELECTION_IP = "plugin-markup-selection";
66     private static final String JavaDoc BLOJSOM_PLUGIN_MARKUP_SELECTIONS = "BLOJSOM_PLUGIN_MARKUP_SELECTIONS";
67     private static final String JavaDoc MARKUP_SELECTION_TEMPLATE = "org/blojsom/plugin/markup/templates/admin-markup-selection-attachment.vm";
68     private static final String JavaDoc MARKUP_SELECTIONS = "markup-selections";
69
70     private EventBroadcaster _eventBroadcaster;
71     private ServletConfig JavaDoc _servletConfig;
72
73     private Map JavaDoc _markupSelections;
74
75     /**
76      * Create a new instance of the markup selection plugin
77      */

78     public MarkupSelectionPlugin() {
79     }
80
81     /**
82      * Set the {@link EventBroadcaster} event broadcaster
83      *
84      * @param eventBroadcaster {@link EventBroadcaster}
85      */

86     public void setEventBroadcaster(EventBroadcaster eventBroadcaster) {
87         _eventBroadcaster = eventBroadcaster;
88     }
89
90     /**
91      * Set the {@link ServletConfig} for the fetcher to grab initialization parameters
92      *
93      * @param servletConfig {@link ServletConfig}
94      */

95     public void setServletConfig(ServletConfig JavaDoc servletConfig) {
96         _servletConfig = servletConfig;
97     }
98
99     /**
100      * Initialize this plugin. This method only called when the plugin is instantiated.
101      *
102      * @throws org.blojsom.plugin.PluginException
103      * If there is an error initializing the plugin
104      */

105     public void init() throws PluginException {
106         String JavaDoc markupSelection = _servletConfig.getInitParameter(PLUGIN_MARKUP_SELECTION_IP);
107         _markupSelections = new TreeMap JavaDoc();
108         if (!BlojsomUtils.checkNullOrBlank(markupSelection)) {
109             String JavaDoc[] markupTypes = BlojsomUtils.parseCommaList(markupSelection);
110             for (int i = 0; i < markupTypes.length; i++) {
111                 String JavaDoc markupType = markupTypes[i];
112                 String JavaDoc[] markupNameAndKey = BlojsomUtils.parseDelimitedList(markupType, ":");
113                 if (markupNameAndKey != null && markupNameAndKey.length == 2) {
114                     _markupSelections.put(markupNameAndKey[0], markupNameAndKey[1]);
115                     if (_logger.isDebugEnabled()) {
116                         _logger.debug("Added markup type and key: " + markupNameAndKey[0] + ":" + markupNameAndKey[1]);
117                     }
118                 }
119             }
120         }
121
122         _eventBroadcaster.addListener(this);
123     }
124
125     /**
126      * Process the blog entries
127      *
128      * @param httpServletRequest Request
129      * @param httpServletResponse Response
130      * @param blog {@link org.blojsom.blog.Blog} instance
131      * @param context Context
132      * @param entries Blog entries retrieved for the particular request
133      * @return Modified set of blog entries
134      * @throws PluginException If there is an error processing the blog entries
135      */

136     public Entry[] process(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, Blog blog, Map JavaDoc context, Entry[] entries) throws PluginException {
137         return entries;
138     }
139
140     /**
141      * Perform any cleanup for the plugin. Called after {@link #process}.
142      *
143      * @throws org.blojsom.plugin.PluginException
144      * If there is an error performing cleanup for this plugin
145      */

146     public void cleanup() throws PluginException {
147     }
148
149     /**
150      * Called when BlojsomServlet is taken out of service
151      *
152      * @throws org.blojsom.plugin.PluginException
153      * If there is an error in finalizing this plugin
154      */

155     public void destroy() throws PluginException {
156     }
157
158     /**
159      * Handle an event broadcast from another component
160      *
161      * @param event {@link org.blojsom.event.Event} to be handled
162      */

163     public void handleEvent(Event event) {
164     }
165
166     /**
167      * Process an event from another component
168      *
169      * @param event {@link org.blojsom.event.Event} to be handled
170      */

171     public void processEvent(Event event) {
172         if (event instanceof ProcessEntryEvent) {
173             _logger.debug("Handling process blog entry event");
174
175             if (!_markupSelections.isEmpty()) {
176                 ProcessEntryEvent processEntryEvent = (ProcessEntryEvent) event;
177                 Map JavaDoc templateAdditions = (Map JavaDoc) processEntryEvent.getContext().get("BLOJSOM_TEMPLATE_ADDITIONS");
178                 if (templateAdditions == null) {
179                     templateAdditions = new TreeMap JavaDoc();
180                 }
181
182                 templateAdditions.put(getClass().getName(), "#parse('" + MARKUP_SELECTION_TEMPLATE + "')");
183                 processEntryEvent.getContext().put("BLOJSOM_TEMPLATE_ADDITIONS", templateAdditions);
184
185                 processEntryEvent.getContext().put(BLOJSOM_PLUGIN_MARKUP_SELECTIONS, Collections.unmodifiableMap(_markupSelections));
186
187                 String JavaDoc[] markupSelections = processEntryEvent.getHttpServletRequest().getParameterValues(MARKUP_SELECTIONS);
188                 Entry entry = processEntryEvent.getEntry();
189
190                 if (markupSelections != null && markupSelections.length > 0) {
191                     // Remove the markup selections if the user selections the blank option
192
if (markupSelections.length == 1 && "".equals(markupSelections[0])) {
193                         Iterator JavaDoc markupSelectionsIterator = _markupSelections.values().iterator();
194                         while (markupSelectionsIterator.hasNext()) {
195                             entry.getMetaData().remove(markupSelectionsIterator.next().toString());
196                         }
197                     } else {
198                         // Otherwise, set the new markup selections
199
for (int i = 0; i < markupSelections.length; i++) {
200                             String JavaDoc markupSelection = markupSelections[i];
201                             entry.getMetaData().put(markupSelection, Boolean.TRUE.toString());
202                         }
203                     }
204                 }
205             } else {
206                 if (_logger.isDebugEnabled()) {
207                     _logger.debug("No markup selections available");
208                 }
209             }
210         }
211     }
212 }
Popular Tags