1 25 package org.jresearch.gossip.util; 26 27 import java.text.MessageFormat ; 28 import java.util.ArrayList ; 29 import java.util.Enumeration ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.ResourceBundle ; 35 36 import org.apache.oro.text.PatternCacheLRU; 37 import org.apache.oro.text.perl.Perl5Util; 38 import org.apache.struts.util.MessageResources; 39 import org.jresearch.gossip.IConst; 40 import org.jresearch.gossip.configuration.Configurator; 41 import org.jresearch.gossip.exception.ConfiguratorException; 42 import org.tiling.memo.LruCacheFactory; 43 44 49 public class MessageProcessor { 50 51 private static MessageProcessor ourInstance; 52 53 private PatternCacheLRU cache = new PatternCacheLRU(50); 54 55 private Map resultCache = new LruCacheFactory(1000).createCache(); 56 57 private static HashMap emoticonsMap; 58 59 private static final String EMOTICON_IMG_TAG = "<img SRC=\"{0}images/emoticons/{1}.gif\" alt=\"{2}\" title=\"{2}\">"; 60 61 private static final String TAG_NOSMILE_NAME = "NOSMILE"; 62 63 private static final String TAG_NOSMILE = "[" + TAG_NOSMILE_NAME + "]"; 64 65 private static final String TAG_NOSMILE_END = "[/" + TAG_NOSMILE_NAME + "]"; 66 67 private Perl5Util perlUtil; 68 69 private static Object lock = new Object (); 70 71 77 public synchronized static void setEmoticonsMap(ResourceBundle bundle) { 78 Enumeration keys = bundle.getKeys(); 79 emoticonsMap = new HashMap (); 80 81 while (keys.hasMoreElements()) { 82 String key = (String ) keys.nextElement(); 83 String pattern = bundle.getString(key); 84 85 emoticonsMap.put(key, HtmlCodec.encode(pattern)); 86 } 87 } 88 89 94 public static HashMap getEmoticonsMap() { 95 return emoticonsMap; 96 } 97 98 103 public static MessageProcessor getInstance() { 104 if (ourInstance == null) { 105 synchronized (lock) { 106 if (ourInstance == null) { 107 ourInstance = new MessageProcessor(); 108 } 109 } 110 } 111 112 return ourInstance; 113 } 114 115 private MessageProcessor() { 116 perlUtil = new Perl5Util(cache); 117 } 118 119 131 public String prepareMessage(String result, int cutToLength, 132 MessageResources messages) { 133 String key = Integer.toString(result.hashCode()) + cutToLength; 134 Object value = resultCache.get(key); 135 136 if ((value == null) && !resultCache.containsKey(key)) { 137 value = prepare(result, cutToLength, messages); 138 resultCache.put(key, value); 139 } 140 141 return (String ) value; 142 } 143 144 148 public String cleanup(String result) { 149 return perlUtil.substitute("s/\\[(.*?)\\]/\r/gsi", result); 150 } 151 152 private String prepare(String result, int cutToLength, 153 MessageResources messages) { 154 155 if (cutToLength > 0) { 156 result = cleanup(result); 157 158 if (result.length() > cutToLength) { 159 result = result.substring(0, cutToLength); 160 result += "..."; 161 } 162 } else { 163 result = perlUtil.substitute( 165 "s/\\[b\\](.*?)\\[\\/b\\]/<strong>$1<\\/strong>/gsi", 166 result); 167 result = perlUtil.substitute( 168 "s/\\[i\\](.*?)\\[\\/i\\]/<em>$1<\\/em>/gsi", result); 169 result = perlUtil 170 .substitute( 171 "s/\\[url\\](http[s]?:\\/\\/){1}(.*?)\\[\\/url\\]/<A HREF=\"$1$2\" TARGET=\"_blank\">$2<\\/A>/gsi", 172 result); 173 result = perlUtil 174 .substitute( 175 "s/\\[url=(http[s]?:\\/\\/){1}(.*?)\\](.*?)\\[\\/url\\]/<A HREF=\"$1$2\" TARGET=\"_blank\">$3<\\/A>/gsi", 176 result); 177 result = perlUtil 178 .substitute( 179 "s/\\[img\\](http[s]?:\\/\\/){1}(.*?)\\[\\/img\\]/<IMG SRC=\"$1$2\" BORDER=0>/gsi", 180 result); 181 result = perlUtil.substitute( 182 "s/\\[HR\\]/<hr size=\"1\" width=\"90%\" noshade>/gsi", 183 result); 184 result = perlUtil.substitute("s/\\[QUOTE\\]/<blockquote>/gsi", 185 result); 186 result = perlUtil.substitute( 187 "s/\\[\\/QUOTE\\]/<\\/blockquote>/gsi", result); 188 result = perlUtil.substitute( 189 "s/\\[code\\](.*?)\\[\\/code\\]/<code>$1<\\/code>/gsi", 190 result); 191 result = nl2br(result); 192 } 193 194 return result; 195 } 196 197 205 public String nl2br(String result) { 206 return perlUtil.substitute("s/\\n/<br>/g", result); 207 } 208 209 222 223 public String processEmoticons(String result, MessageResources messages) 224 throws ConfiguratorException { 225 result = perlUtil.substitute("s/" + TAG_NOSMILE_NAME + "\\]/" 227 + TAG_NOSMILE_NAME + "\\]/gsi", result); 228 229 List tokens = splitOnNoSmileTag(result); 230 StringBuffer res = new StringBuffer (); 231 for (int i = 0; i < tokens.size(); i++) { 232 MPToken token = (MPToken) tokens.get(i); 233 if (token.processSmiles) { 234 res.append(replaceEmoticons(token.text, messages)); 235 } else { 236 res.append(token.text); 237 } 238 } 239 return res.toString(); 240 } 241 242 private String replaceEmoticons(String result, MessageResources messages) 243 throws ConfiguratorException { 244 Iterator it = emoticonsMap.keySet().iterator(); 245 Object [] args = { 246 Configurator.getInstance().get(IConst.CONFIG.WEB_ROOT), "", "" }; 247 250 while (it.hasNext()) { 251 String key = (String ) it.next(); 252 String pattern = (String ) emoticonsMap.get(key); 253 args[1] = key; 254 args[2] = messages.getMessage(Configurator.getInstance().getLocale( 255 IConst.CONFIG.DEFAULT_LOCALE), key); 256 result = replace(result, pattern, MessageFormat.format( 257 EMOTICON_IMG_TAG, args)); 258 } 259 260 return result; 261 } 262 263 private List splitOnNoSmileTag(String result) { 264 ArrayList tokens = new ArrayList (); 265 StringBuffer sb = new StringBuffer (result); 266 int ind1 = 0; 267 int ind2 = 0; 268 int length = sb.length(); 269 while (ind1 < length && ind2 < length) { 270 ind2 = sb.indexOf(TAG_NOSMILE, ind1); 271 if (ind2 == -1) { 272 ind2 = length; 273 } 274 tokens.add(new MPToken(true, sb.substring(ind1, ind2))); 275 if (ind2 != length) { 276 ind1 = ind2 + TAG_NOSMILE.length(); 277 ind2 = sb.indexOf(TAG_NOSMILE_END, ind1); 278 if (ind2 == -1) { 279 ind2 = length; 280 } 281 tokens.add(new MPToken(false, sb.substring(ind1, ind2))); 282 if (ind2 != length) { 283 ind1 = ind2 + TAG_NOSMILE_END.length(); 284 } 285 } 286 } 287 return tokens; 288 } 289 290 private class MPToken { 291 292 296 protected MPToken(boolean processSmiles, String text) { 297 super(); 298 this.processSmiles = processSmiles; 299 this.text = text; 300 } 301 302 boolean processSmiles; 303 304 String text; 305 } 306 307 322 public String replace(String result, String pattern, String replace) { 323 if (result == null) { 324 throw new IllegalArgumentException ("result parameter, value = " 325 + result); 326 } 327 328 if (replace == null) { 329 throw new IllegalArgumentException ("replace parameter, value = " 330 + replace); 331 } 332 333 if ((pattern == null) || "".equals(pattern)) { 334 throw new IllegalArgumentException ("pattern parameter, value = " 335 + pattern); 336 } 337 338 if ("".equals(result)) { 339 return result; 340 } 341 342 int s = 0; 343 int e = 0; 344 StringBuffer sb = new StringBuffer (result); 345 346 while ((e = sb.indexOf(pattern, s)) > -1) { 347 sb.replace(e, e + pattern.length(), replace); 348 s = e + replace.length(); 349 } 350 351 return sb.toString(); 352 } 353 } | Popular Tags |