All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ajax4jsf.framework.util.javascript.JSEncoder Maven / Gradle / Ivy

/**
 * Copyright 2004 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.ajax4jsf.framework.util.javascript;

/**
 * @author shura
 * Encode chars as JavaScript sequences
 */
public class JSEncoder {
    private char APOSTROPHE[] = { '\\', '\'' };

    private static final char ENCODE_HEX[] = "0123456789ABCDEF".toCharArray();

    private static final char ENCODE_APOS[] = "\\'".toCharArray();

    private static final char ENCODE_QUOT[] = "\\\"".toCharArray();

    private static final char ENCODE_CR[] = "\\r".toCharArray();

    private static final char ENCODE_LF[] = "\\n".toCharArray();

    private static final char ENCODE_FF[] = "\\f".toCharArray();

    private static final char ENCODE_TAB[] = "\\t".toCharArray();

    private static final char ENCODE_BS[] = "\\\\".toCharArray();

    private static final char ENCODE_ESC[] = "\\e".toCharArray();

    /**
     * Create a new instance of this XMLEncoder.
     */
    public JSEncoder() {
    }

    /**
     * Return true or false wether this encoding can encode the specified
     * character or not.
     * 

* This method will return true for the following character range:
* * #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] * *

* * @see W3C XML 1.0 */ public boolean compile(char c) { if ((c == 0x09) || // [\t] (c == 0x0a) || // [\n] (c == 0x0d) || // [\r](c == 0x22) || // ["] (c == 0x22) || // ["] (c == 0x27) || // ['] (c == 0x5c) || // [\] (c == 0x03) || // [esc] (c < 0x20) || // See ((c > 0xd7ff) && (c < 0xe000)) || (c > 0xfffd)|| (c > 0xff)) { return (false); } return (true); } /** * Return an array of characters representing the encoding for the specified * character. */ public char[] encode(char c) { switch (c) { case 0x03: return (ENCODE_FF); // (>) [>] case 0x09: return (ENCODE_TAB); // (>) [>] case 0x0a: return (ENCODE_LF); // (>) [>] case 0x0d: return (ENCODE_CR); // (>) [>] case 0x22: return (ENCODE_QUOT); // (") ["] case 0x27: return (ENCODE_APOS); // (') ['] case 0x5c: return (ENCODE_BS); // (<) [<] default: { if (c > 0xff) { char ret[] = { '\\', 'u', ENCODE_HEX[c >> 0xc & 0xf], ENCODE_HEX[c >> 0x8 & 0xf], ENCODE_HEX[c >> 0x4 & 0xf], ENCODE_HEX[c & 0xf] }; return (ret); } char ret[] = { '\\', 'x', ENCODE_HEX[c >> 0x4 & 0xf], ENCODE_HEX[c & 0xf] }; return (ret); } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy