KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > rendering > plugins > SmileysPlugin


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.ui.rendering.plugins;
20
21 import java.util.Enumeration JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Properties JavaDoc;
24 import java.util.regex.Matcher JavaDoc;
25 import java.util.regex.Pattern JavaDoc;
26 import org.apache.commons.lang.StringEscapeUtils;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.roller.RollerException;
30 import org.apache.roller.config.RollerRuntimeConfig;
31 import org.apache.roller.model.WeblogEntryPlugin;
32 import org.apache.roller.pojos.WeblogEntryData;
33 import org.apache.roller.pojos.WebsiteData;
34
35 /**
36  * Converts ascii emoticons into HTML image tags.
37  */

38 public class SmileysPlugin implements WeblogEntryPlugin {
39     
40     private static Log mLogger = LogFactory.getLog(SmileysPlugin.class);
41     
42     public static Pattern JavaDoc[] smileyPatterns = new Pattern JavaDoc[0]; // public for tests
43
static String JavaDoc[] imageTags = new String JavaDoc[0];
44     private static Properties JavaDoc smileyDefs = new Properties JavaDoc();
45     
46     private String JavaDoc name = "Emoticons";
47     private String JavaDoc description = "Change ASCII emoticons to graphics. " +
48             ":-) becomes <img SRC='./images/smileys/smile.gif'>";
49     
50     
51     static {
52         try {
53             smileyDefs.load(SmileysPlugin.class.getResourceAsStream("smileys.properties"));
54         } catch (Exception JavaDoc e) {
55             mLogger.error("Unable to load smileys.properties", e);
56         }
57     }
58
59     
60     public SmileysPlugin() {
61         mLogger.debug("SmileysPlugin instantiated.");
62     }
63     
64     
65     public String JavaDoc getName() {
66         return name;
67     }
68     
69     
70     public String JavaDoc getDescription() {
71         return StringEscapeUtils.escapeJavaScript(description);
72     }
73     
74     
75     /*
76      * Convert the SmileyDefs into RegEx patterns and img tags for
77      * later use. Need an HttpServletRequest though so that we can
78      * get the ServletContext Path. But only do it once.
79      */

80     public synchronized void init(WebsiteData website) throws RollerException {
81         // don't do this work if Smileys already loaded
82
if (SmileysPlugin.smileyPatterns.length < 1) {
83             String JavaDoc baseURL = RollerRuntimeConfig.getAbsoluteContextURL();
84             
85             Pattern JavaDoc[] tempP = new Pattern JavaDoc[SmileysPlugin.smileyDefs.size()];
86             String JavaDoc[] tempS = new String JavaDoc[SmileysPlugin.smileyDefs.size()];
87             //System.out.println("# smileys: " + smileyDefs.size());
88
int count = 0;
89             Enumeration JavaDoc enum1 = SmileysPlugin.smileyDefs.propertyNames();
90             while(enum1.hasMoreElements()) {
91                 String JavaDoc smiley = (String JavaDoc)enum1.nextElement();
92                 String JavaDoc smileyAlt = htmlEscape(smiley);
93                 tempP[count] = Pattern.compile(regexEscape(smiley));
94                 tempS[count] = "<img SRC=\"" +
95                         baseURL + "/images/smileys/" +
96                         smileyDefs.getProperty(smiley, "smile.gif") +
97                         "\" class=\"smiley\"" +
98                         " alt=\"" + smileyAlt + "\"" +
99                         " title=\"" + smileyAlt +"\" />";
100                 //System.out.println(smiley + "=" + tempS[count]);
101
count++;
102             }
103             SmileysPlugin.smileyPatterns = tempP;
104             SmileysPlugin.imageTags = tempS;
105         }
106     }
107     
108     
109     /**
110      * Find occurences of ascii emoticons and turn them into HTML image pointers.
111      */

112     public String JavaDoc render(WeblogEntryData entry, String JavaDoc text) {
113         Matcher JavaDoc matcher = null;
114         for (int i=0; i<smileyPatterns.length; i++) {
115             matcher = smileyPatterns[i].matcher(text);
116             text = matcher.replaceAll(imageTags[i]);
117         }
118         return text;
119     }
120     
121     
122     /*
123      * To display the smiley 'glyph' certain characters
124      * must be HTML escaped.
125      */

126     private String JavaDoc htmlEscape(String JavaDoc smiley) {
127         char[] chars = smiley.toCharArray();
128         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
129         for (int i=0; i<chars.length; i++) {
130             if (chars[i] == '"') {
131                 buf.append("&quot;");
132             } else if (chars[i] == '>') {
133                 buf.append("&gt;");
134             } else if (chars[i] == '<') {
135                 buf.append("&lt;");
136             } else {
137                 buf.append(chars[i]);
138             }
139         }
140         return buf.toString();
141     }
142     
143     /**
144      * Some characters have to escaped with a backslash before
145      * being compiled into a Regular Expression.
146      *
147      * @param smiley
148      * @return
149      */

150     private static char[] escape_regex = new char[]
151     {'-', '(', ')', '\\', '|', ':', '^', '$', '*', '+', '?',
152      '{', '}', '!', '=', '<', '>', '&', '[', ']' };
153     
154     private String JavaDoc regexEscape(String JavaDoc smiley) {
155         char[] chars = smiley.toCharArray();
156         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
157         for (int i=0; i<chars.length; i++) {
158             for (int x=0; x<escape_regex.length; x++) {
159                 if (escape_regex[x] == chars[i]) {
160                     buf.append("\\");
161                     break;
162                 }
163             }
164             buf.append(chars[i]);
165         }
166         return buf.toString();
167     }
168     
169 }
170
Popular Tags