use of com.google.refine.expr.EvalError in project OpenRefine by OpenRefine.
the class SmartSplit method call.
@Override
public Object call(Properties bindings, Object[] args) {
if (args.length >= 1 && args.length <= 2) {
CSVParser parser = null;
Object v = args[0];
String s = v.toString();
if (args.length > 1) {
String sep = args[1].toString();
parser = new CSVParser(sep.charAt(0), CSVParser.DEFAULT_QUOTE_CHARACTER, CSVParser.DEFAULT_ESCAPE_CHARACTER, CSVParser.DEFAULT_STRICT_QUOTES, CSVParser.DEFAULT_IGNORE_LEADING_WHITESPACE, false);
}
if (parser == null) {
int tab = s.indexOf('\t');
if (tab >= 0) {
parser = s_tabParser;
} else {
parser = s_commaParser;
}
}
try {
return parser.parseLine(s);
} catch (IOException e) {
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " error: " + e.getMessage());
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 1 or 2 strings");
}
use of com.google.refine.expr.EvalError in project OpenRefine by OpenRefine.
the class ToTitlecase method call.
@Override
public Object call(Properties bindings, Object[] args) {
if (args.length == 1 && args[0] != null) {
Object o = args[0];
String s = o instanceof String ? (String) o : o.toString();
return WordUtils.capitalizeFully(s, delimiters);
} else if (args.length == 2 && args[0] != null && args[1] != null) {
Object o = args[0];
String s = o instanceof String ? (String) o : o.toString();
o = args[1];
String delims = o instanceof String ? (String) o : o.toString();
return WordUtils.capitalizeFully(s, delims.toCharArray());
} else {
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a one or two strings");
}
}
use of com.google.refine.expr.EvalError in project OpenRefine by OpenRefine.
the class Cross method call.
@Override
public Object call(Properties bindings, Object[] args) {
if (args.length == 3) {
// from project is implied
// from cell
Object wrappedCell = args[0];
Object toProjectName = args[1];
Object toColumnName = args[2];
if (wrappedCell != null && wrappedCell instanceof WrappedCell && toProjectName != null && toProjectName instanceof String && toColumnName != null && toColumnName instanceof String) {
ProjectJoin join = ProjectManager.singleton.getInterProjectModel().getJoin(ProjectManager.singleton.getProjectMetadata(((Project) bindings.get("project")).id).getName(), ((WrappedCell) wrappedCell).columnName, (String) toProjectName, (String) toColumnName);
return join.getRows(((WrappedCell) wrappedCell).cell.value);
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a cell, a project name to join with, and a column name in that project");
}
use of com.google.refine.expr.EvalError in project OpenRefine by OpenRefine.
the class FacetCount method call.
@Override
public Object call(Properties bindings, Object[] args) {
if (args.length == 3 && args[1] instanceof String && args[2] instanceof String) {
// choice value to look up
Object choiceValue = args[0];
String facetExpression = (String) args[1];
String columnName = (String) args[2];
Project project = (Project) bindings.get("project");
Column column = project.columnModel.getColumnByName(columnName);
if (column == null) {
return new EvalError("No such column named " + columnName);
}
String key = "nominal-bin:" + facetExpression;
ExpressionNominalValueGrouper grouper = (ExpressionNominalValueGrouper) column.getPrecompute(key);
if (grouper == null) {
try {
Evaluable eval = MetaParser.parse(facetExpression);
Engine engine = new Engine(project);
grouper = new ExpressionNominalValueGrouper(eval, columnName, column.getCellIndex());
engine.getAllRows().accept(project, grouper);
column.setPrecompute(key, grouper);
} catch (ParsingException e) {
return new EvalError("Error parsing facet expression " + facetExpression);
}
}
return grouper.getChoiceValueCountMultiple(choiceValue);
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a choice value, an expression as a string, and a column name");
}
use of com.google.refine.expr.EvalError in project OpenRefine by OpenRefine.
the class Filter 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 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);
if (r instanceof Boolean && ((Boolean) r).booleanValue()) {
results.add(v);
}
}
} 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);
if (r instanceof Boolean && ((Boolean) r).booleanValue()) {
results.add(v);
}
} 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);
if (r instanceof Boolean && ((Boolean) r).booleanValue()) {
results.add(v);
}
}
}
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);
}
}
}
Aggregations