Here you can find the source of writeString(StringBuilder out, CharSequence value, boolean escapeXML)
Parameter | Description |
---|---|
out | the output buffer |
value | the String value |
static public void writeString(StringBuilder out, CharSequence value, boolean escapeXML) throws IOException
//package com.java2s; //License from project: Apache License import java.io.IOException; public class Main { /**/* w w w.j a v a2s . c om*/ * Encodes a String in JavaScript Object Notation. * * @param out the output buffer * @param value the String value */ static public void writeString(StringBuilder out, CharSequence value, boolean escapeXML) throws IOException { if (value == null) { out.append("null"); } else { // escape chars as necessary out.append('\''); for (int i = 0; i < value.length(); i++) { char ch = value.charAt(i); _escapeChar(out, ch, escapeXML); } out.append('\''); } } /** * Encodes a char in JavaScript Object Notation. * * @param out the output buffer * @param value the char value */ static private void _escapeChar(StringBuilder out, char value, boolean escapeXML) { switch (value) { // Escapes needed for XML case '&': if (escapeXML) out.append("&"); else out.append('&'); break; case '<': if (escapeXML) out.append("<"); else out.append('<'); break; case '>': if (escapeXML) out.append(">"); else out.append('>'); break; // Double quote case '\"': if (escapeXML) out.append("\\""); else out.append("\\\""); break; // Apostrophe case '\'': out.append("\\\'"); break; // Backslash case '\\': out.append("\\\\"); break; case '\b': out.append("\\b"); break; case '\f': out.append("\\f"); break; case '\n': out.append("\\n"); break; case '\r': out.append("\\r"); break; case '\t': out.append("\\t"); break; default: if (value >= 32 && value < 128) { // no escaping necessary out.append(value); } else { String hex = Integer.toHexString(value); if (value > 0x00FF) { // use unicode escaping out.append("\\u"); if (value < 0x1000) out.append('0'); out.append(hex); } else { // use hex escaping out.append("\\x"); if (value < 0x10) out.append('0'); out.append(hex); } } break; } } }