KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > plugin > technorati > TechnoratiTagsPlugin


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.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 JavaDoc;
44 import javax.servlet.http.HttpServletResponse JavaDoc;
45 import java.text.MessageFormat JavaDoc;
46 import java.util.ArrayList JavaDoc;
47 import java.util.HashMap JavaDoc;
48 import java.util.Map JavaDoc;
49 import java.util.TreeMap JavaDoc;
50
51 /**
52  * Technorati tags plugin
53  *
54  * @author David Czarnecki
55  * @version $Id: TechnoratiTagsPlugin.java,v 1.2 2006/08/30 13:37:57 czarneckid Exp $
56  * @since blojsom 3.0
57  */

58 public class TechnoratiTagsPlugin extends StandaloneVelocityPlugin implements Listener {
59
60     private static final String JavaDoc TECHNORATI_TAGS_TEMPLATE = "org/blojsom/plugin/technorati/templates/admin-technorati-tags.vm";
61     private static final String JavaDoc TECHNORATI_TAG_LINK_TEMPLATE = "org/blojsom/plugin/technorati/templates/technorati-tag-link.vm";
62     private static final String JavaDoc TECHNORATI_TAGS = "TECHNORATI_TAGS";
63     private static final String JavaDoc TECHNORATI_TAG_LINKS = "TECHNORATI_TAG_LINKS";
64
65     private EventBroadcaster _eventBroadcaster;
66
67     public static final String JavaDoc METADATA_TECHNORATI_TAGS = "technorati-tags";
68
69     /**
70      * Create a new instance of the Technorati tag plugin
71      */

72     public TechnoratiTagsPlugin() {
73     }
74
75     /**
76      * Set the {@link org.blojsom.event.EventBroadcaster} event broadcaster
77      *
78      * @param blojsomEventBroadcaster {@link org.blojsom.event.EventBroadcaster}
79      */

80     public void setEventBroadcaster(EventBroadcaster eventBroadcaster) {
81         _eventBroadcaster = eventBroadcaster;
82     }
83
84     /**
85      * Initialize this plugin. This method only called when the plugin is instantiated.
86      *
87      * @throws org.blojsom.plugin.PluginException
88      * If there is an error initializing the plugin
89      */

90     public void init() throws PluginException {
91         super.init();
92
93         _eventBroadcaster.addListener(this);
94     }
95
96     /**
97      * Process the blog entries
98      *
99      * @param httpServletRequest Request
100      * @param httpServletResponse Response
101      * @param blog {@link Blog} instance
102      * @param context Context
103      * @param entries Blog entries retrieved for the particular request
104      * @return Modified set of blog entries
105      * @throws PluginException If there is an error processing the blog entries
106      */

107     public Entry[] process(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, Blog blog, Map JavaDoc context, Entry[] entries) throws PluginException {
108         for (int i = 0; i < entries.length; i++) {
109             Entry entry = entries[i];
110             Map JavaDoc entryMetaData = entry.getMetaData();
111
112             if (BlojsomUtils.checkMapForKey(entryMetaData, METADATA_TECHNORATI_TAGS)) {
113                 String JavaDoc[] tags = BlojsomUtils.parseOnlyCommaList((String JavaDoc) entryMetaData.get(METADATA_TECHNORATI_TAGS));
114                 if (tags != null && tags.length > 0) {
115                     ArrayList JavaDoc tagLinks = new ArrayList JavaDoc(tags.length);
116                     String JavaDoc tagLinkTemplate = mergeTemplate(TECHNORATI_TAG_LINK_TEMPLATE, blog, new HashMap JavaDoc());
117                     for (int j = 0; j < tags.length; j++) {
118                         String JavaDoc tag = tags[j].trim();
119                         tag = BlojsomUtils.escapeStringSimple(tag);
120
121                         tagLinks.add(MessageFormat.format(tagLinkTemplate, new String JavaDoc[]{tag}));
122                     }
123
124                     entryMetaData.put(TECHNORATI_TAG_LINKS, tagLinks.toArray(new String JavaDoc[tagLinks.size()]));
125                 }
126             }
127         }
128
129         return entries;
130     }
131
132     /**
133      * Perform any cleanup for the plugin. Called after {@link #process}.
134      *
135      * @throws org.blojsom.plugin.PluginException
136      * If there is an error performing cleanup for this plugin
137      */

138     public void cleanup() throws PluginException {
139     }
140
141     /**
142      * Called when BlojsomServlet is taken out of service
143      *
144      * @throws org.blojsom.plugin.PluginException
145      * If there is an error in finalizing this plugin
146      */

147     public void destroy() throws PluginException {
148     }
149
150     /**
151      * Handle an event broadcast from another component
152      *
153      * @param event {@link org.blojsom.event.Event} to be handled
154      */

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

163     public void processEvent(Event event) {
164         if (event instanceof ProcessEntryEvent) {
165             ProcessEntryEvent processBlogEntryEvent = (ProcessEntryEvent) event;
166
167             String JavaDoc technoratiTags = BlojsomUtils.getRequestValue(METADATA_TECHNORATI_TAGS, processBlogEntryEvent.getHttpServletRequest());
168             if (processBlogEntryEvent.getEntry() != null) {
169                 String JavaDoc savedTechnoratiTags = (String JavaDoc) processBlogEntryEvent.getEntry().getMetaData().get(METADATA_TECHNORATI_TAGS);
170                 if (savedTechnoratiTags != null) {
171                     if (technoratiTags == null) {
172                         // Request parameter not available, save old set of tags
173
technoratiTags = savedTechnoratiTags;
174                         // Request parameter blank, so throw away set of tags
175
} else if ("".equals(technoratiTags.trim())) {
176                         technoratiTags = "";
177                     }
178                 }
179
180                 processBlogEntryEvent.getEntry().getMetaData().put(METADATA_TECHNORATI_TAGS, technoratiTags);
181             }
182
183             Map JavaDoc context = processBlogEntryEvent.getContext();
184
185             Map JavaDoc templateAdditions = (Map JavaDoc) processBlogEntryEvent.getContext().get("BLOJSOM_TEMPLATE_ADDITIONS");
186             if (templateAdditions == null) {
187                 templateAdditions = new TreeMap JavaDoc();
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