StringEscapeUtil { public static String escapeURL(String str) { try { return escapeURLInternal(str, null, true); } catch (UnsupportedEncodingException e) { return str; } }  static String escapeJava(String str); static String escapeJava(String str, boolean strict); static void escapeJava(String str, Appendable out); static void escapeJava(String str, Appendable out, boolean strict); static String escapeJavaScript(String str); static String escapeJavaScript(String str, boolean strict); static void escapeJavaScript(String str, Appendable out); static void escapeJavaScript(String str, Appendable out, boolean strict); static String unescapeJava(String str); static void unescapeJava(String str, Appendable out); static String unescapeJavaScript(String str); static void unescapeJavaScript(String str, Appendable out); static String escapeHtml(String str); static void escapeHtml(String str, Appendable out); static String escapeXml(String str); static void escapeXml(String str, Appendable out); static String escapeEntities(Entities entities, String str); static void escapeEntities(Entities entities, String str, Appendable out); static String unescapeHtml(String str); static void unescapeHtml(String str, Appendable out); static String unescapeXml(String str); static void unescapeXml(String str, Appendable out); static String unescapeEntities(Entities entities, String str); static void unescapeEntities(Entities entities, String str, Appendable out); static String escapeSql(String str); static void escapeSql(String str, Appendable out); static String escapeURL(String str); static String escapeURL(String str, String encoding); static String escapeURL(String str, String encoding, boolean strict); static void escapeURL(String str, String encoding, Appendable out); static void escapeURL(String str, String encoding, Appendable out, boolean strict); static String unescapeURL(String str); static String unescapeURL(String str, String encoding); static void unescapeURL(String str, String encoding, Appendable out);  }
@Test public void escapeURLStrict() throws Exception { String unreserved = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.!~*'()"; assertSame(unreserved, StringEscapeUtil.escapeURL(unreserved)); StringBuilder buffer = new StringBuilder(256); StringBuilder expectedBuffer = new StringBuilder(256 * 3); for (int i = 0; i < 256; i++) { buffer.append((char) i); if (i == ' ') { expectedBuffer.append('+'); } else if (unreserved.indexOf(i) == -1) { new Formatter(expectedBuffer).format("%%%02X", i); } else { expectedBuffer.append((char) i); } } String str = buffer.toString(); String expectedStr = expectedBuffer.toString(); assertEquals(expectedStr, StringEscapeUtil.escapeURL(str, "8859_1")); StringWriter writer = new StringWriter(); StringEscapeUtil.escapeURL(str, "8859_1", writer); assertEquals(expectedStr, writer.toString()); assertEquals("%D6%D0%BB%AA%C8%CB%C3%F1%B9%B2%BA%CD%B9%FA", StringEscapeUtil.escapeURL("中华人民共和国", "GBK")); writer = new StringWriter(); StringEscapeUtil.escapeURL("中华人民共和国", "GBK", writer); assertEquals("%D6%D0%BB%AA%C8%CB%C3%F1%B9%B2%BA%CD%B9%FA", writer.toString()); }
@Test public void escapeURLLoose() throws Exception { String reserved = ";/?:@&=+$,"; assertEquals("%3B%2F%3F%3A%40%26%3D%2B%24%2C", StringEscapeUtil.escapeURL(reserved, "8859_1", false)); StringBuilder buffer = new StringBuilder(256); StringBuilder expectedBuffer = new StringBuilder(256 * 3); for (int i = 0; i < 256; i++) { buffer.append((char) i); if (i == ' ') { expectedBuffer.append('+'); } else if (reserved.indexOf(i) == -1 && i > 32) { expectedBuffer.append((char) i); } else { new Formatter(expectedBuffer).format("%%%02X", i); } } String str = buffer.toString(); String expectedStr = expectedBuffer.toString(); assertEquals(expectedStr, StringEscapeUtil.escapeURL(str, "8859_1", false)); StringWriter writer = new StringWriter(); StringEscapeUtil.escapeURL(str, "8859_1", writer, false); assertEquals(expectedStr, writer.toString()); str = "中华人民共和国"; assertSame(str, StringEscapeUtil.escapeURL(str, "GBK", false)); str = "中华人民共和国\u3000"; assertEquals("中华人民共和国%A1%A1", StringEscapeUtil.escapeURL(str, "GBK", false)); writer = new StringWriter(); StringEscapeUtil.escapeURL("中华人民共和国", "GBK", writer, false); assertEquals("中华人民共和国", writer.toString()); }
@Test public void escapeURLEncoding() { LocaleUtil.setContext(Locale.CHINA, "GBK"); assertEquals("%D6%D0%BB%AA%C8%CB%C3%F1%B9%B2%BA%CD%B9%FA", StringEscapeUtil.escapeURL("中华人民共和国")); LocaleUtil.setContext(Locale.US, "8859_1"); assertEquals("%3F%3F%3F%3F%3F%3F%3F", StringEscapeUtil.escapeURL("中华人民共和国")); LocaleUtil.resetContext(); }