/** * We initialize our run and options functions here. * @throws ScriptException If no run function exists. */ @Override public void postInit() throws ScriptException { Object run = scope.get("run", scope); Object inputs = scope.get("inputs", scope); if (run instanceof Function) this.run = (Function)run; else throw new ScriptException(name + " must define a run function"); if (inputs instanceof NativeJavaArray) { try { this.inputs = (Setting[]) ((NativeJavaArray)inputs).unwrap(); } catch(ClassCastException e) { throw new ScriptException(e); } } }
public static String toString(Object ob) { String parameterToString; if (ob != null) { if (ob instanceof NativeObject) { NativeObject nativeObject = (NativeObject) ob; parameterToString = nativeToString(nativeObject); } else if (ob instanceof NativeJavaObject) { NativeJavaObject nativeJavaObject = (NativeJavaObject) ob; parameterToString = toString(nativeJavaObject.unwrap()); } else if (ob instanceof NativeJavaArray || ob instanceof NativeArray || ob.getClass().isArray() || ob instanceof Collection<?>) { parameterToString = toStringList(ob).toString(); } else if (ob instanceof NodeList) { parameterToString = ""; NodeList nl = (NodeList) ob; for (int i = 0; i < nl.getLength(); i++) { parameterToString += nodeToString(nl.item(i)); } } else if (ob instanceof Node) { parameterToString = nodeToString((Node)ob); } else { parameterToString = ob.toString(); } } else { parameterToString = null; } return parameterToString; }
public static List<String> toStringList(Object ob) { List<String> list; if (ob != null) { if (ob instanceof NativeJavaObject) { NativeJavaObject nativeJavaObject = (NativeJavaObject) ob; list = toStringList(nativeJavaObject.unwrap()); } else if (ob instanceof NativeJavaArray) { Object object = ((NativeJavaArray) ob).unwrap(); list = toStringList(object); } else if (ob.getClass().isArray()) { list = toStringList(Arrays.asList((Object[]) ob)); } else if (ob instanceof NativeArray) { NativeArray array = (NativeArray) ob; list = new ArrayList<String>((int) array.getLength()); for (java.util.Iterator<?> i = array.iterator(); i.hasNext();) { list.add(toString(i.next())); } } else if (ob instanceof Collection<?>) { Collection<?> collection = GenericUtils.cast(ob); list = new ArrayList<String>(collection.size()); for (Object o : collection) { list.add(toString(o)); } } else { list = Arrays.asList(toString(ob)); } } else { list = Collections.emptyList(); } return list; }
private void init() { if ((evaluated != null) && (list == null)) { if (evaluated instanceof NodeList) { list = new ArrayList<Object>(); NodeList nodeList = (NodeList)evaluated; for (int i=0; i<nodeList.getLength(); i++) list.add(nodeList.item(i)); } else if (evaluated instanceof Collection<?>) { list = new ArrayList<Object>((Collection<?>) evaluated); } else if (evaluated instanceof NativeJavaArray) { Object object = ((NativeJavaArray)evaluated).unwrap(); list = Arrays.asList((Object[])object); } else if (evaluated instanceof NativeArray) { list = new ArrayList<Object>(); NativeArray array = (NativeArray)evaluated; for (int i=0; i<array.getLength(); i++) list.add(array.get(i,array)); } // else if (evaluated instanceof NativeJavaObject) { // list = Arrays.asList(new String[] {(String) ((NativeJavaObject)evaluated).getDefaultValue(String.class)}); // } else if (evaluated.getClass().isArray()) { list = Arrays.asList((Object[])evaluated); } else list = Arrays.asList(new Object[] {evaluated.toString()}); } }
private static String toString(Object obj) { if (obj == null) { return "null"; } if (obj instanceof String) { return (String) obj; } if (obj instanceof NativeJavaArray) { Object array = ((NativeJavaArray) obj).unwrap(); int len = Array.getLength(array); StringBuilder sb = new StringBuilder("["); for (int i = 0; i < len; i++) { if (i != 0) { sb.append(","); } sb.append(toString(Array.get(array, i))); } return sb.append("]").toString(); } if (obj instanceof BaseFunction) { String funcName = ((BaseFunction) obj).getFunctionName(); if (StringUtils.isEmpty(funcName)) { return "function()"; } return "function " + funcName + "()"; } String str = ScriptRuntime.toString(obj); if (obj instanceof NativeArray) { return "[" + str + "]"; } return str; }