use of com.google.refine.model.Cell in project OpenRefine by OpenRefine.
the class DualExpressionsNumberComparisonRowFilter method filterRow.
@Override
public boolean filterRow(Project project, int rowIndex, Row row) {
Cell x_cell = _x_cellIndex < 0 ? null : row.getCell(_x_cellIndex);
Properties x_bindings = ExpressionUtils.createBindings(project);
ExpressionUtils.bind(x_bindings, row, rowIndex, _x_columnName, x_cell);
Object x_value = _x_evaluable.evaluate(x_bindings);
Cell y_cell = _y_cellIndex < 0 ? null : row.getCell(_y_cellIndex);
Properties y_bindings = ExpressionUtils.createBindings(project);
ExpressionUtils.bind(y_bindings, row, rowIndex, _y_columnName, y_cell);
Object y_value = _y_evaluable.evaluate(y_bindings);
if (x_value != null && y_value != null) {
if (x_value.getClass().isArray() || y_value.getClass().isArray()) {
return false;
} else if (x_value instanceof Collection<?> || y_value instanceof Collection<?>) {
return false;
}
// else, fall through
}
return checkValue(x_value, y_value);
}
use of com.google.refine.model.Cell in project OpenRefine by OpenRefine.
the class ExpressionEqualRowFilter method internalInvertedFilterRow.
public boolean internalInvertedFilterRow(Project project, int rowIndex, Row row) {
Cell cell = _cellIndex < 0 ? null : row.getCell(_cellIndex);
Properties bindings = ExpressionUtils.createBindings(project);
ExpressionUtils.bind(bindings, row, rowIndex, _columnName, cell);
Object value = _evaluable.evaluate(bindings);
if (value != null) {
if (value.getClass().isArray()) {
Object[] a = (Object[]) value;
for (Object v : a) {
if (testValue(v)) {
return false;
}
}
return true;
} else if (value instanceof Collection<?>) {
for (Object v : ExpressionUtils.toObjectCollection(value)) {
if (testValue(v)) {
return false;
}
}
return true;
} else if (value instanceof JSONArray) {
JSONArray a = (JSONArray) value;
int l = a.length();
for (int i = 0; i < l; i++) {
try {
if (testValue(a.get(i))) {
return false;
}
} catch (JSONException e) {
// ignore
}
}
return true;
}
// else, fall through
}
return !testValue(value);
}
use of com.google.refine.model.Cell in project OpenRefine by OpenRefine.
the class ExpressionStringComparisonRowFilter method filterRow.
@Override
public boolean filterRow(Project project, int rowIndex, Row row) {
Cell cell = _cellIndex < 0 ? null : row.getCell(_cellIndex);
Properties bindings = ExpressionUtils.createBindings(project);
ExpressionUtils.bind(bindings, row, rowIndex, _columnName, cell);
Object value = _evaluable.evaluate(bindings);
if (value != null) {
if (value.getClass().isArray()) {
Object[] a = (Object[]) value;
for (Object v : a) {
if (checkValue(v instanceof String ? ((String) v) : v.toString())) {
return true;
}
}
} else if (value instanceof Collection<?>) {
for (Object v : ExpressionUtils.toObjectCollection(value)) {
if (checkValue(v.toString())) {
return true;
}
}
return false;
} else if (value instanceof JSONArray) {
JSONArray a = (JSONArray) value;
int l = a.length();
for (int i = 0; i < l; i++) {
try {
if (checkValue(a.get(i).toString())) {
return true;
}
} catch (JSONException e) {
// ignore
}
}
return false;
} else {
if (checkValue(value instanceof String ? ((String) value) : value.toString())) {
return true;
}
}
}
return false;
}
use of com.google.refine.model.Cell in project OpenRefine by OpenRefine.
the class ExpressionBasedRowEvaluable method eval.
@Override
public Object eval(Project project, int rowIndex, Row row, Properties bindings) {
Cell cell = row.getCell(_cellIndex);
ExpressionUtils.bind(bindings, row, rowIndex, _columnName, cell);
return _eval.evaluate(bindings);
}
use of com.google.refine.model.Cell in project OpenRefine by OpenRefine.
the class PreviewExpressionCommand method doPost.
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
Project project = getProject(request);
int cellIndex = Integer.parseInt(request.getParameter("cellIndex"));
String columnName = cellIndex < 0 ? "" : project.columnModel.getColumnByCellIndex(cellIndex).getName();
String expression = request.getParameter("expression");
String rowIndicesString = request.getParameter("rowIndices");
if (rowIndicesString == null) {
respond(response, "{ \"code\" : \"error\", \"message\" : \"No row indices specified\" }");
return;
}
boolean repeat = "true".equals(request.getParameter("repeat"));
int repeatCount = 10;
if (repeat) {
String repeatCountString = request.getParameter("repeatCount");
try {
repeatCount = Math.max(Math.min(Integer.parseInt(repeatCountString), 10), 0);
} catch (Exception e) {
}
}
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
JSONArray rowIndices = ParsingUtilities.evaluateJsonStringToArray(rowIndicesString);
int length = rowIndices.length();
JSONWriter writer = new JSONWriter(response.getWriter());
writer.object();
try {
Evaluable eval = MetaParser.parse(expression);
writer.key("code");
writer.value("ok");
writer.key("results");
writer.array();
Properties bindings = ExpressionUtils.createBindings(project);
for (int i = 0; i < length; i++) {
Object result = null;
int rowIndex = rowIndices.getInt(i);
if (rowIndex >= 0 && rowIndex < project.rows.size()) {
Row row = project.rows.get(rowIndex);
Cell cell = row.getCell(cellIndex);
try {
ExpressionUtils.bind(bindings, row, rowIndex, columnName, cell);
result = eval.evaluate(bindings);
if (repeat) {
for (int r = 0; r < repeatCount && ExpressionUtils.isStorable(result); r++) {
Cell newCell = new Cell((Serializable) result, (cell != null) ? cell.recon : null);
ExpressionUtils.bind(bindings, row, rowIndex, columnName, newCell);
Object newResult = eval.evaluate(bindings);
if (ExpressionUtils.isError(newResult)) {
break;
} else if (ExpressionUtils.sameValue(result, newResult)) {
break;
} else {
result = newResult;
}
}
}
} catch (Exception e) {
// ignore
}
}
if (result == null) {
writer.value(null);
} else if (ExpressionUtils.isError(result)) {
writer.object();
writer.key("message");
writer.value(((EvalError) result).message);
writer.endObject();
} else {
StringBuffer sb = new StringBuffer();
writeValue(sb, result, false);
writer.value(sb.toString());
}
}
writer.endArray();
} catch (ParsingException e) {
writer.key("code");
writer.value("error");
writer.key("type");
writer.value("parser");
writer.key("message");
writer.value(e.getMessage());
} catch (Exception e) {
writer.key("code");
writer.value("error");
writer.key("type");
writer.value("other");
writer.key("message");
writer.value(e.getMessage());
}
writer.endObject();
} catch (Exception e) {
respondException(response, e);
}
}
Aggregations