1 18 19 package org.apache.roller.ui.rendering.plugins; 20 21 import java.util.Enumeration ; 22 import java.util.Map ; 23 import java.util.Properties ; 24 import java.util.regex.Matcher ; 25 import java.util.regex.Pattern ; 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 38 public class SmileysPlugin implements WeblogEntryPlugin { 39 40 private static Log mLogger = LogFactory.getLog(SmileysPlugin.class); 41 42 public static Pattern [] smileyPatterns = new Pattern [0]; static String [] imageTags = new String [0]; 44 private static Properties smileyDefs = new Properties (); 45 46 private String name = "Emoticons"; 47 private String 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 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 getName() { 66 return name; 67 } 68 69 70 public String getDescription() { 71 return StringEscapeUtils.escapeJavaScript(description); 72 } 73 74 75 80 public synchronized void init(WebsiteData website) throws RollerException { 81 if (SmileysPlugin.smileyPatterns.length < 1) { 83 String baseURL = RollerRuntimeConfig.getAbsoluteContextURL(); 84 85 Pattern [] tempP = new Pattern [SmileysPlugin.smileyDefs.size()]; 86 String [] tempS = new String [SmileysPlugin.smileyDefs.size()]; 87 int count = 0; 89 Enumeration enum1 = SmileysPlugin.smileyDefs.propertyNames(); 90 while(enum1.hasMoreElements()) { 91 String smiley = (String )enum1.nextElement(); 92 String 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 count++; 102 } 103 SmileysPlugin.smileyPatterns = tempP; 104 SmileysPlugin.imageTags = tempS; 105 } 106 } 107 108 109 112 public String render(WeblogEntryData entry, String text) { 113 Matcher 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 126 private String htmlEscape(String smiley) { 127 char[] chars = smiley.toCharArray(); 128 StringBuffer buf = new StringBuffer (); 129 for (int i=0; i<chars.length; i++) { 130 if (chars[i] == '"') { 131 buf.append("""); 132 } else if (chars[i] == '>') { 133 buf.append(">"); 134 } else if (chars[i] == '<') { 135 buf.append("<"); 136 } else { 137 buf.append(chars[i]); 138 } 139 } 140 return buf.toString(); 141 } 142 143 150 private static char[] escape_regex = new char[] 151 {'-', '(', ')', '\\', '|', ':', '^', '$', '*', '+', '?', 152 '{', '}', '!', '=', '<', '>', '&', '[', ']' }; 153 154 private String regexEscape(String smiley) { 155 char[] chars = smiley.toCharArray(); 156 StringBuffer buf = new StringBuffer (); 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 |