001: /**
002: * Copyright (c) 2003-2007, David A. Czarnecki
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * Redistributions of source code must retain the above copyright notice, this list of conditions and the
009: * following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
011: * following disclaimer in the documentation and/or other materials provided with the distribution.
012: * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
013: * endorse or promote products derived from this software without specific prior written permission.
014: * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
015: * without prior written permission of David A. Czarnecki.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
018: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
019: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
021: * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
022: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
025: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
029: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030: */package org.blojsom.plugin.markup;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.blojsom.blog.Entry;
035: import org.blojsom.blog.Blog;
036: import org.blojsom.event.EventBroadcaster;
037: import org.blojsom.event.Listener;
038: import org.blojsom.event.Event;
039: import org.blojsom.plugin.Plugin;
040: import org.blojsom.plugin.PluginException;
041: import org.blojsom.plugin.admin.event.ProcessEntryEvent;
042: import org.blojsom.util.BlojsomUtils;
043:
044: import javax.servlet.ServletConfig;
045: import javax.servlet.http.HttpServletRequest;
046: import javax.servlet.http.HttpServletResponse;
047: import java.util.Collections;
048: import java.util.Iterator;
049: import java.util.Map;
050: import java.util.TreeMap;
051:
052: /**
053: * Markup selection plugin allows an individual to select a markup filter to apply
054: * to their blog entry.
055: *
056: * @author David Czarnecki
057: * @version $Id: MarkupSelectionPlugin.java,v 1.2 2007/01/17 02:35:11 czarneckid Exp $
058: * @since blojsom 3.0
059: */
060: public class MarkupSelectionPlugin implements Plugin, Listener {
061:
062: private Log _logger = LogFactory
063: .getLog(MarkupSelectionPlugin.class);
064:
065: private static final String PLUGIN_MARKUP_SELECTION_IP = "plugin-markup-selection";
066: private static final String BLOJSOM_PLUGIN_MARKUP_SELECTIONS = "BLOJSOM_PLUGIN_MARKUP_SELECTIONS";
067: private static final String MARKUP_SELECTION_TEMPLATE = "org/blojsom/plugin/markup/templates/admin-markup-selection-attachment.vm";
068: private static final String MARKUP_SELECTIONS = "markup-selections";
069:
070: private EventBroadcaster _eventBroadcaster;
071: private ServletConfig _servletConfig;
072:
073: private Map _markupSelections;
074:
075: /**
076: * Create a new instance of the markup selection plugin
077: */
078: public MarkupSelectionPlugin() {
079: }
080:
081: /**
082: * Set the {@link EventBroadcaster} event broadcaster
083: *
084: * @param eventBroadcaster {@link EventBroadcaster}
085: */
086: public void setEventBroadcaster(EventBroadcaster eventBroadcaster) {
087: _eventBroadcaster = eventBroadcaster;
088: }
089:
090: /**
091: * Set the {@link ServletConfig} for the fetcher to grab initialization parameters
092: *
093: * @param servletConfig {@link ServletConfig}
094: */
095: public void setServletConfig(ServletConfig servletConfig) {
096: _servletConfig = servletConfig;
097: }
098:
099: /**
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 markupSelection = _servletConfig
107: .getInitParameter(PLUGIN_MARKUP_SELECTION_IP);
108: _markupSelections = new TreeMap();
109: if (!BlojsomUtils.checkNullOrBlank(markupSelection)) {
110: String[] markupTypes = BlojsomUtils
111: .parseCommaList(markupSelection);
112: for (int i = 0; i < markupTypes.length; i++) {
113: String markupType = markupTypes[i];
114: String[] markupNameAndKey = BlojsomUtils
115: .parseDelimitedList(markupType, ":");
116: if (markupNameAndKey != null
117: && markupNameAndKey.length == 2) {
118: _markupSelections.put(markupNameAndKey[0],
119: markupNameAndKey[1]);
120: if (_logger.isDebugEnabled()) {
121: _logger.debug("Added markup type and key: "
122: + markupNameAndKey[0] + ":"
123: + markupNameAndKey[1]);
124: }
125: }
126: }
127: }
128:
129: _eventBroadcaster.addListener(this );
130: }
131:
132: /**
133: * Process the blog entries
134: *
135: * @param httpServletRequest Request
136: * @param httpServletResponse Response
137: * @param blog {@link org.blojsom.blog.Blog} instance
138: * @param context Context
139: * @param entries Blog entries retrieved for the particular request
140: * @return Modified set of blog entries
141: * @throws PluginException If there is an error processing the blog entries
142: */
143: public Entry[] process(HttpServletRequest httpServletRequest,
144: HttpServletResponse httpServletResponse, Blog blog,
145: Map context, Entry[] entries) throws PluginException {
146: return entries;
147: }
148:
149: /**
150: * Perform any cleanup for the plugin. Called after {@link #process}.
151: *
152: * @throws org.blojsom.plugin.PluginException
153: * If there is an error performing cleanup for this plugin
154: */
155: public void cleanup() throws PluginException {
156: }
157:
158: /**
159: * Called when BlojsomServlet is taken out of service
160: *
161: * @throws org.blojsom.plugin.PluginException
162: * If there is an error in finalizing this plugin
163: */
164: public void destroy() throws PluginException {
165: }
166:
167: /**
168: * Handle an event broadcast from another component
169: *
170: * @param event {@link org.blojsom.event.Event} to be handled
171: */
172: public void handleEvent(Event event) {
173: }
174:
175: /**
176: * Process an event from another component
177: *
178: * @param event {@link org.blojsom.event.Event} to be handled
179: */
180: public void processEvent(Event event) {
181: if (event instanceof ProcessEntryEvent) {
182: _logger.debug("Handling process blog entry event");
183:
184: if (!_markupSelections.isEmpty()) {
185: ProcessEntryEvent processEntryEvent = (ProcessEntryEvent) event;
186: Map templateAdditions = (Map) processEntryEvent
187: .getContext().get("BLOJSOM_TEMPLATE_ADDITIONS");
188: if (templateAdditions == null) {
189: templateAdditions = new TreeMap();
190: }
191:
192: templateAdditions.put(getClass().getName(), "#parse('"
193: + MARKUP_SELECTION_TEMPLATE + "')");
194: processEntryEvent.getContext()
195: .put("BLOJSOM_TEMPLATE_ADDITIONS",
196: templateAdditions);
197:
198: processEntryEvent.getContext().put(
199: BLOJSOM_PLUGIN_MARKUP_SELECTIONS,
200: Collections.unmodifiableMap(_markupSelections));
201:
202: String[] markupSelections = processEntryEvent
203: .getHttpServletRequest().getParameterValues(
204: MARKUP_SELECTIONS);
205: Entry entry = processEntryEvent.getEntry();
206:
207: if (markupSelections != null
208: && markupSelections.length > 0) {
209: // Remove the markup selections if the user selections the blank option
210: if (markupSelections.length == 1
211: && "".equals(markupSelections[0])) {
212: Iterator markupSelectionsIterator = _markupSelections
213: .values().iterator();
214: while (markupSelectionsIterator.hasNext()) {
215: entry.getMetaData().remove(
216: markupSelectionsIterator.next()
217: .toString());
218: }
219: } else {
220: // Otherwise, set the new markup selections
221: for (int i = 0; i < markupSelections.length; i++) {
222: String markupSelection = markupSelections[i];
223: entry.getMetaData().put(markupSelection,
224: Boolean.TRUE.toString());
225: }
226: }
227: }
228: } else {
229: if (_logger.isDebugEnabled()) {
230: _logger.debug("No markup selections available");
231: }
232: }
233: }
234: }
235: }
|