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.emoticons;
031:
032: import org.blojsom.blog.Blog;
033: import org.blojsom.blog.Comment;
034: import org.blojsom.blog.Entry;
035: import org.blojsom.plugin.Plugin;
036: import org.blojsom.plugin.PluginException;
037: import org.blojsom.util.BlojsomUtils;
038:
039: import javax.servlet.http.HttpServletRequest;
040: import javax.servlet.http.HttpServletResponse;
041: import java.util.*;
042:
043: /**
044: * Enhanced Emoticons Plugin. This is slightly modified version of the built-in
045: * Emoticon Plugin. It just adds the ability to configure the available
046: * emoticons and the patterns they correspond to via the emoticons properties
047: * file.
048: *
049: * @author David Czarnecki
050: * @author Jan Wessely
051: * @version $Id: EnhancedEmoticonsPlugin.java,v 1.6 2007/01/22 14:45:07 czarneckid Exp $
052: * @since blojsom 3.0
053: */
054: public class EnhancedEmoticonsPlugin implements Plugin {
055:
056: private static final String BLOJSOM_PLUGIN_METADATA_EMOTICONS_DISABLED = "emoticons-disabled";
057:
058: private static final String EMOTICONS_PARAM = "emoticons";
059: private static final String EMOTICONS_PATTERN_POSTFIX = ".pattern";
060: private static final String IMG_OPEN = "<img src=\"";
061: private static final String IMG_CLOSE = "\"";
062: private static final String IMG_ALT_START = " alt=\"";
063: private static final String IMG_ALT_END = "\" />";
064: private static final String EMOTICONS_CLASS = " class=\"emoticons\" ";
065:
066: private Map _emoticons;
067: private boolean _parseComments = false;
068:
069: /**
070: * Default constructor
071: */
072: public EnhancedEmoticonsPlugin() {
073: }
074:
075: /**
076: * Set the emoticons configuration data
077: *
078: * @param emoticons Emoticons configuration data
079: */
080: public void setEmoticons(Map emoticons) {
081: _emoticons = emoticons;
082: }
083:
084: /**
085: * Set whether or not to parse comments for emoticons
086: *
087: * @param parseComments Parse comments
088: */
089: public void setParseComments(boolean parseComments) {
090: _parseComments = parseComments;
091: }
092:
093: /**
094: * Initialize this plugin. This method only called when the plugin is
095: * instantiated.
096: *
097: * @throws PluginException If there is an error initializing the plugin
098: */
099: public void init() throws PluginException {
100: }
101:
102: /**
103: * Read the list of available Emoticons and return it as a
104: * {@link java.util.List}.
105: *
106: * @param emoticons list of available plugins from the emoticons properties file.
107: * @return {@link java.util.List} of available emoticons.
108: */
109: private List parseEmoticons(String emoticons) {
110: List list = new ArrayList();
111: StringTokenizer tok = new StringTokenizer(emoticons,
112: "\t\n\r\f,; ");
113:
114: while (tok.hasMoreTokens()) {
115: list.add(tok.nextToken());
116: }
117:
118: return list;
119: }
120:
121: /**
122: * Process the blog entries
123: *
124: * @param httpServletRequest Request
125: * @param httpServletResponse Response
126: * @param blog {@link Blog} instance
127: * @param context Contextblojsom
128: * @param entries Blog entries retrieved for the particular request
129: * @return Modified set of blog entries
130: * @throws PluginException If there is an error processing the blog entries
131: */
132: public org.blojsom.blog.Entry[] process(
133: HttpServletRequest httpServletRequest,
134: HttpServletResponse httpServletResponse, Blog blog,
135: Map context, org.blojsom.blog.Entry[] entries)
136: throws PluginException {
137: if (_emoticons == null) {
138: return entries;
139: }
140:
141: List availableEmoticons = parseEmoticons((String) _emoticons
142: .get(EMOTICONS_PARAM));
143: String blogBaseUrl = blog.getBlogBaseURL();
144:
145: for (int i = 0; i < entries.length; i++) {
146: Entry entry = entries[i];
147:
148: if (!BlojsomUtils.checkMapForKey(entry.getMetaData(),
149: BLOJSOM_PLUGIN_METADATA_EMOTICONS_DISABLED)) {
150: String updatedDescription = entry.getDescription();
151: Iterator iter = availableEmoticons.iterator();
152:
153: while (iter.hasNext()) {
154: String emoticon = (String) iter.next();
155: updatedDescription = replaceEmoticon(
156: updatedDescription, emoticon, blogBaseUrl);
157: }
158:
159: entry.setDescription(updatedDescription);
160:
161: if (_parseComments) {
162: Comment comments[] = entry.getCommentsAsArray();
163: for (int j = 0; j < comments.length; j++) {
164: Comment comment = comments[j];
165:
166: String updatedCommentText = comment
167: .getComment();
168: iter = availableEmoticons.iterator();
169:
170: while (iter.hasNext()) {
171: String emoticon = (String) iter.next();
172: updatedCommentText = replaceEmoticon(
173: updatedCommentText, emoticon,
174: blogBaseUrl);
175: }
176:
177: comment.setComment(updatedCommentText);
178: }
179: }
180: }
181: }
182:
183: return entries;
184: }
185:
186: /**
187: * Replace the references in the description with the URL to the image for
188: * the emoticon
189: *
190: * @param emoticonString Description string
191: * @param emoticon Emoticon name
192: * @param url Base URL for the blog
193: * @return Updated description with emoticon references replaced with URLs
194: * to the images for the emoticons
195: */
196: private String replaceEmoticon(String emoticonString,
197: String emoticon, String url) {
198: String emoticonImage, emoticonPattern;
199: emoticonImage = (String) _emoticons.get(emoticon);
200: emoticonPattern = (String) _emoticons.get(emoticon
201: + EMOTICONS_PATTERN_POSTFIX);
202:
203: if (!BlojsomUtils.checkNullOrBlank(emoticonImage)) {
204: StringBuffer imageReference = new StringBuffer(IMG_OPEN);
205: imageReference.append(url);
206: imageReference.append(emoticonImage);
207: imageReference.append(IMG_CLOSE);
208: imageReference.append(EMOTICONS_CLASS);
209: imageReference.append(IMG_ALT_START);
210: imageReference.append(emoticonImage);
211: imageReference.append(IMG_ALT_END);
212:
213: return BlojsomUtils.replace(emoticonString,
214: emoticonPattern, imageReference.toString());
215: }
216:
217: return emoticonString;
218: }
219:
220: /**
221: * Perform any cleanup for the plugin. Called after {@link #process}.
222: *
223: * @throws PluginException If there is an error performing cleanup for this plugin
224: */
225: public void cleanup() throws PluginException {
226: }
227:
228: /**
229: * Called when BlojsomServlet is taken out of service
230: *
231: * @throws PluginException If there is an error in finalizing this plugin
232: */
233: public void destroy() throws PluginException {
234: }
235: }
|