Example usage for org.apache.commons.lang3.text.translate CharSequenceTranslator hex

List of usage examples for org.apache.commons.lang3.text.translate CharSequenceTranslator hex

Introduction

In this page you can find the example usage for org.apache.commons.lang3.text.translate CharSequenceTranslator hex.

Prototype

public static String hex(final int codepoint) 

Source Link

Document

Returns an upper case hexadecimal String for the given character.

Usage

From source file:brut.androlib.meta.YamlStringEscapeUtils.java

/**
 * @param out write to receieve the escaped string
 * @param str String to escape values in, may be null
 * @param escapeSingleQuote escapes single quotes if <code>true</code>
 * @param escapeForwardSlash TODO//from  ww  w.ja  va 2  s  .co m
 * @throws IOException if an IOException occurs
 */
private static void escapeJavaStyleString(Writer out, String str, boolean escapeSingleQuote,
        boolean escapeForwardSlash) throws IOException {
    if (out == null) {
        throw new IllegalArgumentException("The Writer must not be null");
    }
    if (str == null) {
        return;
    }
    int sz;
    sz = str.length();
    for (int i = 0; i < sz; i++) {
        char ch = str.charAt(i);
        // "[^\t\n\r\u0020-\u007E\u0085\u00A0-\uD7FF\uE000-\uFFFD]"
        // handle unicode
        if (ch > 0xFFFD) {
            out.write("\\u" + CharSequenceTranslator.hex(ch));
        } else if (ch > 0xD7FF && ch < 0xE000) {
            out.write("\\u" + CharSequenceTranslator.hex(ch));
        } else if (ch > 0x7E && ch != 0x85 && ch < 0xA0) {
            out.write("\\u00" + CharSequenceTranslator.hex(ch));
        } else if (ch < 32) {
            switch (ch) {
            case '\t':
                out.write('\\');
                out.write('t');
                break;
            case '\n':
                out.write('\\');
                out.write('n');
                break;
            case '\r':
                out.write('\\');
                out.write('r');
                break;
            default:
                if (ch > 0xf) {
                    out.write("\\u00" + CharSequenceTranslator.hex(ch));
                } else {
                    out.write("\\u000" + CharSequenceTranslator.hex(ch));
                }
                break;
            }
        } else {
            switch (ch) {
            case '\'':
                if (escapeSingleQuote) {
                    out.write('\\');
                }
                out.write('\'');
                break;
            case '"':
                out.write('\\');
                out.write('"');
                break;
            case '\\':
                out.write('\\');
                out.write('\\');
                break;
            case '/':
                if (escapeForwardSlash) {
                    out.write('\\');
                }
                out.write('/');
                break;
            default:
                out.write(ch);
                break;
            }
        }
    }
}