1 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 ; 46 import javax.servlet.http.HttpServletRequest ; 47 import javax.servlet.http.HttpServletResponse ; 48 import java.util.Collections ; 49 import java.util.Iterator ; 50 import java.util.Map ; 51 import java.util.TreeMap ; 52 53 61 public class MarkupSelectionPlugin implements Plugin, Listener { 62 63 private Log _logger = LogFactory.getLog(MarkupSelectionPlugin.class); 64 65 private static final String PLUGIN_MARKUP_SELECTION_IP = "plugin-markup-selection"; 66 private static final String BLOJSOM_PLUGIN_MARKUP_SELECTIONS = "BLOJSOM_PLUGIN_MARKUP_SELECTIONS"; 67 private static final String MARKUP_SELECTION_TEMPLATE = "org/blojsom/plugin/markup/templates/admin-markup-selection-attachment.vm"; 68 private static final String MARKUP_SELECTIONS = "markup-selections"; 69 70 private EventBroadcaster _eventBroadcaster; 71 private ServletConfig _servletConfig; 72 73 private Map _markupSelections; 74 75 78 public MarkupSelectionPlugin() { 79 } 80 81 86 public void setEventBroadcaster(EventBroadcaster eventBroadcaster) { 87 _eventBroadcaster = eventBroadcaster; 88 } 89 90 95 public void setServletConfig(ServletConfig servletConfig) { 96 _servletConfig = servletConfig; 97 } 98 99 105 public void init() throws PluginException { 106 String markupSelection = _servletConfig.getInitParameter(PLUGIN_MARKUP_SELECTION_IP); 107 _markupSelections = new TreeMap (); 108 if (!BlojsomUtils.checkNullOrBlank(markupSelection)) { 109 String [] markupTypes = BlojsomUtils.parseCommaList(markupSelection); 110 for (int i = 0; i < markupTypes.length; i++) { 111 String markupType = markupTypes[i]; 112 String [] 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 136 public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException { 137 return entries; 138 } 139 140 146 public void cleanup() throws PluginException { 147 } 148 149 155 public void destroy() throws PluginException { 156 } 157 158 163 public void handleEvent(Event event) { 164 } 165 166 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 templateAdditions = (Map ) processEntryEvent.getContext().get("BLOJSOM_TEMPLATE_ADDITIONS"); 178 if (templateAdditions == null) { 179 templateAdditions = new TreeMap (); 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 [] markupSelections = processEntryEvent.getHttpServletRequest().getParameterValues(MARKUP_SELECTIONS); 188 Entry entry = processEntryEvent.getEntry(); 189 190 if (markupSelections != null && markupSelections.length > 0) { 191 if (markupSelections.length == 1 && "".equals(markupSelections[0])) { 193 Iterator markupSelectionsIterator = _markupSelections.values().iterator(); 194 while (markupSelectionsIterator.hasNext()) { 195 entry.getMetaData().remove(markupSelectionsIterator.next().toString()); 196 } 197 } else { 198 for (int i = 0; i < markupSelections.length; i++) { 200 String 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 |