Example usage for com.google.gwt.user.server.rpc.impl CharVector add

List of usage examples for com.google.gwt.user.server.rpc.impl CharVector add

Introduction

In this page you can find the example usage for com.google.gwt.user.server.rpc.impl CharVector add.

Prototype

public void add(char ch) 

Source Link

Usage

From source file:com.foo.server.rpc230.ServerSerializationStreamWriterSenasa.java

License:Apache License

/**
 * This method takes a string and outputs a JavaScript string literal. The
 * data is surrounded with quotes, and any contained characters that need to
 * be escaped are mapped onto their escape sequence.
 * //from   ww w  .  ja  v  a2  s . c o  m
 * Assumptions: We are targeting a version of JavaScript that that is later
 * than 1.3 that supports unicode strings.
 */
public static String escapeString(String toEscape) {
    // make output big enough to escape every character (plus the quotes)
    char[] input = toEscape.toCharArray();
    CharVector charVector = new CharVector(input.length * 2 + 2, input.length);

    charVector.add(JS_QUOTE_CHAR);

    for (int i = 0, n = input.length; i < n; ++i) {
        char c = input[i];
        if (needsUnicodeEscape(c)) {
            unicodeEscape(c, charVector);
        } else {
            charVector.add(c);
        }
    }

    charVector.add(JS_QUOTE_CHAR);
    return String.valueOf(charVector.asArray(), 0, charVector.getSize());
}

From source file:com.foo.server.rpc230.ServerSerializationStreamWriterSenasa.java

License:Apache License

/**
 * Writes a safe escape sequence for a character. Some characters have a
 * short form, such as \n for U+000D, while others are represented as \\xNN
 * or \\uNNNN.//from  ww w. ja v  a  2  s  .co m
 * 
 * @param ch
 *            character to unicode escape
 * @param charVector
 *            char vector to receive the unicode escaped representation
 */
private static void unicodeEscape(char ch, CharVector charVector) {
    charVector.add(JS_ESCAPE_CHAR);
    if (ch < NUMBER_OF_JS_ESCAPED_CHARS && JS_CHARS_ESCAPED[ch] != 0) {
        charVector.add(JS_CHARS_ESCAPED[ch]);
    } else if (ch < 256) {
        charVector.add('x');
        charVector.add(NIBBLE_TO_HEX_CHAR[(ch >> 4) & 0x0F]);
        charVector.add(NIBBLE_TO_HEX_CHAR[ch & 0x0F]);
    } else {
        charVector.add('u');
        charVector.add(NIBBLE_TO_HEX_CHAR[(ch >> 12) & 0x0F]);
        charVector.add(NIBBLE_TO_HEX_CHAR[(ch >> 8) & 0x0F]);
        charVector.add(NIBBLE_TO_HEX_CHAR[(ch >> 4) & 0x0F]);
        charVector.add(NIBBLE_TO_HEX_CHAR[ch & 0x0F]);
    }
}