Java Nashorn Javascript Library encode(final Object self, final String string, final boolean component)

Here you can find the source of encode(final Object self, final String string, final boolean component)

Description

encode

License

Open Source License

Declaration

private static String encode(final Object self, final String string,
            final boolean component) 

Method Source Code

//package com.java2s;
/*//from  w ww .  j  a v  a 2 s  . c  o m
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import static jdk.nashorn.internal.runtime.ECMAErrors.uriError;

public class Main {
    private static final String URI_UNESCAPED_NONALPHANUMERIC = "-_.!~*'()";
    private static final String URI_RESERVED = ";/?:@&=+$,#";

    private static String encode(final Object self, final String string,
            final boolean component) {
        if (string.isEmpty()) {
            return string;
        }

        final int len = string.length();
        final StringBuilder sb = new StringBuilder();

        for (int k = 0; k < len; k++) {
            final char C = string.charAt(k);
            if (isUnescaped(C, component)) {
                sb.append(C);
                continue;
            }

            if (C >= 0xDC00 && C <= 0xDFFF) {
                return error(string, k);
            }

            int V;
            if (C < 0xD800 || C > 0xDBFF) {
                V = C;
            } else {
                k++;
                if (k == len) {
                    return error(string, k);
                }

                final char kChar = string.charAt(k);
                if (kChar < 0xDC00 || kChar > 0xDFFF) {
                    return error(string, k);
                }
                V = ((C - 0xD800) * 0x400 + (kChar - 0xDC00) + 0x10000);
            }

            try {
                sb.append(toHexEscape(V));
            } catch (final Exception e) {
                throw uriError(e, "bad.uri", string, Integer.toString(k));
            }
        }

        return sb.toString();
    }

    private static boolean isUnescaped(final char ch,
            final boolean component) {
        if (('A' <= ch && ch <= 'Z') || ('a' <= ch && ch <= 'z')
                || ('0' <= ch && ch <= '9')) {
            return true;
        }

        if (URI_UNESCAPED_NONALPHANUMERIC.indexOf(ch) >= 0) {
            return true;
        }

        if (!component) {
            return URI_RESERVED.indexOf(ch) >= 0;
        }

        return false;
    }

    private static String error(final String string, final int index) {
        throw uriError("bad.uri", string, Integer.toString(index));
    }

    private static String toHexEscape(final int u0) {
        int u = u0;
        int len;
        final byte[] b = new byte[6];

        if (u <= 0x7f) {
            b[0] = (byte) u;
            len = 1;
        } else {
            // > 0x7ff -> length 2
            // > 0xffff -> length 3
            // and so on. each new length is an additional 5 bits from the
            // original 11
            // the final mask is 8-len zeros in the low part.
            len = 2;
            for (int mask = u >>> 11; mask != 0; mask >>>= 5) {
                len++;
            }
            for (int i = len - 1; i > 0; i--) {
                b[i] = (byte) (0x80 | (u & 0x3f));
                u >>>= 6; // 64 bits per octet.
            }

            b[0] = (byte) (~((1 << (8 - len)) - 1) | u);
        }

        final StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len; i++) {
            sb.append('%');
            if ((b[i] & 0xff) < 0x10) {
                sb.append('0');
            }
            sb.append(Integer.toHexString(b[i] & 0xff).toUpperCase());
        }

        return sb.toString();
    }
}

Related

  1. addToArray(final Object array, final Object value)
  2. addToNativeObject(final Object object, final String key, final Object value)
  3. decode(final Object self, final String string, final boolean component)
  4. error(final String string, final int index)
  5. escape(Object value)
  6. getArrayLength(NativeArray arr)
  7. isArrayType(final Object value)