Search in sources :

Example 1 with HasFields

use of com.google.refine.expr.HasFields in project OpenRefine by OpenRefine.

the class Get method call.

@Override
public Object call(Properties bindings, Object[] args) {
    if (args.length > 1 && args.length <= 3) {
        Object v = args[0];
        Object from = args[1];
        Object to = (args.length == 3) ? args[2] : null;
        if (v != null && from != null) {
            if (v instanceof HasFields && from instanceof String) {
                return ((HasFields) v).getField((String) from, bindings);
            } else if (v instanceof JSONObject && from instanceof String) {
                try {
                    return ((JSONObject) v).get((String) from);
                } catch (JSONException e) {
                // ignore; will return null
                }
            } else {
                if (from instanceof Number && (to == null || to instanceof Number)) {
                    if (v.getClass().isArray() || v instanceof List<?> || v instanceof HasFieldsList || v instanceof JSONArray) {
                        int length = 0;
                        if (v.getClass().isArray()) {
                            length = ((Object[]) v).length;
                        } else if (v instanceof HasFieldsList) {
                            length = ((HasFieldsList) v).length();
                        } else if (v instanceof JSONArray) {
                            length = ((JSONArray) v).length();
                        } else {
                            length = ExpressionUtils.toObjectList(v).size();
                        }
                        int start = ((Number) from).intValue();
                        if (start < 0) {
                            start = length + start;
                        }
                        start = Math.min(length, Math.max(0, start));
                        if (to == null) {
                            if (v.getClass().isArray()) {
                                return ((Object[]) v)[start];
                            } else if (v instanceof HasFieldsList) {
                                return ((HasFieldsList) v).get(start);
                            } else if (v instanceof JSONArray) {
                                try {
                                    return ((JSONArray) v).get(start);
                                } catch (JSONException e) {
                                // ignore; will return null
                                }
                            } else {
                                return ExpressionUtils.toObjectList(v).get(start);
                            }
                        } else {
                            int end = ((Number) to).intValue();
                            if (end < 0) {
                                end = length + end;
                            }
                            end = Math.min(length, Math.max(start, end));
                            if (end > start) {
                                if (v.getClass().isArray()) {
                                    Object[] a2 = new Object[end - start];
                                    System.arraycopy(v, start, a2, 0, end - start);
                                    return a2;
                                } else if (v instanceof HasFieldsList) {
                                    return ((HasFieldsList) v).getSubList(start, end);
                                } else if (v instanceof JSONArray) {
                                    JSONArray a = (JSONArray) v;
                                    Object[] a2 = new Object[end - start];
                                    for (int i = 0; i < a2.length; i++) {
                                        try {
                                            a2[i] = a.get(start + i);
                                        } catch (JSONException e) {
                                        // ignore
                                        }
                                    }
                                    return a2;
                                } else {
                                    return ExpressionUtils.toObjectList(v).subList(start, end);
                                }
                            }
                        }
                    } else {
                        String s = (v instanceof String) ? (String) v : v.toString();
                        int start = ((Number) from).intValue();
                        if (start < 0) {
                            start = s.length() + start;
                        }
                        start = Math.min(s.length(), Math.max(0, start));
                        if (to != null) {
                            int end = ((Number) to).intValue();
                            if (end < 0) {
                                end = s.length() + end;
                            }
                            end = Math.min(s.length(), Math.max(start, end));
                            return s.substring(start, end);
                        } else {
                            return s.substring(start, start + 1);
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : HasFieldsList(com.google.refine.expr.HasFieldsList) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) List(java.util.List) HasFieldsList(com.google.refine.expr.HasFieldsList) HasFields(com.google.refine.expr.HasFields)

Example 2 with HasFields

use of com.google.refine.expr.HasFields in project OpenRefine by OpenRefine.

the class JythonHasFieldsWrapper method __finditem__.

@Override
public PyObject __finditem__(PyObject key) {
    String k = (String) key.__tojava__(String.class);
    Object v = _obj.getField(k, _bindings);
    if (v != null) {
        if (v instanceof PyObject) {
            return (PyObject) v;
        } else if (v instanceof HasFields) {
            return new JythonHasFieldsWrapper((HasFields) v, _bindings);
        } else if (Py.getAdapter().canAdapt(v)) {
            return Py.java2py(v);
        } else {
            return new JythonObjectWrapper(v);
        }
    } else {
        return null;
    }
}
Also used : PyObject(org.python.core.PyObject) HasFields(com.google.refine.expr.HasFields) PyObject(org.python.core.PyObject)

Example 3 with HasFields

use of com.google.refine.expr.HasFields in project OpenRefine by OpenRefine.

the class HasField method call.

@Override
public Object call(Properties bindings, Object[] args) {
    if (args.length > 1 && args.length <= 2) {
        Object v = args[0];
        Object f = args[1];
        if (v != null && f != null && f instanceof String) {
            String name = (String) f;
            if (v instanceof HasFields) {
                return ((HasFields) v).getField(name, bindings) != null;
            } else if (v instanceof JSONObject) {
                try {
                    return ((JSONObject) v).get(name) != null;
                } catch (JSONException e) {
                // ignore; will return false
                }
            }
        }
    }
    return false;
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) HasFields(com.google.refine.expr.HasFields)

Aggregations

HasFields (com.google.refine.expr.HasFields)3 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 HasFieldsList (com.google.refine.expr.HasFieldsList)1 List (java.util.List)1 JSONArray (org.json.JSONArray)1 PyObject (org.python.core.PyObject)1