String (@Nullable String str, final boolean escapeWhiteSpace, final boolean convertNoBreakSpace) { if (str == null) { return null; } StringBuilder buffer = null; for (int i = 0; i < str.length(); i++) { String entity; char ch = str.charAt(i); switch (ch) { case '\n': entity = escapeWhiteSpace ? "&#10;" : null; break; case '\r': entity = escapeWhiteSpace ? "&#13;" : null; break; case '\t': entity = escapeWhiteSpace ? "&#9;" : null; break; case '\"': entity = QUOT; break; case '<': entity = LT; break; case '>': entity = GT; break; case '&': entity = AMP; break; case 160: // unicode char for &nbsp; entity = convertNoBreakSpace ? NBSP : null; break; default: entity = null; break; } buffer = appendEscapedSymbol(str, buffer, i, entity, ch); } // If there were any entities, return the escaped characters // that we put in the StringBuffer. Otherwise, just return // the unmodified input string. return buffer == null ? str : buffer.toString(); }