Search in sources :

Example 1 with HasFieldsList

use of com.google.refine.expr.HasFieldsList 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 HasFieldsList

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

the class Slice 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 && 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();
                int end = (to != null) ? ((Number) to).intValue() : length;
                if (start < 0) {
                    start = length + start;
                }
                start = Math.min(length, Math.max(0, start));
                if (end < 0) {
                    end = length + end;
                }
                end = Math.min(length, Math.max(start, end));
                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);
                }
            }
        }
    }
    return null;
}
Also used : HasFieldsList(com.google.refine.expr.HasFieldsList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Aggregations

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