1 31 package org.blojsom.plugin.technorati; 32 33 import org.blojsom.blog.Blog; 34 import org.blojsom.blog.Entry; 35 import org.blojsom.event.Event; 36 import org.blojsom.event.EventBroadcaster; 37 import org.blojsom.event.Listener; 38 import org.blojsom.plugin.PluginException; 39 import org.blojsom.plugin.admin.event.ProcessEntryEvent; 40 import org.blojsom.plugin.velocity.StandaloneVelocityPlugin; 41 import org.blojsom.util.BlojsomUtils; 42 43 import javax.servlet.http.HttpServletRequest ; 44 import javax.servlet.http.HttpServletResponse ; 45 import java.text.MessageFormat ; 46 import java.util.ArrayList ; 47 import java.util.HashMap ; 48 import java.util.Map ; 49 import java.util.TreeMap ; 50 51 58 public class TechnoratiTagsPlugin extends StandaloneVelocityPlugin implements Listener { 59 60 private static final String TECHNORATI_TAGS_TEMPLATE = "org/blojsom/plugin/technorati/templates/admin-technorati-tags.vm"; 61 private static final String TECHNORATI_TAG_LINK_TEMPLATE = "org/blojsom/plugin/technorati/templates/technorati-tag-link.vm"; 62 private static final String TECHNORATI_TAGS = "TECHNORATI_TAGS"; 63 private static final String TECHNORATI_TAG_LINKS = "TECHNORATI_TAG_LINKS"; 64 65 private EventBroadcaster _eventBroadcaster; 66 67 public static final String METADATA_TECHNORATI_TAGS = "technorati-tags"; 68 69 72 public TechnoratiTagsPlugin() { 73 } 74 75 80 public void setEventBroadcaster(EventBroadcaster eventBroadcaster) { 81 _eventBroadcaster = eventBroadcaster; 82 } 83 84 90 public void init() throws PluginException { 91 super.init(); 92 93 _eventBroadcaster.addListener(this); 94 } 95 96 107 public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException { 108 for (int i = 0; i < entries.length; i++) { 109 Entry entry = entries[i]; 110 Map entryMetaData = entry.getMetaData(); 111 112 if (BlojsomUtils.checkMapForKey(entryMetaData, METADATA_TECHNORATI_TAGS)) { 113 String [] tags = BlojsomUtils.parseOnlyCommaList((String ) entryMetaData.get(METADATA_TECHNORATI_TAGS)); 114 if (tags != null && tags.length > 0) { 115 ArrayList tagLinks = new ArrayList (tags.length); 116 String tagLinkTemplate = mergeTemplate(TECHNORATI_TAG_LINK_TEMPLATE, blog, new HashMap ()); 117 for (int j = 0; j < tags.length; j++) { 118 String tag = tags[j].trim(); 119 tag = BlojsomUtils.escapeStringSimple(tag); 120 121 tagLinks.add(MessageFormat.format(tagLinkTemplate, new String []{tag})); 122 } 123 124 entryMetaData.put(TECHNORATI_TAG_LINKS, tagLinks.toArray(new String [tagLinks.size()])); 125 } 126 } 127 } 128 129 return entries; 130 } 131 132 138 public void cleanup() throws PluginException { 139 } 140 141 147 public void destroy() throws PluginException { 148 } 149 150 155 public void handleEvent(Event event) { 156 } 157 158 163 public void processEvent(Event event) { 164 if (event instanceof ProcessEntryEvent) { 165 ProcessEntryEvent processBlogEntryEvent = (ProcessEntryEvent) event; 166 167 String technoratiTags = BlojsomUtils.getRequestValue(METADATA_TECHNORATI_TAGS, processBlogEntryEvent.getHttpServletRequest()); 168 if (processBlogEntryEvent.getEntry() != null) { 169 String savedTechnoratiTags = (String ) processBlogEntryEvent.getEntry().getMetaData().get(METADATA_TECHNORATI_TAGS); 170 if (savedTechnoratiTags != null) { 171 if (technoratiTags == null) { 172 technoratiTags = savedTechnoratiTags; 174 } else if ("".equals(technoratiTags.trim())) { 176 technoratiTags = ""; 177 } 178 } 179 180 processBlogEntryEvent.getEntry().getMetaData().put(METADATA_TECHNORATI_TAGS, technoratiTags); 181 } 182 183 Map context = processBlogEntryEvent.getContext(); 184 185 Map templateAdditions = (Map ) processBlogEntryEvent.getContext().get("BLOJSOM_TEMPLATE_ADDITIONS"); 186 if (templateAdditions == null) { 187 templateAdditions = new TreeMap (); 188 } 189 190 templateAdditions.put(getClass().getName(), "#parse('" + TECHNORATI_TAGS_TEMPLATE + "')"); 191 processBlogEntryEvent.getContext().put("BLOJSOM_TEMPLATE_ADDITIONS", templateAdditions); 192 193 context.put(TECHNORATI_TAGS, technoratiTags); 194 195 if (processBlogEntryEvent.getEntry() != null) { 196 processBlogEntryEvent.getEntry().getMetaData().put(METADATA_TECHNORATI_TAGS, technoratiTags); 197 } 198 199 processBlogEntryEvent.getContext().put(TECHNORATI_TAGS, technoratiTags); 200 } 201 } 202 } | Popular Tags |