Package org.mozilla.javascript

Examples of org.mozilla.javascript.NativeObject


    @JSConstructor
    public static Object constructor(Context cx, Object[] args, Function ctorObj, boolean inNewExpr) {
        if (args.length != 1) {
            throw ScriptRuntime.constructError("Error", "Constructor takes a single argument");
        }
        NativeObject config = prepConfig(cx, (Scriptable) args[0]);
        Polygon poly = null;
        if (inNewExpr) {
            poly = new Polygon(config);
        } else {
            poly = new Polygon(config.getParentScope(), config);
        }
        return poly;
    }
View Full Code Here


            throw ScriptRuntime.constructError("Error", "Constructor takes a single argument");
        }
        Schema schema = null;
        Object arg = args[0];
        if (arg instanceof Scriptable) {
            NativeObject config = prepConfig((Scriptable) arg);
            if (inNewExpr) {
                schema = new Schema(config);
            } else {
                schema = new Schema(config.getParentScope(), config);
            }
        } else {
            throw ScriptRuntime.constructError("Error", "Could not create schema from argument: " + Context.toString(arg));
        }
        return schema;
View Full Code Here

     * @param obj
     */
    static NativeObject prepConfig(Scriptable obj) {
        Scriptable scope = ScriptableObject.getTopLevelScope(obj);
        Context cx = getCurrentContext();
        NativeObject config = null;
        if (obj instanceof NativeObject) {
            config = (NativeObject) obj;
        } else if (obj instanceof NativeArray) {
            config = (NativeObject) cx.newObject(scope, "Object");
            config.put("fields", config, (NativeArray) obj);
        } else {
            throw ScriptRuntime.constructError("Error", "Could not create config from argument: " + Context.toString(obj));
        }
        return config;
    }
View Full Code Here

    @JSConstructor
    public static Object constructor(Context cx, Object[] args, Function ctorObj, boolean inNewExpr) {
        if (args.length != 1) {
            throw ScriptRuntime.constructError("Error", "LineString constructor takes a single argument");
        }
        NativeObject config = prepConfig(cx, (Scriptable) args[0]);
        LineString line = null;
        if (inNewExpr) {
            line = new LineString(config);
        } else {
            line = new LineString(config.getParentScope(), config);
        }
        return line;
    }
View Full Code Here

            throw ScriptRuntime.constructError("Error", "Constructor takes a single argument");
        }
        Field field = null;
        Object arg = args[0];
        if (arg instanceof NativeObject) {
            NativeObject config = (NativeObject) arg;
            if (inNewExpr) {
                field = new Field(config);
            } else {
                field = new Field(config.getParentScope(), config);
            }
        } else {
            throw ScriptRuntime.constructError("Error", "Could not create field from argument: " + Context.toString(arg));
        }
        return field;
View Full Code Here

    public NativeObject getOutputs() {
        return createJSParameterMap(outputs);
    }

    private NativeObject createJSParameterMap(Map<String, Parameter<?>> map) {
        NativeObject obj = (NativeObject) getCurrentContext().newObject(getParentScope());
        for (String id : map.keySet()) {
            Parameter<?> param = map.get(id);
            obj.put(id, obj, createJSParameter(param));
        }
        return obj;
    }
View Full Code Here

        }
        return obj;
    }

    private Object createJSParameter(Parameter<?> param) {
        NativeObject obj = (NativeObject) getCurrentContext().newObject(getParentScope());
       
        Class<?> binding = param.getType();
        String typeName = Type.getName(binding);
        if (typeName != null) {
            obj.put("type", obj, typeName);
        } else {
            obj.put("type", obj, Context.javaToJS(binding, getParentScope()));
        }
       
        obj.put("name", obj, param.getName());
       
        InternationalString i18nTitle = param.getTitle();
        if (i18nTitle != null) {
            obj.put("title", obj, i18nTitle.toString());
        }

        InternationalString i18nDescription = param.getDescription();
        if (i18nDescription != null) {
            obj.put("description", obj, i18nDescription.toString());
        }
       
        int minOccurs = param.getMinOccurs();
        if (minOccurs > -1) {
            obj.put("minOccurs", obj, minOccurs);
        }

        int maxOccurs = param.getMaxOccurs();
        if (maxOccurs > -1) {
            obj.put("maxOccurs", obj, maxOccurs);
        }
       
        Object defaultValue = param.getDefaultValue();
        if (defaultValue != null) {
            obj.put("defaultValue", obj, defaultValue);
        }

        return obj;
    }
View Full Code Here

                throw ScriptRuntime.constructError("Error", "Cannot create schema from provided value: " + Context.toString(schemaObj));
            }
        }

        Object propertiesObj = getOptionalMember(config, "properties", NativeObject.class, "Object");
        NativeObject properties = null;
        if (propertiesObj != null) {
            properties = (NativeObject) propertiesObj;
        }

        if (schema == null) {
            if (properties != null) {
                schema = Schema.fromValues(scope, properties);
            } else {
                throw ScriptRuntime.constructError("Error", "Feature config must include schema or properties.");
            }
        }

        SimpleFeatureBuilder builder = new SimpleFeatureBuilder((SimpleFeatureType) schema.unwrap());

        if (properties != null) {
            Object[] names = properties.getIds();
            for (Object nameObj : names) {
                String name = (String) nameObj;
                if (schema.get(name) == null) {
                    throw ScriptRuntime.constructError("Error", "Feature schema has no field with the given name: " + name);
                }
                Object value = properties.get(name, properties);
                builder.set(name, jsToJava(value));
            }
        }
       
        String id = null;
View Full Code Here

     * @return
     */
    private NativeObject prepConfig(NativeObject config) {
        Scriptable scope = config.getParentScope();
        Object propertiesObj = getOptionalMember(config, "properties", NativeObject.class, "Object");
        NativeObject properties = null;
        if (propertiesObj != null) {
            properties = (NativeObject) propertiesObj;
        }

        Geometry geometry = null;
        if (config.has("geometry", config)) {
            // GeoJSON config
            Object geometryObj = config.get("geometry", config);
            if (!(geometryObj instanceof NativeObject)) {
                throw ScriptRuntime.constructError("Error",
                        "Expected geometry member to be an object, got: " + Context.toString(geometryObj));
            }
            geometryObj = JSON.readObj((NativeObject) geometryObj);
            if (!(geometryObj instanceof Geometry)) {
                throw ScriptRuntime.constructError("Error", "Expected geometry memeber to be a valid geometry, got: " + Context.toString(geometryObj));
            }
            geometry = (Geometry) geometryObj;
            config.delete("geometry");
            // allow passing a geometry only with no other properties
            if (properties == null) {
                Context cx = Context.enter();
                try {
                    properties = (NativeObject) cx.newObject(scope);
                } finally {
                    Context.exit();
                }
                config.put("properties", config, properties);
            }
        }
        if (geometry != null && properties != null) {
            properties.put("geometry", properties, geometry);
        }
        return config;
    }
View Full Code Here

            throw ScriptRuntime.constructError("Error", "Constructor takes a single argument");
        }
        Feature feature = null;
        Object arg = args[0];
        if (arg instanceof NativeObject) {
            NativeObject config = (NativeObject) arg;
            if (inNewExpr) {
                feature = new Feature(config);
            } else {
                feature = new Feature(config.getParentScope(), config);
            }
        } else {
            throw ScriptRuntime.constructError("Error", "Could not create feature from argument: " + Context.toString(arg));
        }
        return feature;
View Full Code Here

TOP

Related Classes of org.mozilla.javascript.NativeObject

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.