use of com.google.refine.expr.EvalError in project OpenRefine by OpenRefine.
the class ForRange method call.
@Override
public Object call(Properties bindings, Evaluable[] args) {
Object fromO = args[0].evaluate(bindings);
Object toO = args[1].evaluate(bindings);
Object stepO = args[2].evaluate(bindings);
if (ExpressionUtils.isError(fromO)) {
return fromO;
} else if (ExpressionUtils.isError(toO)) {
return toO;
} else if (ExpressionUtils.isError(stepO)) {
return stepO;
} else if (!(fromO instanceof Number) || !(toO instanceof Number) || !(stepO instanceof Number)) {
return new EvalError("First, second, and third arguments of forRange must all be numbers");
}
String indexName = ((VariableExpr) args[3]).getName();
Object oldIndexValue = bindings.get(indexName);
try {
List<Object> results = new ArrayList<Object>();
if (isIntegral((Number) fromO) && isIntegral((Number) stepO)) {
long from = ((Number) fromO).longValue();
long step = ((Number) stepO).longValue();
double to = ((Number) toO).doubleValue();
while (from < to) {
bindings.put(indexName, from);
Object r = args[4].evaluate(bindings);
results.add(r);
from += step;
}
} else {
double from = ((Number) fromO).longValue();
double step = ((Number) stepO).longValue();
double to = ((Number) toO).doubleValue();
while (from < to) {
bindings.put(indexName, from);
Object r = args[4].evaluate(bindings);
results.add(r);
from += step;
}
}
return results.toArray();
} finally {
/*
* Restore the old values bound to the variables, if any.
*/
if (oldIndexValue != null) {
bindings.put(indexName, oldIndexValue);
} else {
bindings.remove(indexName);
}
}
}
use of com.google.refine.expr.EvalError in project OpenRefine by OpenRefine.
the class ToDate method call.
@Override
public Object call(Properties bindings, Object[] args) {
if (args.length == 0) {
// missing value, can this happen?
return null;
}
Object arg0 = args[0];
String o1;
if (arg0 instanceof Date) {
return arg0;
} else if (arg0 instanceof Calendar) {
return ((Calendar) arg0).getTime();
} else if (arg0 instanceof Long) {
// treat integers as years
o1 = ((Long) arg0).toString();
} else if (arg0 instanceof String) {
o1 = (String) arg0;
} else {
// ignore cell values that aren't strings
return new EvalError("Not a String - cannot parse to date");
}
// "o, boolean month_first (optional)"
if (args.length == 1 || (args.length == 2 && args[1] instanceof Boolean)) {
boolean month_first = true;
if (args.length == 2) {
month_first = (Boolean) args[1];
}
try {
return CalendarParser.parse(o1, (month_first) ? CalendarParser.MM_DD_YY : CalendarParser.DD_MM_YY);
} catch (CalendarParserException e) {
Date d = ParsingUtilities.stringToDate(o1);
if (d != null) {
return d;
} else {
try {
return javax.xml.bind.DatatypeConverter.parseDateTime(o1).getTime();
} catch (IllegalArgumentException e2) {
}
// alternate implementation which may be useful on some JVMs?
// try {
// return javax.xml.datatype.DatatypeFactory.newInstance().newXMLGregorianCalendar(o1).toGregorianCalendar().getTime();
// } catch (DatatypeConfigurationException e2) {
// }
}
return new EvalError("Cannot parse to date");
}
}
// "o, format1, format2 (optional), ..."
Locale locale = Locale.getDefault();
if (args.length >= 2) {
for (int i = 1; i < args.length; i++) {
if (!(args[i] instanceof String)) {
// skip formats that aren't strings
continue;
}
String format = StringUtils.trim((String) args[i]);
DateFormat formatter;
// Attempt to parse first string as a language tag
if (i == 1) {
// Locale possibleLocale = Locale.forLanguageTag(format); // Java 1.7+ only
Locale possibleLocale;
int c = format.indexOf('_');
if (c > 0) {
possibleLocale = new Locale(format.substring(0, c), format.substring(c + 1));
} else {
possibleLocale = new Locale(format);
}
boolean valid = false;
for (Locale l : DateFormat.getAvailableLocales()) {
if (l.equals(possibleLocale)) {
locale = possibleLocale;
valid = true;
break;
}
}
if (valid) {
// If we got a valid locale
if (args.length == 2) {
// No format strings to try, process using default
formatter = DateFormat.getDateInstance(DateFormat.DEFAULT, locale);
formatter.setLenient(true);
GregorianCalendar date = parse(o1, formatter);
if (date != null) {
return date;
} else {
return new EvalError("Unable to parse as date");
}
}
// Don't try to process locale string as a format string if it was valid
continue;
}
}
try {
formatter = new SimpleDateFormat(format, locale);
} catch (IllegalArgumentException e) {
return new EvalError("Unknown date format");
}
formatter.setLenient(true);
GregorianCalendar date = parse(o1, formatter);
if (date != null) {
return date;
}
}
return new EvalError("Unable to parse as date");
}
return null;
}
use of com.google.refine.expr.EvalError in project OpenRefine by OpenRefine.
the class FunctionCallExpr method evaluate.
@Override
public Object evaluate(Properties bindings) {
Object[] args = new Object[_args.length];
for (int i = 0; i < _args.length; i++) {
Object v = _args[i].evaluate(bindings);
if (ExpressionUtils.isError(v)) {
// bubble up the error
return v;
}
args[i] = v;
}
try {
return _function.call(bindings, args);
} catch (Exception e) {
return new EvalError(e);
}
}
use of com.google.refine.expr.EvalError in project OpenRefine by OpenRefine.
the class Uniques 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<?>) {
Set<Object> set = null;
if (v.getClass().isArray()) {
Object[] a = (Object[]) v;
set = new HashSet<Object>(a.length);
for (Object element : a) {
set.add(element);
}
} else {
set = new HashSet<Object>(ExpressionUtils.toObjectList(v));
}
return set.toArray();
}
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects an array");
}
use of com.google.refine.expr.EvalError in project OpenRefine by OpenRefine.
the class Inc method call.
@Override
public Object call(Properties bindings, Object[] args) {
if (args.length == 3 && args[0] != null && (args[0] instanceof Calendar || args[0] instanceof Date) && args[1] != null && args[1] instanceof Number && args[2] != null && args[2] instanceof String) {
Calendar date;
if (args[0] instanceof Calendar) {
// must copy so not to modify original
date = (Calendar) ((Calendar) args[0]).clone();
} else {
date = Calendar.getInstance();
date.setTime((Date) args[0]);
}
int amount = ((Number) args[1]).intValue();
String unit = (String) args[2];
date.add(getField(unit), amount);
return date;
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a date, a number and a string");
}
Aggregations