Java Text File Write writeString(StringBuilder out, CharSequence value, boolean escapeXML)

Here you can find the source of writeString(StringBuilder out, CharSequence value, boolean escapeXML)

Description

Encodes a String in JavaScript Object Notation.

License

Apache License

Parameter

Parameter Description
out the output buffer
value the String value

Declaration

static public void writeString(StringBuilder out, CharSequence value, boolean escapeXML) throws IOException 

Method Source Code

//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("&amp;");
            else
                out.append('&');
            break;
        case '<':
            if (escapeXML)
                out.append("&lt;");
            else
                out.append('<');
            break;
        case '>':
            if (escapeXML)
                out.append("&gt;");
            else
                out.append('>');
            break;

        // Double quote
        case '\"':
            if (escapeXML)
                out.append("\\&quot;");
            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;
        }
    }
}

Related

  1. writeString(String path, String str)
  2. writeString(String path, String str, boolean append)
  3. writeString(String s, File output, String encoding)
  4. writeString(String string, ObjectOutput out)
  5. writeString(String value)
  6. writeStringField(String name, String value, JsonGenerator jgen)
  7. writeStringFile(String FileName, String text)
  8. writeStringInto(String s, File outputFile)
  9. writeStringInto(String s, File outputFile)