KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > plugin > emoticons > EnhancedEmoticonsPlugin


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.emoticons;
32
33 import org.blojsom.blog.Blog;
34 import org.blojsom.blog.Entry;
35 import org.blojsom.plugin.Plugin;
36 import org.blojsom.plugin.PluginException;
37 import org.blojsom.util.BlojsomUtils;
38
39 import javax.servlet.http.HttpServletRequest JavaDoc;
40 import javax.servlet.http.HttpServletResponse JavaDoc;
41 import java.util.*;
42
43 /**
44  * Enhanced Emoticons Plugin. This is slightly modified version of the built-in
45  * Emoticon Plugin. It just adds the ability to configure the available
46  * emoticons and the patterns they correspond to via the emoticons properties
47  * file.
48  *
49  * @author David Czarnecki
50  * @author Jan Wessely
51  * @version $Id: EnhancedEmoticonsPlugin.java,v 1.2 2006/03/26 20:29:11 czarneckid Exp $
52  * @since blojsom 3.0
53  */

54 public class EnhancedEmoticonsPlugin implements Plugin {
55
56     private static final String JavaDoc BLOJSOM_PLUGIN_METADATA_EMOTICONS_DISABLED = "emoticons-disabled";
57
58     private static final String JavaDoc EMOTICONS_PARAM = "emoticons";
59     private static final String JavaDoc EMOTICONS_PATTERN_POSTFIX = ".pattern";
60     private static final String JavaDoc IMG_OPEN = "<img SRC=\"";
61     private static final String JavaDoc IMG_CLOSE = "\"";
62     private static final String JavaDoc IMG_ALT_START = " alt=\"";
63     private static final String JavaDoc IMG_ALT_END = "\" />";
64     private static final String JavaDoc EMOTICONS_CLASS = " class=\"emoticons\" ";
65
66     private Map _emoticons;
67
68     /**
69      * Default constructor
70      */

71     public EnhancedEmoticonsPlugin() {
72     }
73
74     /**
75      * Set the emoticons configuration data
76      *
77      * @param emoticons Emoticons configuration data
78      */

79     public void setEmoticons(Map emoticons) {
80         _emoticons = emoticons;
81     }
82
83     /**
84      * Initialize this plugin. This method only called when the plugin is
85      * instantiated.
86      *
87      * @throws PluginException If there is an error initializing the plugin
88      */

89     public void init() throws PluginException {
90     }
91
92     /**
93      * Read the list of available Emoticons and return it as a
94      * {@link java.util.List}.
95      *
96      * @param emoticons list of available plugins from the emoticons properties file.
97      * @return {@link java.util.List} of available emoticons.
98      */

99     private List parseEmoticons(String JavaDoc emoticons) {
100         List list = new ArrayList();
101         StringTokenizer tok = new StringTokenizer(emoticons, "\t\n\r\f,; ");
102
103         while (tok.hasMoreTokens()) {
104             list.add(tok.nextToken());
105         }
106
107         return list;
108     }
109
110     /**
111      * Process the blog entries
112      *
113      * @param httpServletRequest Request
114      * @param httpServletResponse Response
115      * @param user {@link org.blojsom.blog.BlogUser}instance
116      * @param context Context
117      * @param entries Blog entries retrieved for the particular request
118      * @return Modified set of blog entries
119      * @throws PluginException If there is an error processing the blog entries
120      */

121     public org.blojsom.blog.Entry[] process(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, Blog blog, Map context, org.blojsom.blog.Entry[] entries) throws PluginException {
122         if (_emoticons == null) {
123             return entries;
124         }
125
126         List availableEmoticons = parseEmoticons((String JavaDoc) _emoticons.get(EMOTICONS_PARAM));
127         String JavaDoc blogBaseUrl = blog.getBlogBaseURL();
128
129         for (int i = 0; i < entries.length; i++) {
130             Entry entry = entries[i];
131
132             if (!BlojsomUtils.checkMapForKey(entry.getMetaData(), BLOJSOM_PLUGIN_METADATA_EMOTICONS_DISABLED)) {
133                 String JavaDoc updatedDescription = entry.getDescription();
134                 Iterator iter = availableEmoticons.iterator();
135
136                 while (iter.hasNext()) {
137                     String JavaDoc emoticon = (String JavaDoc) iter.next();
138                     updatedDescription = replaceEmoticon(updatedDescription, emoticon, blogBaseUrl);
139                 }
140
141                 entry.setDescription(updatedDescription);
142             }
143         }
144
145         return entries;
146     }
147
148     /**
149      * Replace the references in the description with the URL to the image for
150      * the emoticon
151      *
152      * @param emoticonString Description string
153      * @param emoticon Emoticon name
154      * @param url Base URL for the blog
155      * @return Updated description with emoticon references replaced with URLs
156      * to the images for the emoticons
157      */

158     private String JavaDoc replaceEmoticon(String JavaDoc emoticonString, String JavaDoc emoticon, String JavaDoc url) {
159         String JavaDoc emoticonImage, emoticonPattern;
160         emoticonImage = (String JavaDoc) _emoticons.get(emoticon);
161         emoticonPattern = (String JavaDoc) _emoticons.get(emoticon + EMOTICONS_PATTERN_POSTFIX);
162
163         if (!BlojsomUtils.checkNullOrBlank(emoticonImage)) {
164             StringBuffer JavaDoc imageReference = new StringBuffer JavaDoc(IMG_OPEN);
165             imageReference.append(url);
166             imageReference.append(emoticonImage);
167             imageReference.append(IMG_CLOSE);
168             imageReference.append(EMOTICONS_CLASS);
169             imageReference.append(IMG_ALT_START);
170             imageReference.append(emoticonImage);
171             imageReference.append(IMG_ALT_END);
172
173             return BlojsomUtils.replace(emoticonString, emoticonPattern, imageReference.toString());
174         }
175
176         return emoticonString;
177     }
178
179     /**
180      * Perform any cleanup for the plugin. Called after {@link #process}.
181      *
182      * @throws PluginException If there is an error performing cleanup for this plugin
183      */

184     public void cleanup() throws PluginException {
185     }
186
187     /**
188      * Called when BlojsomServlet is taken out of service
189      *
190      * @throws PluginException If there is an error in finalizing this plugin
191      */

192     public void destroy() throws PluginException {
193     }
194 }
Popular Tags