Java Nashorn Javascript Library escape(Object value)

Here you can find the source of escape(Object value)

Description

escape

License

LGPL

Declaration

static public String escape(Object value) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.util.Arrays;
import java.util.List;

import java.util.stream.Collectors;

public class Main {
    static public String escape(Object value) {
        if (value == null) {
            return "NULL";
        }//from w  w  w . j  a va 2 s.  co m
        if (Number.class.isAssignableFrom(value.getClass())) {
            return value.toString();
        }
        if (Boolean.class.isAssignableFrom(value.getClass())) {
            return (boolean) value ? "1" : "0";
        }
        if (jdk.nashorn.api.scripting.ScriptObjectMirror.class.isAssignableFrom(value.getClass())) {
            value = jdk.nashorn.api.scripting.ScriptUtils.convert(value, Object[].class);
        }
        if (List.class.isAssignableFrom(value.getClass())) {
            value = ((List) value).toArray();
        }
        if (value.getClass().isArray()) {
            String csv = Arrays.stream((Object[]) value).distinct().map(x -> escape(x))
                    .collect(Collectors.joining(","));
            if (csv == null || csv.equals("")) {
                return "null";
            }
            return csv;
        }
        // else
        return "'" + value.toString().replace("'", "''") + "'";
    }
}

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. encode(final Object self, final String string, final boolean component)
  5. error(final String string, final int index)
  6. getArrayLength(NativeArray arr)
  7. isArrayType(final Object value)
  8. isDateType(final ScriptObjectMirror value)
  9. isNashornScript(final String className)