Search in sources :

Example 36 with NativeJavaObject

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

the class WebForm method registerComponentsToScope.

private int registerComponentsToScope(Scriptable fs, ElementScope es, int counter, Object[] comps, Map<String, Object[]> hmChildrenJavaMembers, Object parent) {
    if (comps != null) {
        for (Object comp : comps) {
            if (comp instanceof WebCellBasedView) {
                WebCellBasedView portal = (WebCellBasedView) comp;
                counter = registerComponentsToScope(fs, es, counter, portal.getComponents(), hmChildrenJavaMembers, comp);
            }
            String name = null;
            if (comp instanceof WrapperContainer) {
                comp = ((WrapperContainer) comp).getDelegate();
            }
            if (comp instanceof IComponent) {
                name = ((IComponent) comp).getName();
            } else if (comp instanceof java.awt.Component) {
                name = ((java.awt.Component) comp).getName();
            }
            if (comp instanceof WebImageBeanHolder) {
                comp = ((WebImageBeanHolder) comp).getDelegate();
            } else if (comp instanceof WebBeanHolder) {
                comp = ((WebBeanHolder) comp).getDelegate();
            }
            String groupName = FormElementGroup.getName((String) formController.getComponentProperty(comp, ComponentFactory.GROUPID_COMPONENT_PROPERTY));
            Object scriptable = comp;
            if (comp instanceof IScriptableProvider)
                scriptable = ((IScriptableProvider) comp).getScriptObject();
            JavaMembers jm = ScriptObjectRegistry.getJavaMembers(scriptable.getClass(), ScriptableObject.getTopLevelScope(fs));
            // $NON-NLS-1$
            boolean named = name != null && !name.equals("") && !name.startsWith(ComponentFactory.WEB_ID_PREFIX);
            if (groupName != null || named) {
                try {
                    Scriptable s;
                    if (parent instanceof WebCellBasedView) {
                        s = new CellNativeJavaObject(fs, comp, jm, (WebCellBasedView) parent);
                    } else {
                        s = new NativeJavaObject(fs, scriptable, jm);
                    }
                    if (named) {
                        es.put(name, fs, s);
                        es.put(counter++, fs, s);
                        hmChildrenJavaMembers.put(name, new Object[] { jm, scriptable });
                    }
                    if (groupName != null) {
                        Object group = es.get(groupName, fs);
                        if (group == Scriptable.NOT_FOUND) {
                            group = new NativeJavaObject(fs, new RuntimeGroup(groupName), ScriptObjectRegistry.getJavaMembers(RuntimeGroup.class, ScriptableObject.getTopLevelScope(fs)));
                            es.put(groupName, fs, group);
                            es.put(counter++, fs, group);
                        }
                        if (scriptable instanceof IRuntimeComponent && group instanceof NativeJavaObject && ((NativeJavaObject) group).unwrap() instanceof RuntimeGroup) {
                            ((RuntimeGroup) (((NativeJavaObject) group).unwrap())).addScriptBaseMethodsObj((IRuntimeComponent) scriptable);
                        }
                    }
                } catch (Throwable ex) {
                    // incase classdefnot founds are thrown for beans,applets/plugins
                    Debug.error(ex);
                }
            }
        }
    }
    return counter;
}
Also used : WebImageBeanHolder(com.servoy.j2db.server.headlessclient.dataui.WebImageBeanHolder) JavaMembers(org.mozilla.javascript.JavaMembers) IComponent(com.servoy.j2db.ui.IComponent) WebCellBasedView(com.servoy.j2db.server.headlessclient.dataui.WebCellBasedView) Scriptable(org.mozilla.javascript.Scriptable) RuntimeGroup(com.servoy.j2db.scripting.RuntimeGroup) WebBeanHolder(com.servoy.j2db.server.headlessclient.dataui.WebBeanHolder) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) IRuntimeComponent(com.servoy.j2db.ui.runtime.IRuntimeComponent) IComponent(com.servoy.j2db.ui.IComponent) BaseComponent(com.servoy.j2db.persistence.BaseComponent) IFieldComponent(com.servoy.j2db.ui.IFieldComponent) Component(org.apache.wicket.Component) IRuntimeComponent(com.servoy.j2db.ui.runtime.IRuntimeComponent) IScriptableProvider(com.servoy.j2db.scripting.IScriptableProvider) NativeJavaObject(org.mozilla.javascript.NativeJavaObject)

Example 37 with NativeJavaObject

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

the class FormManager method setCurrentContainer.

/**
 * @param mainContainer
 */
public void setCurrentContainer(IMainContainer mainContainer, String name) {
    if (mainContainer != null) {
        currentContainer = mainContainer;
        if (name != null) {
            // reset it in the containers (must be done for the webclient)
            containers.put(name, mainContainer);
        }
    } else {
        currentContainer = getMainContainer(null);
    }
    enableCmds(true);
    FormController formController = currentContainer.getController();
    if (formController != null) {
        IExecutingEnviroment scriptEngine = application.getScriptEngine();
        if (scriptEngine != null) {
            SolutionScope ss = scriptEngine.getSolutionScope();
            Context.enter();
            try {
                // $NON-NLS-1$
                ss.put("currentcontroller", ss, new NativeJavaObject(ss, formController.initForJSUsage(), new InstanceJavaMembers(ss, JSForm.class)));
            } finally {
                Context.exit();
            }
        }
    }
    application.getRuntimeWindowManager().setCurrentWindowName(name);
}
Also used : InstanceJavaMembers(com.servoy.j2db.scripting.InstanceJavaMembers) IExecutingEnviroment(com.servoy.j2db.scripting.IExecutingEnviroment) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) SolutionScope(com.servoy.j2db.scripting.SolutionScope)

Example 38 with NativeJavaObject

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

the class BasicFormController method initForJSUsage.

/**
 * Initialize this FormController(or related classes/methods) to be used in javascript
 */
public synchronized JSForm initForJSUsage(CreationalPrototype creationalPrototype) {
    if (formScope == null) {
        try {
            // make scope for state delegation via prototype mechanism
            List<Form> forms = application.getFlattenedSolution().getFormHierarchy(form);
            List<ISupportScriptProviders> scriptProviders = new ArrayList<ISupportScriptProviders>();
            for (Form frm : forms) {
                scriptProviders.add(new RuntimeSupportScriptProviders(application, frm));
            }
            formScope = new FormScope(this, scriptProviders.toArray(new ISupportScriptProviders[scriptProviders.size()]));
            if (formModel != null) {
                ITable formTable = application.getFoundSetManager().getTable(form.getDataSource());
                formScope.setPrototype(new SelectedRecordScope(this, formTable == null ? null : application.getScriptEngine().getTableScope(formTable)));
                // $NON-NLS-1$
                formScope.putWithoutFireChange("foundset", formModel);
            }
            // create JS place holder for this object
            scriptableForm = new BasicFormController.JSForm(this);
            // set parent scope
            NativeJavaObject formObject = new NativeJavaObject(formScope, scriptableForm, ScriptObjectRegistry.getJavaMembers(FormController.JSForm.class, formScope));
            // $NON-NLS-1$
            formScope.putWithoutFireChange("controller", formObject);
            // register the place holder 'scriptableForm' in CreationalPrototype scope
            creationalPrototype.setLocked(false);
            // $NON-NLS-1$
            creationalPrototype.put(((Integer) creationalPrototype.get("length", creationalPrototype)).intValue(), creationalPrototype, formScope);
            creationalPrototype.put(getName(), creationalPrototype, formScope);
            creationalPrototype.setLocked(true);
            formScope.createVars();
        } catch (Exception ex) {
            // $NON-NLS-1$
            application.reportError(application.getI18NMessage("servoy.formPanel.error.initFormScope") + ": " + getName(), ex);
            return null;
        }
    }
    return scriptableForm;
}
Also used : Form(com.servoy.j2db.persistence.Form) ArrayList(java.util.ArrayList) ServoyException(com.servoy.j2db.util.ServoyException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) FormScope(com.servoy.j2db.scripting.FormScope) ISupportScriptProviders(com.servoy.j2db.persistence.ISupportScriptProviders) RuntimeSupportScriptProviders(com.servoy.j2db.FormController.RuntimeSupportScriptProviders) SelectedRecordScope(com.servoy.j2db.scripting.SelectedRecordScope) ITable(com.servoy.j2db.persistence.ITable) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ITwoNativeJavaObject(com.servoy.j2db.scripting.ITwoNativeJavaObject)

Example 39 with NativeJavaObject

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

the class ServoyWrapFactory method wrap.

/**
 * @see org.mozilla.javascript.WrapFactory#wrap(org.mozilla.javascript.Context, org.mozilla.javascript.Scriptable, java.lang.Object, java.lang.Class)
 */
@Override
public Object wrap(Context cx, Scriptable scope, Object obj, Class staticType) {
    if (application.getSolution() == null) {
        throw new ExitScriptException("killing current script, client/solution already terminated");
    }
    if (obj == null || obj == Undefined.instance || obj instanceof Scriptable || obj instanceof String || obj instanceof CharSequence || obj instanceof Number || obj instanceof Boolean) {
        return obj;
    }
    if (obj instanceof Date) {
        return cx.newObject(scope, "Date", new Object[] { new Double(NativeDate.convertToUTCMillisFromJava(((Date) obj).getTime())) });
    }
    if (obj instanceof DbIdentValue || obj instanceof UUID) {
        return new NativeJavaObject(scope, obj, ScriptObjectRegistry.getJavaMembers(obj.getClass(), null));
    }
    if (cx != null) {
        if (obj instanceof JSConvertedMap<?, ?>) {
            Scriptable newObject = null;
            JSConvertedMap<?, ?> map = (JSConvertedMap<?, ?>) obj;
            if (map.getConstructorName() != null) {
                newObject = cx.newObject(scope, map.getConstructorName());
            } else {
                newObject = cx.newObject(scope);
            }
            Iterator<?> iterator = map.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<?, ?> next = (Entry<?, ?>) iterator.next();
                Object key = next.getKey();
                Object value = next.getValue();
                if (value != null) {
                    value = wrap(cx, newObject, value, value.getClass());
                }
                if (key instanceof Integer) {
                    newObject.put(((Integer) key).intValue(), newObject, value);
                } else if (key instanceof String) {
                    newObject.put((String) key, newObject, value);
                } else {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    Debug.error("Try to create a JSConvertedMap->NativeObject with a key that isnt a string or integer:" + key + " for value: " + value);
                }
            }
            return newObject;
        }
        if (obj.getClass() == JSONObject.class) {
            JSONObject json = (JSONObject) obj;
            Scriptable newObject = cx.newObject(scope);
            Iterator<String> iterator = json.keys();
            while (iterator.hasNext()) {
                String key = iterator.next();
                Object value = null;
                try {
                    value = ServoyJSONObject.jsonNullToNull(json.get(key));
                } catch (JSONException e) {
                    Debug.error(e);
                }
                if (value != null) {
                    value = wrap(cx, newObject, value, value.getClass());
                }
                newObject.put(key, newObject, value);
            }
            return newObject;
        }
        if (obj.getClass() == JSONArray.class) {
            JSONArray array = (JSONArray) obj;
            Scriptable newObject = cx.newObject(scope, "Array");
            for (int i = 0; i < array.length(); i++) {
                Object value = null;
                try {
                    value = array.get(i);
                } catch (JSONException e) {
                    Debug.error(e);
                }
                if (value != null) {
                    value = wrap(cx, newObject, value, value.getClass());
                }
                newObject.put(i, newObject, value);
            }
            return newObject;
        }
    }
    if (obj instanceof IDataSet) {
        return new JSDataSet(application, (IDataSet) obj);
    }
    if (obj instanceof FormController) {
        throw new IllegalArgumentException(// $NON-NLS-1$
        "Bad practice: FormController cant be wrapped to javascript (for example not usable as argument in Scheduler plugin)");
    }
    // if it is a none primitive array
    if (obj.getClass().isArray() && !obj.getClass().getComponentType().isPrimitive()) {
        // then convert the array to a NativeArray, first convert all the elements of the array itself.
        Object[] source = (Object[]) obj;
        Object[] array = new Object[source.length];
        for (int i = 0; i < source.length; i++) {
            Object src = source[i];
            array[i] = wrap(cx, scope, src, src != null ? src.getClass() : null);
        }
        return cx.newArray(scope, array);
    }
    return super.wrap(cx, scope, obj, staticType);
}
Also used : JSDataSet(com.servoy.j2db.dataprocessing.JSDataSet) Entry(java.util.Map.Entry) DbIdentValue(com.servoy.j2db.dataprocessing.ValueFactory.DbIdentValue) UUID(com.servoy.j2db.util.UUID) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) FormController(com.servoy.j2db.FormController) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Scriptable(org.mozilla.javascript.Scriptable) ExitScriptException(com.servoy.j2db.ExitScriptException) Date(java.util.Date) NativeDate(org.mozilla.javascript.NativeDate) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) IDataSet(com.servoy.j2db.dataprocessing.IDataSet) Map(java.util.Map)

Example 40 with NativeJavaObject

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

the class ScanFileFilter method evaluateFilter.

private boolean evaluateFilter(String filter, FileInfo fileInfo) {
    Context ctx = ContextFactory.getGlobal().enterContext();
    try {
        Scriptable scope = new DefaultScriptable(ctx);
        scope.put("file", scope, new NativeJavaObject(scope, fileInfo, FileInfo.class));
        Object result = ctx.evaluateString(scope, filter, "<filter>", 1, null);
        return Context.toBoolean(result);
    } catch (Exception ex) {
        return false;
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) DefaultScriptable(org.santfeliu.util.script.DefaultScriptable) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) DefaultScriptable(org.santfeliu.util.script.DefaultScriptable) Scriptable(org.mozilla.javascript.Scriptable) NativeJavaObject(org.mozilla.javascript.NativeJavaObject)

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