Here you can find the source of quoteHtmlContent(String x)
Parameter | Description |
---|---|
x | string to quote |
static public String quoteHtmlContent(String x)
//package com.java2s; public class Main { private static final char[] htmlIn = { '&', '"', '\'', '<', '>', '\n' }; private static final String[] htmlOut = { "&", """, "'", "<", ">", "\n<p>" }; /**/*from w ww.j a va 2 s. c o m*/ * Replace special characters with entities for HTML content. * special: '&', '"', '\'', '<', '>', '\n' * * @param x string to quote * @return equivilent string using entities for any special chars */ static public String quoteHtmlContent(String x) { return replace(x, htmlIn, htmlOut); } /** * Replace any char "out" in s with "in". * * @param s string to replace * @param out repalce this character * @param in with this string * @return modified string if needed */ static public String replace(String s, char out, String in) { if (s.indexOf(out) < 0) { return s; } // gotta do it StringBuilder sb = new StringBuilder(s); replace(sb, out, in); return sb.toString(); } /** * Replace all ocurrences of any char in replaceChar with corresponding String in replaceWith * * @param x operate on this string * @param replaceChar get rid of these * @param replaceWith replace with these * @return resulting string */ static public String replace(String x, char[] replaceChar, String[] replaceWith) { // common case no replacement boolean ok = true; for (int i = 0; i < replaceChar.length; i++) { int pos = x.indexOf(replaceChar[i]); ok = (pos < 0); if (!ok) { break; } } if (ok) { return x; } // gotta do it StringBuilder sb = new StringBuilder(x); for (int i = 0; i < replaceChar.length; i++) { int pos = x.indexOf(replaceChar[i]); if (pos >= 0) { replace(sb, replaceChar[i], replaceWith[i]); } } return sb.toString(); } /** * Replaces all occurrences of "pattern" in "string" with "value" * * @param string string to munge * @param pattern pattern to replace * @param value replacement value * @return munged string */ public static String replace(String string, String pattern, String value) { if (pattern.length() == 0) return string; StringBuilder returnValue = new StringBuilder(); int patternLength = pattern.length(); while (true) { int idx = string.indexOf(pattern); if (idx < 0) { break; } returnValue.append(string.substring(0, idx)); if (value != null) { returnValue.append(value); } string = string.substring(idx + patternLength); } returnValue.append(string); return returnValue.toString(); } /** * Replace any char "out" in sb with String "in". * * @param sb StringBuilder to replace * @param out repalce this character * @param in with this string */ static public void replace(StringBuilder sb, char out, String in) { for (int i = 0; i < sb.length(); i++) { if (sb.charAt(i) == out) { sb.replace(i, i + 1, in); i += in.length() - 1; } } } /** * Replace any of the characters from out with corresponding character from in * * @param sb the StringBuilder * @param out get rid of any of these characters * @param in replacing with the character at same index */ static public void replace(StringBuilder sb, String out, String in) { for (int i = 0; i < sb.length(); i++) { int c = sb.charAt(i); for (int j = 0; j < out.length(); j++) { if (out.charAt(j) == c) sb.setCharAt(i, in.charAt(j)); } } } }