1 package org.roller.presentation.velocity.plugins.smileys; 2 3 import org.apache.commons.lang.StringEscapeUtils; 4 import org.apache.commons.logging.Log; 5 import org.apache.commons.logging.LogFactory; 6 import org.apache.velocity.context.Context; 7 import org.roller.RollerException; 8 import org.roller.pojos.WeblogEntryData; 9 import org.roller.presentation.RollerRequest; 10 import org.roller.presentation.velocity.PagePlugin; 11 12 import java.util.Enumeration ; 13 import java.util.Properties ; 14 import java.util.regex.Matcher ; 15 import java.util.regex.Pattern ; 16 17 23 public class SmileysPlugin implements PagePlugin 24 { 25 private String name = "Emoticons"; 26 private String description = "Change ASCII emoticons to graphics. " + 27 ":-) becomes <img SRC='./images/smileys/smile.gif'>"; 28 29 public String toString() { return name; } 30 31 static Pattern [] smileyPatterns = new Pattern [0]; 32 static String [] imageTags = new String [0]; 33 34 private static Log mLogger = 35 LogFactory.getFactory().getInstance(SmileysPlugin.class); 36 37 40 private static Properties smileyDefs = new Properties (); 41 static 42 { 43 try 44 { 45 smileyDefs.load(SmileysPlugin.class.getResourceAsStream("smileys.properties")); 46 } 47 catch (Exception e) 48 { 49 mLogger.error("Unable to load smileys.properties", e); 50 } 51 } 52 53 public SmileysPlugin() 54 { 55 mLogger.debug("SmileysPlugin instantiated."); 56 } 57 58 64 public String render(String text) 65 { 66 Matcher matcher = null; 67 for (int i=0; i<smileyPatterns.length; i++) 68 { 69 matcher = smileyPatterns[i].matcher(text); 70 text = matcher.replaceAll(imageTags[i]); 71 } 72 return text; 73 } 74 75 82 public synchronized void init(RollerRequest rreq, Context ctx) throws RollerException 83 { 84 if (SmileysPlugin.smileyPatterns.length < 1) 86 { 87 String contextPath = ""; 88 if (rreq != null && rreq.getRequest() != null) 89 { 90 contextPath = rreq.getRequest().getContextPath(); 91 } 92 Pattern [] tempP = new Pattern [SmileysPlugin.smileyDefs.size()]; 93 String [] tempS = new String [SmileysPlugin.smileyDefs.size()]; 94 int count = 0; 96 Enumeration enum1 = SmileysPlugin.smileyDefs.propertyNames(); 97 while(enum1.hasMoreElements()) 98 { 99 String smiley = (String )enum1.nextElement(); 100 String smileyAlt = htmlEscape(smiley); 101 tempP[count] = Pattern.compile(regexEscape(smiley)); 102 tempS[count] = "<img SRC=\"" + 103 contextPath + "/images/smileys/" + 104 smileyDefs.getProperty(smiley, "smile.gif") + 105 "\" class=\"smiley\"" + 106 " alt=\"" + smileyAlt + "\"" + 107 " title=\"" + smileyAlt +"\">"; 108 count++; 110 } 111 SmileysPlugin.smileyPatterns = tempP; 112 SmileysPlugin.imageTags = tempS; 113 } 114 } 115 116 120 private String htmlEscape(String smiley) 121 { 122 char[] chars = smiley.toCharArray(); 123 StringBuffer buf = new StringBuffer (); 124 for (int i=0; i<chars.length; i++) 125 { 126 if (chars[i] == '"') 127 { 128 buf.append("""); 129 } 130 else if (chars[i] == '>') 131 { 132 buf.append(">"); 133 } 134 else if (chars[i] == '<') 135 { 136 buf.append("<"); 137 } 138 else 139 { 140 buf.append(chars[i]); 141 } 142 } 143 return buf.toString(); 144 } 145 146 153 private static char[] escape_regex = new char[] 154 {'-', '(', ')', '\\', '|', ':', '^', '$', '*', '+', '?', 155 '{', '}', '!', '=', '<', '>', '&', '[', ']' }; 156 private String regexEscape(String smiley) 157 { 158 char[] chars = smiley.toCharArray(); 159 StringBuffer buf = new StringBuffer (); 160 for (int i=0; i<chars.length; i++) 161 { 162 for (int x=0; x<escape_regex.length; x++) 163 { 164 if (escape_regex[x] == chars[i]) 165 { 166 buf.append("\\"); 167 break; 168 } 169 } 170 buf.append(chars[i]); 171 } 172 return buf.toString(); 173 } 174 175 178 public String render(WeblogEntryData entry, boolean skipFlag) 179 { 180 return render(entry.getText()); 181 } 182 183 public String getName() { return name; } 184 public String getDescription() { return StringEscapeUtils.escapeJavaScript(description); } 185 } 186 | Popular Tags |