Search in sources :

Example 26 with NativeJavaObject

use of org.mozilla.javascript.NativeJavaObject in project pentaho-platform by pentaho.

the class JavaScriptResultSet method jsFunction_addRow.

public static Object jsFunction_addRow(final Context cx, final Scriptable thisObj, final Object[] args, final Function funObj) {
    if (args == null) {
        return null;
    }
    if (args.length == 0) {
        return null;
    }
    // TODO support dates
    JavaScriptResultSet resultSet = (JavaScriptResultSet) thisObj;
    if ((args.length == 1) && (args[0] instanceof NativeArray)) {
        NativeArray array = (NativeArray) args[0];
        int length = (int) array.getLength();
        Object[] row = new Object[length];
        String[] columnTypes = ((MemoryMetaData) resultSet.getMetaData()).getColumnTypes();
        for (int i = 0; i < length; i++) {
            Object data = array.get(i, thisObj);
            if (data == null) {
                row[i] = null;
            } else if (columnTypes != null) {
                if (data instanceof NativeJavaObject) {
                    // see if we can force a conversion
                    Object outputClass = null;
                    if ("string".equalsIgnoreCase(columnTypes[i])) {
                        // $NON-NLS-1$
                        outputClass = java.lang.String.class;
                    } else if ("date".equalsIgnoreCase(columnTypes[i])) {
                        // $NON-NLS-1$
                        outputClass = java.util.Date.class;
                    } else if ("int".equalsIgnoreCase(columnTypes[i])) {
                        // $NON-NLS-1$
                        outputClass = java.lang.Integer.class;
                    } else if ("float".equalsIgnoreCase(columnTypes[i])) {
                        // $NON-NLS-1$
                        outputClass = java.lang.Float.class;
                    } else if ("double".equalsIgnoreCase(columnTypes[i])) {
                        // $NON-NLS-1$
                        outputClass = java.lang.Double.class;
                    }
                    if ((NativeJavaObject.canConvert(data, outputClass.getClass()))) {
                        row[i] = Context.jsToJava(data, java.lang.String.class);
                    } else {
                        row[i] = null;
                    }
                }
                if ("string".equalsIgnoreCase(columnTypes[i])) {
                    // $NON-NLS-1$
                    row[i] = data.toString();
                } else if ("date".equalsIgnoreCase(columnTypes[i]) && (data instanceof String)) {
                    // $NON-NLS-1$
                    // $NON-NLS-1$
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                    try {
                        row[i] = format.parse((String) data);
                    } catch (Throwable t) {
                        row[i] = null;
                    }
                } else if ("int".equalsIgnoreCase(columnTypes[i]) && (data instanceof Integer)) {
                    // $NON-NLS-1$
                    row[i] = data;
                } else if ("int".equalsIgnoreCase(columnTypes[i]) && (data instanceof Double)) {
                    // $NON-NLS-1$
                    row[i] = new Integer(((Double) data).intValue());
                } else if ("int".equalsIgnoreCase(columnTypes[i]) && (data instanceof String)) {
                    // $NON-NLS-1$
                    row[i] = new Integer((String) data);
                } else if ("float".equalsIgnoreCase(columnTypes[i]) && (data instanceof Double)) {
                    // $NON-NLS-1$
                    row[i] = data;
                } else if ("float".equalsIgnoreCase(columnTypes[i]) && (data instanceof Integer)) {
                    // $NON-NLS-1$
                    row[i] = new Double(((Integer) data).floatValue());
                } else if ("float".equalsIgnoreCase(columnTypes[i]) && (data instanceof String)) {
                    // $NON-NLS-1$
                    row[i] = new Integer((String) data);
                } else if ("double".equalsIgnoreCase(columnTypes[i]) && (data instanceof Double)) {
                    // $NON-NLS-1$
                    row[i] = data;
                }
            } else if (data instanceof NativeJavaObject) {
                Object obj = ((NativeJavaObject) data).unwrap();
                row[i] = obj;
            } else {
                row[i] = data;
            }
        }
        resultSet.addRow(row);
    } else {
        int length = args.length;
        String[] row = new String[length];
        for (int i = 0; i < length; i++) {
            row[i] = args[i].toString();
        }
        resultSet.addRow(row);
    }
    return null;
}
Also used : NativeArray(org.mozilla.javascript.NativeArray) MemoryMetaData(org.pentaho.commons.connection.memory.MemoryMetaData) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) SimpleDateFormat(java.text.SimpleDateFormat)

Example 27 with NativeJavaObject

use of org.mozilla.javascript.NativeJavaObject in project convertigo by convertigo.

the class AbstractCouchDbTransaction method toJson.

protected Object toJson(Object object) throws JSONException {
    if (object == null)
        return null;
    Object jsonElement = null;
    if (object instanceof NativeObject) {
        JSONObject jsonChildren = new JSONObject();
        NativeObject nativeObject = (NativeObject) object;
        for (Iterator<Entry<Object, Object>> it = GenericUtils.cast(nativeObject.entrySet().iterator()); it.hasNext(); ) {
            Entry<Object, Object> entry = it.next();
            jsonChildren.put(entry.getKey().toString(), toJson(entry.getValue()));
        }
        return jsonChildren;
    }
    if (object instanceof NativeJavaObject) {
        NativeJavaObject nativeJavaObject = (NativeJavaObject) object;
        return toJson(nativeJavaObject.unwrap());
    } else if (object instanceof NativeJavaArray) {
        Object ob = ((NativeJavaArray) object).unwrap();
        return toJson(Arrays.asList((Object[]) ob));
    } else if (object instanceof NativeArray) {
        NativeArray array = (NativeArray) object;
        JSONArray jsonArray = new JSONArray();
        for (int j = 0; j < array.getLength(); j++) {
            jsonArray.put(toJson(array.get(j, array)));
        }
        jsonElement = jsonArray;
    } else if ((object instanceof org.mozilla.javascript.Scriptable)) {
        org.mozilla.javascript.Scriptable jsObj = (org.mozilla.javascript.Scriptable) object;
        return toJson(String.valueOf(jsObj.toString()));
    } else if (object.getClass().isArray()) {
        return toJson(Arrays.asList((Object[]) object));
    } else if (object instanceof Collection<?>) {
        JSONArray jsonArray = new JSONArray();
        for (Object o : (Collection<?>) object) {
            jsonArray.put(toJson(o));
        }
        jsonElement = jsonArray;
    } else if (object instanceof Element) {
        jsonElement = toJson((Element) object);
    } else {
        jsonElement = object;
    }
    return jsonElement;
}
Also used : NativeArray(org.mozilla.javascript.NativeArray) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) Element(org.w3c.dom.Element) JSONArray(org.codehaus.jettison.json.JSONArray) NativeJavaArray(org.mozilla.javascript.NativeJavaArray) NativeObject(org.mozilla.javascript.NativeObject) Entry(java.util.Map.Entry) JSONObject(org.codehaus.jettison.json.JSONObject) NativeObject(org.mozilla.javascript.NativeObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) JSONObject(org.codehaus.jettison.json.JSONObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject)

Example 28 with NativeJavaObject

use of org.mozilla.javascript.NativeJavaObject in project convertigo by convertigo.

the class PacManager method evaluate.

public String evaluate(String url, String host) {
    Object result = "";
    try {
        Context ctx = Context.enter();
        result = RhinoUtils.evalInterpretedJavascript(ctx, scope, "FindProxyForURL (\"" + url + "\",\"" + host + "\")", "check", 0, null);
    } catch (Exception e) {
        Engine.logProxyManager.error("(PacManager) Failed to evaluate .pac for " + url + " from " + host, e);
    } finally {
        Context.exit();
    }
    if (result instanceof NativeJavaObject) {
        result = ((NativeJavaObject) result).unwrap();
    }
    Engine.logProxyManager.debug("(PacManager) evaluate " + url + " from " + host + " : " + result);
    return result.toString();
}
Also used : Context(org.mozilla.javascript.Context) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) IOException(java.io.IOException)

Example 29 with NativeJavaObject

use of org.mozilla.javascript.NativeJavaObject in project convertigo by convertigo.

the class ClientInstructionSetValue method applyOnResponse.

@Override
public boolean applyOnResponse(Shuttle shuttle) {
    String targetPath = getEvaluatedTargetPath(shuttle);
    String value = "";
    Object ob = shuttle.evalJavascript(targetValue);
    if (ob != null) {
        if (ob instanceof NativeJavaArray) {
            Object object = ((NativeJavaArray) ob).unwrap();
            List<Object> list = Arrays.asList((Object[]) object);
            for (int j = 0; j < list.size(); j++) {
                Object item = list.get(j);
                value += item.toString() + ",";
            }
        } else if (ob instanceof NativeJavaObject) {
            NativeJavaObject nativeJavaObject = (NativeJavaObject) ob;
            Object javaObject = nativeJavaObject.unwrap();
            if (javaObject instanceof List) {
                Vector<String> v = GenericUtils.cast(javaObject);
                for (int j = 0; j < v.size(); j++) {
                    value += v.get(j) + ",";
                }
            } else {
                value = (String) nativeJavaObject.getDefaultValue(String.class);
            }
        } else if (ob instanceof NativeArray) {
            NativeArray array = (NativeArray) ob;
            for (int j = 0; j < array.getLength(); j++) {
                Object item = array.get(j, array);
                value += item.toString() + ",";
            }
        } else if (ob instanceof List) {
            Vector<String> v = GenericUtils.cast(ob);
            for (int j = 0; j < v.size(); j++) {
                value += v.get(j) + ",";
            }
        } else {
            value = ob.toString();
        }
    }
    Engine.logSiteClipper.trace("(ClientInstructionSetValue) JQuery selector '" + targetPath + "' with value '" + value + "'");
    shuttle.addPostInstruction(new SetValue(targetPath, value));
    return true;
}
Also used : NativeArray(org.mozilla.javascript.NativeArray) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) List(java.util.List) NativeJavaArray(org.mozilla.javascript.NativeJavaArray) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) Vector(java.util.Vector) SetValue(com.twinsoft.convertigo.engine.siteclipper.clientinstruction.SetValue)

Example 30 with NativeJavaObject

use of org.mozilla.javascript.NativeJavaObject in project convertigo by convertigo.

the class InputHtmlSetValueStatement method getEvent.

@Override
public AbstractEvent getEvent(Context javascriptContext, Scriptable scope) throws EngineException {
    try {
        evaluate(javascriptContext, scope, expression, "expression", true);
    } catch (EngineException e) {
    // TODO:
    }
    String value = "";
    if (evaluated != null) {
        if (evaluated instanceof NativeJavaArray) {
            Object object = ((NativeJavaArray) evaluated).unwrap();
            List<Object> list = Arrays.asList((Object[]) object);
            for (int j = 0; j < list.size(); j++) {
                Object item = list.get(j);
                value += item.toString() + ",";
            }
        } else if (evaluated instanceof NativeJavaObject) {
            NativeJavaObject nativeJavaObject = (NativeJavaObject) evaluated;
            Object javaObject = nativeJavaObject.unwrap();
            if (javaObject instanceof List) {
                Vector<String> v = GenericUtils.cast(javaObject);
                for (int j = 0; j < v.size(); j++) {
                    value += v.get(j) + ",";
                }
            } else {
                value = (String) nativeJavaObject.getDefaultValue(String.class);
            }
        } else if (evaluated instanceof NativeArray) {
            NativeArray array = (NativeArray) evaluated;
            for (int j = 0; j < array.getLength(); j++) {
                Object item = array.get(j, array);
                value += item.toString() + ",";
            }
        } else if (evaluated instanceof List) {
            Vector<String> v = GenericUtils.cast(evaluated);
            for (int j = 0; j < v.size(); j++) {
                value += v.get(j) + ",";
            }
        } else {
            value = evaluated.toString();
        }
    }
    return new InputValueEvent(getXpath(), getUiEvent(), value);
}
Also used : NativeArray(org.mozilla.javascript.NativeArray) InputValueEvent(com.twinsoft.convertigo.engine.parsers.events.InputValueEvent) EngineException(com.twinsoft.convertigo.engine.EngineException) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) List(java.util.List) NativeJavaArray(org.mozilla.javascript.NativeJavaArray) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) Vector(java.util.Vector)

Aggregations

NativeJavaObject (org.mozilla.javascript.NativeJavaObject)45 Scriptable (org.mozilla.javascript.Scriptable)16 Context (org.mozilla.javascript.Context)11 NativeArray (org.mozilla.javascript.NativeArray)9 ScriptableObject (org.mozilla.javascript.ScriptableObject)9 NativeJavaArray (org.mozilla.javascript.NativeJavaArray)8 ArrayList (java.util.ArrayList)7 List (java.util.List)6 IOException (java.io.IOException)5 Test (org.junit.Test)4 NativeObject (org.mozilla.javascript.NativeObject)4 Undefined (org.mozilla.javascript.Undefined)4 NodeList (org.w3c.dom.NodeList)4 IScriptableProvider (com.servoy.j2db.scripting.IScriptableProvider)3 SolutionScope (com.servoy.j2db.scripting.SolutionScope)3 EngineException (com.twinsoft.convertigo.engine.EngineException)3 Collection (java.util.Collection)3 Vector (java.util.Vector)3 FunctionObject (org.mozilla.javascript.FunctionObject)3 Node (org.w3c.dom.Node)3