001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.ui.rendering.plugins;
020:
021: import java.util.Enumeration;
022: import java.util.Map;
023: import java.util.Properties;
024: import java.util.regex.Matcher;
025: import java.util.regex.Pattern;
026: import org.apache.commons.lang.StringEscapeUtils;
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.apache.roller.RollerException;
030: import org.apache.roller.config.RollerRuntimeConfig;
031: import org.apache.roller.business.WeblogEntryPlugin;
032: import org.apache.roller.pojos.WeblogEntryData;
033: import org.apache.roller.pojos.WebsiteData;
034:
035: /**
036: * Converts ascii emoticons into HTML image tags.
037: */
038: public class SmileysPlugin implements WeblogEntryPlugin {
039:
040: private static Log mLogger = LogFactory.getLog(SmileysPlugin.class);
041:
042: public static Pattern[] smileyPatterns = new Pattern[0]; // public for tests
043: static String[] imageTags = new String[0];
044: private static Properties smileyDefs = new Properties();
045:
046: private String name = "Emoticons";
047: private String description = "Change ASCII emoticons to graphics. "
048: + ":-) becomes <img src='./images/smileys/smile.gif'>";
049:
050: static {
051: try {
052: smileyDefs.load(SmileysPlugin.class
053: .getResourceAsStream("smileys.properties"));
054: } catch (Exception e) {
055: mLogger.error("Unable to load smileys.properties", e);
056: }
057: }
058:
059: public SmileysPlugin() {
060: mLogger.debug("SmileysPlugin instantiated.");
061: }
062:
063: public String getName() {
064: return name;
065: }
066:
067: public String getDescription() {
068: return StringEscapeUtils.escapeJavaScript(description);
069: }
070:
071: /*
072: * Convert the SmileyDefs into RegEx patterns and img tags for
073: * later use. Need an HttpServletRequest though so that we can
074: * get the ServletContext Path. But only do it once.
075: */
076: public synchronized void init(WebsiteData website)
077: throws RollerException {
078: // don't do this work if Smileys already loaded
079: if (SmileysPlugin.smileyPatterns.length < 1) {
080: String baseURL = RollerRuntimeConfig
081: .getAbsoluteContextURL();
082:
083: Pattern[] tempP = new Pattern[SmileysPlugin.smileyDefs
084: .size()];
085: String[] tempS = new String[SmileysPlugin.smileyDefs.size()];
086: //System.out.println("# smileys: " + smileyDefs.size());
087: int count = 0;
088: Enumeration enum1 = SmileysPlugin.smileyDefs
089: .propertyNames();
090: while (enum1.hasMoreElements()) {
091: String smiley = (String) enum1.nextElement();
092: String smileyAlt = htmlEscape(smiley);
093: tempP[count] = Pattern.compile(regexEscape(smiley));
094: tempS[count] = "<img src=\"" + baseURL
095: + "/images/smileys/"
096: + smileyDefs.getProperty(smiley, "smile.gif")
097: + "\" class=\"smiley\"" + " alt=\"" + smileyAlt
098: + "\"" + " title=\"" + smileyAlt + "\" />";
099: //System.out.println(smiley + "=" + tempS[count]);
100: count++;
101: }
102: SmileysPlugin.smileyPatterns = tempP;
103: SmileysPlugin.imageTags = tempS;
104: }
105: }
106:
107: /**
108: * Find occurences of ascii emoticons and turn them into HTML image pointers.
109: */
110: public String render(WeblogEntryData entry, String text) {
111: Matcher matcher = null;
112: for (int i = 0; i < smileyPatterns.length; i++) {
113: matcher = smileyPatterns[i].matcher(text);
114: text = matcher.replaceAll(imageTags[i]);
115: }
116: return text;
117: }
118:
119: /*
120: * To display the smiley 'glyph' certain characters
121: * must be HTML escaped.
122: */
123: private String htmlEscape(String smiley) {
124: char[] chars = smiley.toCharArray();
125: StringBuffer buf = new StringBuffer();
126: for (int i = 0; i < chars.length; i++) {
127: if (chars[i] == '"') {
128: buf.append(""");
129: } else if (chars[i] == '>') {
130: buf.append(">");
131: } else if (chars[i] == '<') {
132: buf.append("<");
133: } else {
134: buf.append(chars[i]);
135: }
136: }
137: return buf.toString();
138: }
139:
140: /**
141: * Some characters have to escaped with a backslash before
142: * being compiled into a Regular Expression.
143: *
144: * @param smiley
145: * @return
146: */
147: private static char[] escape_regex = new char[] { '-', '(', ')',
148: '\\', '|', ':', '^', '$', '*', '+', '?', '{', '}', '!',
149: '=', '<', '>', '&', '[', ']' };
150:
151: private String regexEscape(String smiley) {
152: char[] chars = smiley.toCharArray();
153: StringBuffer buf = new StringBuffer();
154: for (int i = 0; i < chars.length; i++) {
155: for (int x = 0; x < escape_regex.length; x++) {
156: if (escape_regex[x] == chars[i]) {
157: buf.append("\\");
158: break;
159: }
160: }
161: buf.append(chars[i]);
162: }
163: return buf.toString();
164: }
165:
166: }
|