Search in sources :

Example 6 with NativeJavaArray

use of org.mozilla.javascript.NativeJavaArray 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 7 with NativeJavaArray

use of org.mozilla.javascript.NativeJavaArray 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 8 with NativeJavaArray

use of org.mozilla.javascript.NativeJavaArray 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)

Example 9 with NativeJavaArray

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

the class HTTPStatement method makeQuery.

private String makeQuery(com.twinsoft.convertigo.engine.Context context, String methodToAnalyse) throws EngineException {
    String variable, httpVariable, httpVariableValue, method, query = "";
    int len = numberOfVariables();
    String urlEncodingCharset = getUrlEncodingCharset();
    if (urlEncodingCharset == null || urlEncodingCharset.length() == 0) {
        urlEncodingCharset = getParentTransaction().getComputedUrlEncodingCharset();
    }
    try {
        for (int i = 0; i < len; i++) {
            HttpStatementVariable httpStatementVariable = (HttpStatementVariable) getVariable(i);
            if (httpStatementVariable != null) {
                variable = httpStatementVariable.getName();
                method = httpStatementVariable.getHttpMethod();
                httpVariable = httpStatementVariable.getHttpName();
                if (method.equals(methodToAnalyse)) {
                    if (query.length() != 0) {
                        query += "&";
                    }
                    try {
                        // evaluate method can throw EngineException
                        // try catch to get the default value in this case
                        evaluate(javascriptContext, scope, variable, httpVariable, false);
                        Engine.logBeans.debug("Javascript evaluation of httpVariable named '" + httpVariable + "' executed");
                        // if no Engine Exception has been thown until here, normal execution
                        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);
                                    httpVariableValue = item.toString();
                                    query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, j != 0);
                                }
                            } 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++) {
                                        httpVariableValue = v.get(j);
                                        query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, j != 0);
                                    }
                                } else {
                                    httpVariableValue = (String) nativeJavaObject.getDefaultValue(String.class);
                                    query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, false);
                                }
                            } else if (evaluated instanceof NativeArray) {
                                NativeArray array = (NativeArray) evaluated;
                                for (int j = 0; j < array.getLength(); j++) {
                                    Object item = array.get(j, array);
                                    httpVariableValue = item.toString();
                                    query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, j != 0);
                                }
                            } else if (evaluated instanceof List) {
                                Vector<String> v = GenericUtils.cast(evaluated);
                                for (int j = 0; j < v.size(); j++) {
                                    httpVariableValue = v.get(j);
                                    query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, j != 0);
                                }
                            } else if (evaluated instanceof Undefined) {
                                throw new EngineException("Undefined");
                            } else {
                                httpVariableValue = evaluated.toString();
                                query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, false);
                            }
                        }
                    } catch (EngineException e) {
                        // Engine Exception has been thrown ==> get variable default value
                        Object value = getVariableValue(variable);
                        if (value != null) {
                            if (value instanceof Collection) {
                                List<String> list = GenericUtils.toString((Collection<?>) value);
                                for (String val : list) {
                                    httpVariableValue = val;
                                    query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, false);
                                }
                            } else {
                                httpVariableValue = value.toString();
                                query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, false);
                            }
                        }
                    }
                }
            }
        }
    } catch (UnsupportedEncodingException e) {
        throw new EngineException("UTF-8 encoding is not supported.", e);
    }
    return query;
}
Also used : NativeArray(org.mozilla.javascript.NativeArray) Undefined(org.mozilla.javascript.Undefined) EngineException(com.twinsoft.convertigo.engine.EngineException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpStatementVariable(com.twinsoft.convertigo.beans.variables.HttpStatementVariable) NativeJavaArray(org.mozilla.javascript.NativeJavaArray) Collection(java.util.Collection) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) XMLVector(com.twinsoft.convertigo.beans.common.XMLVector) Vector(java.util.Vector)

Example 10 with NativeJavaArray

use of org.mozilla.javascript.NativeJavaArray in project servoy-client by Servoy.

the class FoundSet method get.

public Object get(String name, Scriptable start) {
    if (isToplevelKeyword(name))
        return Scriptable.NOT_FOUND;
    if (// $NON-NLS-1$
    name.equals("multiSelect")) {
        return Boolean.valueOf(isMultiSelect());
    }
    if (// $NON-NLS-1$
    "alldataproviders".equals(name)) {
        return new NativeJavaArray(this, alldataproviders());
    }
    Object mobj = jsFunctions.get(name);
    if (mobj != null) {
        ScriptRuntime.setFunctionProtoAndParent((BaseFunction) mobj, start);
        return mobj;
    }
    if (getSize() == 0) {
        try {
            if (isValidRelation(name)) {
                return getPrototypeState().getValue(name);
            }
            if (containsDataProvider(name)) {
                return getDataProviderValue(name);
            }
        } catch (Exception ex) {
            Debug.error(ex);
        }
    } else if (containsDataProvider(name)) {
        return getDataProviderValue(name);
    } else if (has(name, start)) {
        int row = getSelectedIndex();
        // but if a value is being get it should always try to get a value.
        if (row == -1 && getSize() > 0)
            row = 0;
        if (row != -1) {
            IRecordInternal state = getRecord(row);
            if (state instanceof Scriptable) {
                return ((Scriptable) state).get(name, start);
            }
        }
    }
    if (// $NON-NLS-1$
    name.equals("_records_")) {
        int maxRows = getSize();
        if (hadMoreRows()) {
            maxRows--;
        }
        Scriptable records = Context.getCurrentContext().newArray(this, maxRows);
        for (int i = 0; i < maxRows; i++) {
            IRecordInternal record = pksAndRecords.getCachedRecords().get(i);
            records.put(i, records, record);
        }
        return records;
    }
    if (// $NON-NLS-1$
    name.equals("_selection_")) {
        int[] selection = getSelectedIndexes();
        if (selection.length == 0) {
            return Integer.valueOf(0);
        }
        if (selection.length == 1) {
            return Integer.valueOf(selection[0] + 1);
        }
        StringBuilder buf = new StringBuilder();
        buf.append('[');
        for (int i = 0; i < selection.length; i++) {
            // $NON-NLS-1$
            if (i > 0)
                buf.append(", ");
            buf.append(selection[i] + 1);
        }
        buf.append(']');
        return buf.toString();
    }
    return Scriptable.NOT_FOUND;
}
Also used : NativeJavaArray(org.mozilla.javascript.NativeJavaArray) Scriptable(org.mozilla.javascript.Scriptable) ServoyException(com.servoy.j2db.util.ServoyException) ApplicationException(com.servoy.j2db.ApplicationException) RemoteException(java.rmi.RemoteException) RepositoryException(com.servoy.j2db.persistence.RepositoryException)

Aggregations

NativeJavaArray (org.mozilla.javascript.NativeJavaArray)14 NativeArray (org.mozilla.javascript.NativeArray)8 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)8 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Collection (java.util.Collection)5 NodeList (org.w3c.dom.NodeList)4 EngineException (com.twinsoft.convertigo.engine.EngineException)3 Vector (java.util.Vector)3 XmlSchemaCollection (org.apache.ws.commons.schema.XmlSchemaCollection)3 NativeObject (org.mozilla.javascript.NativeObject)3 Scriptable (org.mozilla.javascript.Scriptable)2 Undefined (org.mozilla.javascript.Undefined)2 Node (org.w3c.dom.Node)2 ApplicationException (com.servoy.j2db.ApplicationException)1 ExitScriptException (com.servoy.j2db.ExitScriptException)1 IBasicFormManager (com.servoy.j2db.IBasicFormManager)1 IFormController (com.servoy.j2db.IFormController)1 RepositoryException (com.servoy.j2db.persistence.RepositoryException)1 ScriptMethod (com.servoy.j2db.persistence.ScriptMethod)1