1 19 package org.archive.util; 20 21 import java.io.BufferedReader ; 22 import java.io.IOException ; 23 import java.io.PrintWriter ; 24 import java.io.StringReader ; 25 import java.io.StringWriter ; 26 import java.util.HashMap ; 27 import java.util.Map ; 28 import java.util.regex.Matcher ; 29 import java.util.regex.Pattern ; 30 31 import javax.servlet.jsp.JspWriter ; 32 33 import org.apache.commons.lang.StringEscapeUtils; 34 35 public class TextUtils { 36 private static final String FIRSTWORD = "^([^\\s]*).*$"; 37 38 41 private static final int MAX_ENTITY_WIDTH = 9; 42 43 private static final ThreadLocal <Map <String ,Matcher >> TL_MATCHER_MAP 44 = new ThreadLocal <Map <String ,Matcher >>() { 45 protected Map <String ,Matcher > initialValue() { 46 return new HashMap <String ,Matcher >(50); 47 } 48 }; 49 50 66 public static Matcher getMatcher(String pattern, CharSequence input) { 67 if (pattern == null) { 68 throw new IllegalArgumentException ("String 'pattern' must not be null"); 69 } 70 final Map <String ,Matcher > matchers = TL_MATCHER_MAP.get(); 71 Matcher m = (Matcher )matchers.get(pattern); 72 if(m == null) { 73 m = Pattern.compile(pattern).matcher(input); 74 } else { 75 matchers.put(pattern,null); 76 m.reset(input); 77 } 78 return m; 79 } 80 81 public static void recycleMatcher(Matcher m) { 82 final Map <String ,Matcher > matchers = TL_MATCHER_MAP.get(); 83 matchers.put(m.pattern().pattern(),m); 84 } 85 86 97 public static String replaceAll( 98 String pattern, CharSequence input, String replacement) { 99 Matcher m = getMatcher(pattern, input); 100 String res = m.replaceAll(replacement); 101 recycleMatcher(m); 102 return res; 103 } 104 105 116 public static String replaceFirst( 117 String pattern, CharSequence input, String replacement) { 118 Matcher m = getMatcher(pattern, input); 119 String res = m.replaceFirst(replacement); 120 recycleMatcher(m); 121 return res; 122 } 123 124 134 public static boolean matches(String pattern, CharSequence input) { 135 Matcher m = getMatcher(pattern, input); 136 boolean res = m.matches(); 137 recycleMatcher(m); 138 return res; 139 } 140 141 150 public static String [] split(String pattern, CharSequence input) { 151 Matcher m = getMatcher(pattern,input); 152 String [] retVal = m.pattern().split(input); 153 recycleMatcher(m); 154 return retVal; 155 } 156 157 162 public static String getFirstWord(String s) { 163 Matcher m = getMatcher(FIRSTWORD, s); 164 String retVal = (m != null && m.matches())? m.group(1): null; 165 recycleMatcher(m); 166 return retVal; 167 } 168 169 179 public static String escapeForHTMLJavascript(String s) { 180 return escapeForHTML(StringEscapeUtils.escapeJavaScript(s)); 181 } 182 183 190 public static String escapeForMarkupAttribute(String s) { 191 return StringEscapeUtils.escapeXml(s); 192 } 193 194 201 public static String escapeForHTML(String s) { 202 String escaped = s.replaceAll("&","&"); 204 return escaped.replaceAll("<","<"); 205 } 206 207 215 public static void writeEscapedForHTML(String s, JspWriter out) 216 throws IOException { 217 BufferedReader reader = new BufferedReader (new StringReader (s)); 218 String line; 219 while((line=reader.readLine()) != null){ 220 out.println(StringEscapeUtils.escapeHtml(line)); 221 } 222 } 223 224 229 public static CharSequence unescapeHtml(final CharSequence cs) { 230 if (cs == null) { 231 return cs; 232 } 233 234 int startEntityCode = -1; 236 int endEntityCode = -1; 237 238 for (int i = 0; i < cs.length(); i++) { 240 if (cs.charAt(i) == '&') { 241 startEntityCode = i; 242 } else if (cs.charAt(i) == ';' && startEntityCode >= 0 && 243 i > startEntityCode && 244 ((i - startEntityCode) < MAX_ENTITY_WIDTH)) { 245 endEntityCode = i; 246 } 247 } 248 249 return (startEntityCode != 0 && endEntityCode != 0)? 250 StringEscapeUtils.unescapeHtml(cs.toString()): cs; 251 } 252 253 260 public static String exceptionToString(String message, Throwable e) { 261 StringWriter sw = new StringWriter (); 262 if (message == null || message.length() == 0) { 263 sw.write(message); 264 sw.write("\n"); 265 } 266 e.printStackTrace(new PrintWriter (sw)); 267 return sw.toString(); 268 } 269 } | Popular Tags |