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);
}
}
}
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");
}
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");
}
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;
}
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;
}
Aggregations