Java Java String Format toJavascriptArray(String[][] Vals)

Here you can find the source of toJavascriptArray(String[][] Vals)

Description

to Javascript Array

License

Apache License

Declaration

public static String toJavascriptArray(String[][] Vals) 

Method Source Code

//package com.java2s;
/* ===========================================================================
 * Copyright (C) 2015 CapsicoHealth Inc.
 *
 * 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.//from  w w w  . j  ava  2  s .  c  o m
 */

public class Main {
    public static String toJavascriptArray(String[][] Vals) {
        StringBuilder Str = new StringBuilder();
        return toJavascriptArray(Str, Vals).toString();
    }

    public static StringBuilder toJavascriptArray(StringBuilder Str, String[][] Vals) {
        boolean First = true;
        for (String[] v : Vals) {
            if (First == true)
                First = false;
            else
                Str.append(",");
            Str.append("[");
            toJavascriptArray(Str, v);
            Str.append("]");
        }
        return Str;
    }

    public static String toJavascriptArray(String[] Vals) {
        StringBuilder Str = new StringBuilder();
        return toJavascriptArray(Str, Vals).toString();
    }

    public static StringBuilder toJavascriptArray(StringBuilder Str, String[] Vals) {
        boolean First = true;
        for (String v : Vals) {
            if (First == true)
                First = false;
            else
                Str.append(",");
            Str.append(EscapeJavaScriptQuotes(v));
        }
        return Str;
    }

    public static final String EscapeJavaScriptQuotes(String S) {
        return EscapeHTMLJavaScriptQuotes(S, "'");
    }

    public static final String EscapeHTMLJavaScriptQuotes(String S) {
        return EscapeHTMLJavaScriptQuotes(S, "");
    }

    public static final String EscapeHTMLJavaScriptQuotes(String S, String BeforeAfter) {
        if (S == null)
            return BeforeAfter + BeforeAfter;
        StringBuilder X = new StringBuilder(128);
        X.append(BeforeAfter);

        int len = S.length();
        char[] s = S.toCharArray();
        int i = 0;
        int j = 0;
        while (j < len && s[j] != '"' && s[j] != '\'' && s[j] != '\n' && s[j] != '\r' && s[j] != '\\'
                && s[j] != '<')
            ++j;
        if (j >= len) {
            X.append(S);
            X.append(BeforeAfter);
            return X.toString();
        } else
            do {
                char c = s[j];
                X.append(s, i, j - i).append(c == '"' ? "&quot;"
                        : c == '\'' ? "&#39;" : c == '\n' ? "\\n" : c == '\\' ? "\\\\" : c == '<' ? "&lt;" : "");
                i = ++j;
                while (j < len && s[j] != '"' && s[j] != '\'' && s[j] != '\n' && s[j] != '\r' && s[j] != '\\'
                        && s[j] != '<')
                    ++j;
            } while (j < len);

        X.append(s, i, len - i);
        X.append(BeforeAfter);
        return X.toString();
    }
}

Related

  1. toJavaName(String name)
  2. toJavaPackageName(String name)
  3. toJavaPath(String path)
  4. toJavaPath(String path)
  5. toJavaPattern(String pattern)
  6. toJavaScriptEncode(String string)
  7. toJavaScriptSafeString(String content)
  8. toJavaSecs(long secs)
  9. toJavaSourceType(String type)