/** * Does the given string need to be quoted? * @param str the string to check * @return does the string contain any of the active html characters? */ public static boolean needsQuoting(String str) { if (str == null) { return false; } byte[] bytes = str.getBytes(StandardCharsets.UTF_8); return needsQuoting(bytes, 0 , bytes.length); }
/** * Quote the given item to make it html-safe. * @param item the string to quote * @return the quoted string */ public static String quoteHtmlChars(String item) { if (item == null) { return null; } byte[] bytes = item.getBytes(StandardCharsets.UTF_8); if (needsQuoting(bytes, 0, bytes.length)) { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); try { quoteHtmlChars(buffer, bytes, 0, bytes.length); return buffer.toString("UTF-8"); } catch (IOException ioe) { // Won't happen, since it is a bytearrayoutputstream return null; } } else { return item; } }
/** * Does the given string need to be quoted? * @param str the string to check * @return does the string contain any of the active html characters? */ public static boolean needsQuoting(String str) { if (str == null) { return false; } byte[] bytes = str.getBytes(Charsets.UTF_8); return needsQuoting(bytes, 0 , bytes.length); }
/** * Does the given string need to be quoted? * @param str the string to check * @return does the string contain any of the active html characters? */ public static boolean needsQuoting(String str) { if (str == null) { return false; } byte[] bytes = str.getBytes(StandardCharsets.UTF_8); return needsQuoting(bytes, 0 , bytes.length); }
/** * Does the given string need to be quoted? * @param str the string to check * @return does the string contain any of the active html characters? */ public static boolean needsQuoting(String str) { if (str == null) { return false; } byte[] bytes = str.getBytes(Charsets.UTF_8); return needsQuoting(bytes, 0 , bytes.length); }
/** * Does the given string need to be quoted? * @param str the string to check * @return does the string contain any of the active html characters? */ public static boolean needsQuoting(String str) { if (str == null) { return false; } byte[] bytes = str.getBytes(); return needsQuoting(bytes, 0 , bytes.length); }
/** * Does the given string need to be quoted? * @param str the string to check * @return does the string contain any of the active html characters? */ public static boolean needsQuoting(String str) { if (str == null) { return false; } byte[] bytes = str.getBytes(Charsets.UTF_8); return needsQuoting(bytes, 0 , bytes.length); }
@Test public void testNeedsQuoting() throws Exception { assertTrue(HtmlQuoting.needsQuoting("abcde>")); assertTrue(HtmlQuoting.needsQuoting("<abcde")); assertTrue(HtmlQuoting.needsQuoting("abc'de")); assertTrue(HtmlQuoting.needsQuoting("abcde\"")); assertTrue(HtmlQuoting.needsQuoting("&")); assertFalse(HtmlQuoting.needsQuoting("")); assertFalse(HtmlQuoting.needsQuoting("ab\ncdef")); assertFalse(HtmlQuoting.needsQuoting(null)); }
/** * Quote the given item to make it html-safe. * @param item the string to quote * @return the quoted string */ public static String quoteHtmlChars(String item) { if (item == null) { return null; } byte[] bytes = item.getBytes(); if (needsQuoting(bytes, 0, bytes.length)) { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); try { quoteHtmlChars(buffer, bytes, 0, bytes.length); } catch (IOException ioe) { // Won't happen, since it is a bytearrayoutputstream } return buffer.toString(); } else { return item; } }
/** * Quote the given item to make it html-safe. * @param item the string to quote * @return the quoted string */ public static String quoteHtmlChars(String item) { if (item == null) { return null; } byte[] bytes = item.getBytes(Charsets.UTF_8); if (needsQuoting(bytes, 0, bytes.length)) { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); try { quoteHtmlChars(buffer, bytes, 0, bytes.length); return buffer.toString("UTF-8"); } catch (IOException ioe) { // Won't happen, since it is a bytearrayoutputstream return null; } } else { return item; } }
/** * Quote the given item to make it html-safe. * @param item the string to quote * @return the quoted string */ public static String quoteHtmlChars(String item) { if (item == null) { return null; } byte[] bytes = item.getBytes(Charsets.UTF_8); if (needsQuoting(bytes, 0, bytes.length)) { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); try { quoteHtmlChars(buffer, bytes, 0, bytes.length); return buffer.toString("UTF-8"); } catch (IOException ioe) { // Won't happen, since it is a bytearrayoutputstream return null; } } else { return item; } }
/** * Quote the given item to make it html-safe. * @param item the string to quote * @return the quoted string */ public static String quoteHtmlChars(String item) { if (item == null) { return null; } byte[] bytes = item.getBytes(StandardCharsets.UTF_8); if (needsQuoting(bytes, 0, bytes.length)) { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); try { quoteHtmlChars(buffer, bytes, 0, bytes.length); return buffer.toString("UTF-8"); } catch (IOException ioe) { // Won't happen, since it is a bytearrayoutputstream return null; } } else { return item; } }
/** * Quote the given item to make it html-safe. * @param item the string to quote * @return the quoted string */ public static String quoteHtmlChars(String item) { if (item == null) { return null; } byte[] bytes = item.getBytes(Charsets.UTF_8); if (needsQuoting(bytes, 0, bytes.length)) { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); try { quoteHtmlChars(buffer, bytes, 0, bytes.length); return buffer.toString("UTF-8"); } catch (IOException ioe) { // Won't happen, since it is a bytearrayoutputstream return null; } } else { return item; } }
@Test public void testNeedsQuoting() throws Exception { assertTrue(HtmlQuoting.needsQuoting("abcde>")); assertTrue(HtmlQuoting.needsQuoting("<abcde")); assertTrue(HtmlQuoting.needsQuoting("abc'de")); assertTrue(HtmlQuoting.needsQuoting("abcde\"")); assertTrue(HtmlQuoting.needsQuoting("&")); assertFalse(HtmlQuoting.needsQuoting("")); assertFalse(HtmlQuoting.needsQuoting("ab\ncdef")); assertFalse(HtmlQuoting.needsQuoting(null)); }
@Test public void testNeedsQuoting() throws Exception { assertTrue(HtmlQuoting.needsQuoting("abcde>")); assertTrue(HtmlQuoting.needsQuoting("<abcde")); assertTrue(HtmlQuoting.needsQuoting("abc'de")); assertTrue(HtmlQuoting.needsQuoting("abcde\"")); assertTrue(HtmlQuoting.needsQuoting("&")); assertFalse(HtmlQuoting.needsQuoting("")); assertFalse(HtmlQuoting.needsQuoting("ab\ncdef")); assertFalse(HtmlQuoting.needsQuoting(null)); }