use of com.google.refine.expr.EvalError in project OpenRefine by OpenRefine.
the class Diff 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 && o2 != null) {
if (args.length == 2 && o1 instanceof String && o2 instanceof String) {
return StringUtils.difference((String) o1, (String) o2);
} else if ((o1 instanceof Date || o1 instanceof Calendar) && args.length == 3) {
Object o3 = args[2];
if (o3 != null && o3 instanceof String) {
try {
String unit = ((String) o3).toLowerCase();
Date c1 = (o1 instanceof Date) ? (Date) o1 : ((Calendar) o1).getTime();
Date c2;
if (o2 instanceof Date) {
c2 = (Date) o2;
} else if (o2 instanceof Calendar) {
c2 = ((Calendar) o2).getTime();
} else {
c2 = CalendarParser.parse((o2 instanceof String) ? (String) o2 : o2.toString()).getTime();
}
long delta = (c1.getTime() - c2.getTime()) / 1000;
if ("seconds".equals(unit)) {
return delta;
}
delta /= 60;
if ("minutes".equals(unit)) {
return delta;
}
delta /= 60;
if ("hours".equals(unit)) {
return delta;
}
long days = delta / 24;
if ("days".equals(unit)) {
return days;
}
if ("weeks".equals(unit)) {
return days / 7;
}
if ("months".equals(unit)) {
return days / 30;
}
if ("years".equals(unit)) {
return days / 365;
}
return new EvalError("Unknown time unit " + unit);
} catch (CalendarParserException e) {
return new EvalError(e);
}
}
}
}
}
return new EvalError("Unexpected arguments - expecting either 2 strings or 2 dates and a unit string");
}
use of com.google.refine.expr.EvalError in project OpenRefine by OpenRefine.
the class Match method call.
@Override
public Object call(Properties bindings, Object[] args) {
if (args.length == 2) {
Object s = args[0];
Object p = args[1];
if (s != null && p != null && (p instanceof String || p instanceof Pattern)) {
Pattern pattern = (p instanceof String) ? Pattern.compile((String) p) : (Pattern) p;
Matcher matcher = pattern.matcher(s.toString());
if (matcher.matches()) {
int count = matcher.groupCount();
String[] groups = new String[count];
for (int i = 0; i < count; i++) {
groups[i] = matcher.group(i + 1);
}
return groups;
} else {
return null;
}
}
return null;
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a string or a regexp");
}
use of com.google.refine.expr.EvalError in project OpenRefine by OpenRefine.
the class Reinterpret method reinterpret.
private Object reinterpret(String str, String decoder, String encoder) {
String result = null;
byte[] bytes;
if (decoder == null || decoder.isEmpty()) {
bytes = str.getBytes();
} else {
try {
bytes = str.getBytes(decoder);
} catch (UnsupportedEncodingException e) {
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + ": source encoding '" + decoder + "' is not available or recognized.");
}
}
try {
if (encoder == null || encoder.isEmpty()) {
// system default encoding
result = new String(bytes);
} else {
result = new String(bytes, encoder);
}
} catch (UnsupportedEncodingException e) {
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + ": encoding '" + encoder + "' is not available or recognized.");
}
return result;
}
Aggregations