List of usage examples for jdk.nashorn.api.scripting ScriptObjectMirror size
@Override
public int size()
From source file:JavaScriptTest.java
public static String serialize(Object obj) { StringBuilder ret = new StringBuilder(); if (obj instanceof ScriptObjectMirror) { ScriptObjectMirror om = (ScriptObjectMirror) obj; //System.out.println(om+" isArray "+om.isArray()); //System.out.println(om+" isEmpty "+om.isEmpty()); //System.out.println(om+" isExtensible "+om.isExtensible()); //System.out.println(om+" isFrozen "+om.isFrozen()); //System.out.println(om+" isFunction "+om.isFunction()); //System.out.println(om+" isSealed "+om.isSealed()); //System.out.println(om+" isStrictFunction "+om.isStrictFunction()); //System.out.println(om+" getOwnKeys "+Arrays.asList(om.getOwnKeys(true))); if (om.isFunction()) { ret.append(om.toString());//from w ww. j a v a 2 s. c o m } else if (om.isArray()) { ret.append("["); //ret.append("isArray:"+om.toString()); for (int x = 0; x < om.size(); x++) { Object o = om.getSlot(x); ret.append(serialize(o)); if (x + 1 < om.size()) { ret.append(","); } } ret.append("]"); } else if (om.toString().indexOf("global") > -1) { Iterator<Map.Entry<String, Object>> it = om.entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, Object> entry = it.next(); ret.append("var " + entry.getKey() + "=" + serialize(entry.getValue()) + ";\n"); } } else { ret.append("{"); Iterator<Map.Entry<String, Object>> it = om.entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, Object> entry = it.next(); ret.append(entry.getKey() + ":" + serialize(entry.getValue())); if (it.hasNext()) { ret.append(","); } } ret.append("}"); } } else if (obj instanceof String) { ret.append("\"" + obj + "\""); } else { ret.append(obj); } return ret.toString(); }
From source file:com.nordea.oss.copybook.codegen.CopyBookConverter.java
License:MIT License
public List<String> convert(String copybookString, String packageName, String rootClassName, String accessor, String charset, String subClassHandling, String wrapperClassName) throws Exception { ScriptObjectMirror results = (ScriptObjectMirror) invocable.invokeFunction("convertCopybook", packageName, rootClassName, copybookString, accessor, charset, subClassHandling, wrapperClassName); return Arrays.asList(results.values().toArray(new String[results.size()])); }
From source file:com.qwazr.library.process.ProcessTool.java
License:Apache License
/** * Call this command from Javascript to execute a local binary. * <pre>/*from ww w . j av a2 s . c o m*/ * {@code * { * "working_directory": "/path/to/directory", * "environment_variables": { * "lang": "de", * "hello": "world" * }, * "commands": ["convert", "my.jpg", "my.gif"], * "output_file": "/path/to/out.log", * "error_file": "/path/to/in.log" * } * } * </pre> * * @param som The Javascript object * @return the launched process * @throws ScriptException if any Javascript error occurs * @throws IOException if any I/O error occurs */ public Process execute(final ScriptObjectMirror som) throws ScriptException, IOException { // Extract the working director String workingDirectory = (String) som.get("working_directory"); final File workingDirectoryFile = workingDirectory != null ? new File(workingDirectory) : null; // Extracts the arguments ScriptObjectMirror jsCommands = (ScriptObjectMirror) som.get("commands"); final List<String> commands; if (jsCommands != null) { commands = new ArrayList<>(jsCommands.size()); ScriptUtils.fillStringCollection(jsCommands, commands); } else commands = null; // Set the environment variables ScriptObjectMirror jsEnv = (ScriptObjectMirror) som.get("environment_variables"); final Map<String, String> envVars; if (jsEnv != null) { envVars = new LinkedHashMap<String, String>(); ScriptUtils.fillStringMap(jsEnv, envVars); } else envVars = null; // Set the output and error files String outputPath = (String) som.get("output_file"); final File outputFile = outputPath != null ? new File(outputPath) : null; String errorPath = (String) som.get("error_file"); final File errorFile = errorPath != null ? new File(errorPath) : null; return execute(workingDirectoryFile, commands, envVars, outputFile, errorFile); }
From source file:com.qwazr.tools.ProcessTool.java
License:Apache License
/** * Call this command from Javascript to execute a local binary. * <pre>/* ww w. ja va 2 s.c o m*/ * {@code * { * "working_directory": "/path/to/directory", * "environment_variables": { * "lang": "de", * "hello": "world" * }, * "commands": ["convert", "my.jpg", "my.gif"], * "output_file": "/path/to/out.log", * "error_file": "/path/to/in.log" * } * } * </pre> * * @param som The Javascript object * @return the launched process * @throws ScriptException if any Javascript error occurs * @throws IOException if any I/O error occurs */ public Process execute(ScriptObjectMirror som) throws ScriptException, IOException { // Extract the working directoru String workingDirectory = (String) som.get("working_directory"); final File workingDirectoryFile = workingDirectory != null ? new File(workingDirectory) : null; // Extracts the arguments ScriptObjectMirror jsCommands = (ScriptObjectMirror) som.get("commands"); final List<String> commands; if (jsCommands != null) { commands = new ArrayList<String>(jsCommands.size()); ScriptUtils.fillStringCollection(jsCommands, commands); } else commands = null; // Set the environment variables ScriptObjectMirror jsEnv = (ScriptObjectMirror) som.get("environment_variables"); final Map<String, String> envVars; if (jsEnv != null) { envVars = new LinkedHashMap<String, String>(); ScriptUtils.fillStringMap(jsEnv, envVars); } else envVars = null; // Set the output and error files String outputPath = (String) som.get("output_file"); final File outputFile = outputPath != null ? new File(outputPath) : null; String errorPath = (String) som.get("error_file"); final File errorFile = errorPath != null ? new File(errorPath) : null; return execute(workingDirectoryFile, commands, envVars, outputFile, errorFile); }
From source file:com.qwazr.utils.ScriptUtils.java
License:Apache License
public static <T> T[] toArray(ScriptObjectMirror som, Class<T> type) throws ScriptException { if (som == null) return null; if (!som.isArray()) throw new ScriptException("The JS object is not an array"); T[] array = (T[]) new Object[som.size()]; final AtomicInteger i = new AtomicInteger(0); som.values().forEach(o -> array[i.getAndIncrement()] = ((ScriptObjectMirror) o).to(type)); return array; }
From source file:com.threecrickets.jvm.json.nashorn.ScriptObjectMirrorEncoder.java
License:Mozilla Public License
public void encode(Object object, JsonContext context) throws IOException { ScriptObjectMirror scriptObjectMirror = (ScriptObjectMirror) object; Object wrapped = ScriptObjectMirror.unwrap(scriptObjectMirror, Context.getGlobal()); if (!(wrapped instanceof ScriptObjectMirror)) { context.encode(wrapped);/* ww w.j a va 2 s .com*/ return; } if (scriptObjectMirror.isArray()) { context.out.append('['); int length = scriptObjectMirror.size(); if (length > 0) { context.newline(); for (int i = 0; i < length; i++) { Object value = scriptObjectMirror.getSlot(i); context.indentNested(); context.nest().encode(value); if (i < length - 1) context.comma(); } context.newline(); context.indent(); } context.out.append(']'); } else { context.out.append('{'); String[] keys = scriptObjectMirror.getOwnKeys(true); int length = keys.length; if (length > 0) { context.newline(); for (int i = 0; i < length; i++) { String key = keys[i]; Object value = scriptObjectMirror.get(key); context.indentNested(); context.quoted(key); context.colon(); context.nest().encode(value); if (i < length - 1) context.comma(); } context.newline(); context.indent(); } context.out.append('}'); } }
From source file:hmi.flipper.behaviourselection.template.value.AbstractValue.java
License:BSD License
private Object js2Object(Object jsResult) { if (jsResult instanceof ScriptObjectMirror) { ScriptObjectMirror jsObject = (ScriptObjectMirror) jsResult; if (jsObject.size() > 0 && jsObject.containsKey("0")) { List list = new DefaultList(); for (int i = 0; i < jsObject.size(); i++) { Object toAdd = js2Object(jsObject.get("" + i)); if (toAdd == null) { break; // not a integer valued list }/* w ww. j a v a 2s .com*/ if (toAdd instanceof Double) { list.addItemEnd((Double) toAdd); } else if (toAdd instanceof Integer) { list.addItemEnd((Integer) toAdd); } else if (toAdd instanceof List) { list.addItemEnd((List) toAdd); } else if (toAdd instanceof Record) { list.addItemEnd((Record) toAdd); } else { list.addItemEnd(toAdd.toString()); } } if (list.size() == ((ScriptObjectMirror) jsObject).size()) { return list; } } Record record = new DefaultRecord(); for (Entry<String, Object> ent : jsObject.entrySet()) { record.set(ent.getKey(), js2Object(ent.getValue())); } return record; } else if (jsResult instanceof Long || jsResult instanceof Double || jsResult instanceof Float) { if (jsResult instanceof Long) { return ((Long) jsResult).doubleValue(); } else if (jsResult instanceof Float) { return ((Long) jsResult).floatValue(); } else { return ((Double) jsResult); } } else if (jsResult instanceof Integer) { return (Integer) jsResult; } else if (jsResult instanceof String) { return (String) jsResult; } System.err.println("Could not parse JavaScript result to eligible DefaultRecord type! Type was: " + jsResult.getClass()); return jsResult.toString(); }
From source file:org.bson.jvm.nashorn.ScriptObjectMirrorCodec.java
License:Apache License
@SuppressWarnings("unchecked") public void encode(BsonWriter writer, ScriptObjectMirror scriptObjectMirror, EncoderContext encoderContext) { Object wrapped = ScriptObjectMirror.unwrap(scriptObjectMirror, Context.getGlobal()); if (!(wrapped instanceof ScriptObjectMirror)) { // Attempt to encode the wrapped object Codec<Object> codec = null; try {/*from www .ja v a2 s . co m*/ codec = (Codec<Object>) codecRegistry.get(wrapped.getClass()); } catch (CodecConfigurationException x) { } if (codec != null) { codec.encode(writer, wrapped, encoderContext); return; } } if (scriptObjectMirror.isArray()) { writer.writeStartArray(); for (int i = 0, length = scriptObjectMirror.size(); i < length; i++) { Object item = scriptObjectMirror.getSlot(i); BsonUtil.writeChild(item, writer, encoderContext, codecRegistry); } writer.writeEndArray(); } else { writer.writeStartDocument(); for (String key : scriptObjectMirror.getOwnKeys(true)) { Object value = scriptObjectMirror.get(key); writer.writeName(key); BsonUtil.writeChild(value, writer, encoderContext, codecRegistry); } writer.writeEndDocument(); } }
From source file:org.eclipse.wst.jsdt.internal.esprima.EsprimaParser.java
License:Open Source License
/** * Add the errors reported on the "errors" array to * result./*from ww w . j a v a 2 s . c om*/ * * @param jsObject * @param result */ private void reportErrors(final ScriptObjectMirror jsObject, final JavaScriptUnit result) { ScriptObjectMirror errors = (ScriptObjectMirror) jsObject.getMember("errors"); //$NON-NLS-1$ if (errors == null || errors.size() < 1) return; DefaultProblem[] problems = new DefaultProblem[errors.size()]; for (int i = 0; i < errors.size(); ++i) problems[i] = createProblem(((ScriptObjectMirror) errors.getSlot(i))); result.setFlags(ASTNode.MALFORMED); result.setProblems(problems); }
From source file:org.eclipse.wst.jsdt.internal.esprima.EsprimaParser.java
License:Open Source License
/** * @param jsObject//from w w w. j a v a 2s.co m * @param result */ private void buildComments(ScriptObjectMirror jsObject, JavaScriptUnit result, AST t) { ScriptObjectMirror comments = (ScriptObjectMirror) jsObject.getMember("comments"); //$NON-NLS-1$ int commentSize = comments.size(); Comment[] resultComments = new Comment[commentSize]; for (int i = 0; i < commentSize; ++i) { ScriptObjectMirror obj = (ScriptObjectMirror) comments.getSlot(i); Comment newComment = createComment(obj, t); newComment.setAlternateRoot(result); resultComments[i] = newComment; } result.setCommentTable(resultComments); }