Search in sources :

Example 6 with EvalError

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

the class ForEach method call.

@Override
public Object call(Properties bindings, Evaluable[] args) {
    Object o = args[0].evaluate(bindings);
    if (ExpressionUtils.isError(o)) {
        return o;
    } else if (!ExpressionUtils.isArrayOrCollection(o) && !(o instanceof JSONArray)) {
        return new EvalError("First argument to forEach is not an array");
    }
    String name = ((VariableExpr) args[1]).getName();
    Object oldValue = bindings.get(name);
    try {
        List<Object> results = null;
        if (o.getClass().isArray()) {
            Object[] values = (Object[]) o;
            results = new ArrayList<Object>(values.length);
            for (Object v : values) {
                if (v != null) {
                    bindings.put(name, v);
                } else {
                    bindings.remove(name);
                }
                Object r = args[2].evaluate(bindings);
                results.add(r);
            }
        } else if (o instanceof JSONArray) {
            JSONArray a = (JSONArray) o;
            int l = a.length();
            results = new ArrayList<Object>(l);
            for (int i = 0; i < l; i++) {
                try {
                    Object v = a.get(i);
                    if (v != null) {
                        bindings.put(name, v);
                    } else {
                        bindings.remove(name);
                    }
                    Object r = args[2].evaluate(bindings);
                    results.add(r);
                } catch (JSONException e) {
                    results.add(new EvalError(e.getMessage()));
                }
            }
        } else {
            Collection<Object> collection = ExpressionUtils.toObjectCollection(o);
            results = new ArrayList<Object>(collection.size());
            for (Object v : collection) {
                if (v != null) {
                    bindings.put(name, v);
                } else {
                    bindings.remove(name);
                }
                Object r = args[2].evaluate(bindings);
                results.add(r);
            }
        }
        return results.toArray();
    } finally {
        /*
             *  Restore the old value bound to the variable, if any.
             */
        if (oldValue != null) {
            bindings.put(name, oldValue);
        } else {
            bindings.remove(name);
        }
    }
}
Also used : JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) Collection(java.util.Collection) EvalError(com.google.refine.expr.EvalError) VariableExpr(com.google.refine.grel.ast.VariableExpr)

Example 7 with EvalError

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

the class Reverse method call.

@Override
public Object call(Properties bindings, Object[] args) {
    if (args.length == 1) {
        Object v = args[0];
        if (v != null) {
            if (v instanceof JSONArray) {
                try {
                    v = JSONUtilities.toArray((JSONArray) v);
                } catch (JSONException e) {
                    return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " fails to process a JSON array: " + e.getMessage());
                }
            }
            if (v.getClass().isArray() || v instanceof List<?>) {
                int length = v.getClass().isArray() ? ((Object[]) v).length : ExpressionUtils.toObjectList(v).size();
                Object[] r = new Object[length];
                if (v.getClass().isArray()) {
                    Object[] a = (Object[]) v;
                    for (int i = 0; i < length; i++) {
                        r[i] = a[r.length - i - 1];
                    }
                } else {
                    List<Object> a = ExpressionUtils.toObjectList(v);
                    for (int i = 0; i < length; i++) {
                        r[i] = a.get(r.length - i - 1);
                    }
                }
                return r;
            }
        }
    }
    return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects an array");
}
Also used : JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) EvalError(com.google.refine.expr.EvalError)

Example 8 with EvalError

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

the class DatePart method call.

@Override
public Object call(Properties bindings, Object[] args) {
    if (args.length == 2 && args[0] != null && (args[0] instanceof Calendar || args[0] instanceof Date) && args[1] != null && args[1] instanceof String) {
        String part = (String) args[1];
        if (args[0] instanceof Calendar) {
            return getPart((Calendar) args[0], part);
        } else {
            Calendar c = Calendar.getInstance();
            c.setTime((Date) args[0]);
            return getPart(c, part);
        }
    }
    return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a date and a string");
}
Also used : Calendar(java.util.Calendar) EvalError(com.google.refine.expr.EvalError) Date(java.util.Date)

Example 9 with EvalError

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

the class HtmlAttr method call.

@Override
public Object call(Properties bindings, Object[] args) {
    if (args.length >= 2) {
        Object o1 = args[0];
        Object o2 = args[1];
        if (o1 != null && o1 instanceof Element) {
            Element e1 = (Element) o1;
            if (o2 != null && o2 instanceof String) {
                return e1.attr(o2.toString());
            }
        } else {
            return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " failed as the first parameter is not an HTML Element.  Please first use parseHtml(string) and select(query) prior to using this function");
        }
    }
    return null;
}
Also used : Element(org.jsoup.nodes.Element) EvalError(com.google.refine.expr.EvalError)

Example 10 with EvalError

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

the class SelectHtml method call.

@Override
public Object call(Properties bindings, Object[] args) {
    if (args.length >= 2) {
        Object o1 = args[0];
        Object o2 = args[1];
        if (o1 != null && o1 instanceof Element) {
            Element e1 = (Element) o1;
            if (o2 != null && o2 instanceof String) {
                return e1.select(o2.toString());
            }
        } else {
            return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " failed as the first parameter is not an HTML Element.  Please first use parseHtml(string)");
        }
    }
    return null;
}
Also used : Element(org.jsoup.nodes.Element) EvalError(com.google.refine.expr.EvalError)

Aggregations

EvalError (com.google.refine.expr.EvalError)23 JSONArray (org.json.JSONArray)6 JSONException (org.json.JSONException)6 VariableExpr (com.google.refine.grel.ast.VariableExpr)4 ArrayList (java.util.ArrayList)4 Calendar (java.util.Calendar)4 Date (java.util.Date)4 Evaluable (com.google.refine.expr.Evaluable)3 ParsingException (com.google.refine.expr.ParsingException)3 Project (com.google.refine.model.Project)3 WrappedCell (com.google.refine.expr.WrappedCell)2 CalendarParserException (com.google.refine.expr.util.CalendarParserException)2 IOException (java.io.IOException)2 Collection (java.util.Collection)2 Element (org.jsoup.nodes.Element)2 CSVParser (au.com.bytecode.opencsv.CSVParser)1 ProjectJoin (com.google.refine.InterProjectModel.ProjectJoin)1 ProjectMetadata (com.google.refine.ProjectMetadata)1 Engine (com.google.refine.browsing.Engine)1 ExpressionNominalValueGrouper (com.google.refine.browsing.util.ExpressionNominalValueGrouper)1