1 16 package org.apache.commons.lang; 17 18 import java.io.IOException ; 19 import java.io.Writer ; 20 21 import org.apache.commons.lang.exception.NestableRuntimeException; 22 23 41 public class StringEscapeUtils { 42 43 53 public StringEscapeUtils() { 54 } 55 56 79 public static String escapeJava(String str) { 80 return escapeJavaStyleString(str, false); 81 } 82 83 95 public static void escapeJava(Writer out, String str) throws IOException { 96 escapeJavaStyleString(out, str, false); 97 } 98 99 120 public static String escapeJavaScript(String str) { 121 return escapeJavaStyleString(str, true); 122 } 123 124 136 public static void escapeJavaScript(Writer out, String str) throws IOException { 137 escapeJavaStyleString(out, str, true); 138 } 139 140 private static String escapeJavaStyleString(String str, boolean escapeSingleQuotes) { 141 if (str == null) { 142 return null; 143 } 144 try { 145 StringPrintWriter writer = new StringPrintWriter(str.length() * 2); 146 escapeJavaStyleString(writer, str, escapeSingleQuotes); 147 return writer.getString(); 148 } catch (IOException ioe) { 149 ioe.printStackTrace(); 151 return null; 152 } 153 } 154 155 private static void escapeJavaStyleString(Writer out, String str, boolean escapeSingleQuote) throws IOException { 156 if (out == null) { 157 throw new IllegalArgumentException ("The Writer must not be null"); 158 } 159 if (str == null) { 160 return; 161 } 162 int sz; 163 sz = str.length(); 164 for (int i = 0; i < sz; i++) { 165 char ch = str.charAt(i); 166 167 if (ch > 0xfff) { 169 out.write("\\u" + hex(ch)); 170 } else if (ch > 0xff) { 171 out.write("\\u0" + hex(ch)); 172 } else if (ch > 0x7f) { 173 out.write("\\u00" + hex(ch)); 174 } else if (ch < 32) { 175 switch (ch) { 176 case '\b': 177 out.write('\\'); 178 out.write('b'); 179 break; 180 case '\n': 181 out.write('\\'); 182 out.write('n'); 183 break; 184 case '\t': 185 out.write('\\'); 186 out.write('t'); 187 break; 188 case '\f': 189 out.write('\\'); 190 out.write('f'); 191 break; 192 case '\r': 193 out.write('\\'); 194 out.write('r'); 195 break; 196 default : 197 if (ch > 0xf) { 198 out.write("\\u00" + hex(ch)); 199 } else { 200 out.write("\\u000" + hex(ch)); 201 } 202 break; 203 } 204 } else { 205 switch (ch) { 206 case '\'': 207 if (escapeSingleQuote) { 208 out.write('\\'); 209 } 210 out.write('\''); 211 break; 212 case '"': 213 out.write('\\'); 214 out.write('"'); 215 break; 216 case '\\': 217 out.write('\\'); 218 out.write('\\'); 219 break; 220 default : 221 out.write(ch); 222 break; 223 } 224 } 225 } 226 } 227 228 235 private static String hex(char ch) { 236 return Integer.toHexString(ch).toUpperCase(); 237 } 238 239 248 public static String unescapeJava(String str) { 249 if (str == null) { 250 return null; 251 } 252 try { 253 StringPrintWriter writer = new StringPrintWriter(str.length()); 254 unescapeJava(writer, str); 255 return writer.getString(); 256 } catch (IOException ioe) { 257 ioe.printStackTrace(); 259 return null; 260 } 261 } 262 263 278 public static void unescapeJava(Writer out, String str) throws IOException { 279 if (out == null) { 280 throw new IllegalArgumentException ("The Writer must not be null"); 281 } 282 if (str == null) { 283 return; 284 } 285 int sz = str.length(); 286 StringBuffer unicode = new StringBuffer (4); 287 boolean hadSlash = false; 288 boolean inUnicode = false; 289 for (int i = 0; i < sz; i++) { 290 char ch = str.charAt(i); 291 if (inUnicode) { 292 unicode.append(ch); 295 if (unicode.length() == 4) { 296 try { 299 int value = Integer.parseInt(unicode.toString(), 16); 300 out.write((char) value); 301 unicode.setLength(0); 302 inUnicode = false; 303 hadSlash = false; 304 } catch (NumberFormatException nfe) { 305 throw new NestableRuntimeException("Unable to parse unicode value: " + unicode, nfe); 306 } 307 } 308 continue; 309 } 310 if (hadSlash) { 311 hadSlash = false; 313 switch (ch) { 314 case '\\': 315 out.write('\\'); 316 break; 317 case '\'': 318 out.write('\''); 319 break; 320 case '\"': 321 out.write('"'); 322 break; 323 case 'r': 324 out.write('\r'); 325 break; 326 case 'f': 327 out.write('\f'); 328 break; 329 case 't': 330 out.write('\t'); 331 break; 332 case 'n': 333 out.write('\n'); 334 break; 335 case 'b': 336 out.write('\b'); 337 break; 338 case 'u': 339 { 340 inUnicode = true; 342 break; 343 } 344 default : 345 out.write(ch); 346 break; 347 } 348 continue; 349 } else if (ch == '\\') { 350 hadSlash = true; 351 continue; 352 } 353 out.write(ch); 354 } 355 if (hadSlash) { 356 out.write('\\'); 359 } 360 } 361 362 373 public static String unescapeJavaScript(String str) { 374 return unescapeJava(str); 375 } 376 377 393 public static void unescapeJavaScript(Writer out, String str) throws IOException { 394 unescapeJava(out, str); 395 } 396 397 423 public static String escapeHtml(String str) { 424 if (str == null) { 425 return null; 426 } 427 return Entities.HTML40.escape(str); 430 } 431 432 448 public static String unescapeHtml(String str) { 449 if (str == null) { 450 return null; 451 } 452 return Entities.HTML40.unescape(str); 453 } 454 455 469 public static String escapeXml(String str) { 470 if (str == null) { 471 return null; 472 } 473 return Entities.XML.escape(str); 474 } 475 476 488 public static String unescapeXml(String str) { 489 if (str == null) { 490 return null; 491 } 492 return Entities.XML.unescape(str); 493 } 494 495 513 public static String escapeSql(String str) { 514 if (str == null) { 515 return null; 516 } 517 return StringUtils.replace(str, "'", "''"); 518 } 519 520 } 521 522 | Popular Tags |