use of com.google.refine.expr.ParsingException in project OpenRefine by OpenRefine.
the class PreviewExpressionCommand method doPost.
/**
* The command uses POST but does not actually modify any state so it does
* not require CSRF.
*/
@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) {
respondJSON(response, new PreviewResult("error", "No row indices specified", null));
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) {
}
}
List<Integer> rowIndices = ParsingUtilities.mapper.readValue(rowIndicesString, new TypeReference<List<Integer>>() {
});
int length = rowIndices.size();
try {
Evaluable eval = MetaParser.parse(expression);
List<ExpressionValue> evaluated = new ArrayList<>();
Properties bindings = ExpressionUtils.createBindings(project);
for (int i = 0; i < length; i++) {
Object result = null;
int rowIndex = rowIndices.get(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) {
evaluated.add(null);
} else if (ExpressionUtils.isError(result)) {
evaluated.add(new ErrorMessage(((EvalError) result).message));
} else {
StringBuffer sb = new StringBuffer();
writeValue(sb, result, false);
evaluated.add(new SuccessfulEvaluation(sb.toString()));
}
}
respondJSON(response, new PreviewResult(evaluated));
} catch (ParsingException e) {
respondJSON(response, new PreviewResult("error", e.getMessage(), "parser"));
} catch (Exception e) {
respondJSON(response, new PreviewResult("error", e.getMessage(), "other"));
}
} catch (Exception e) {
respondException(response, e);
}
}
use of com.google.refine.expr.ParsingException in project OpenRefine by OpenRefine.
the class GrelTests method testCrossFunctionEval.
// to demonstrate bug fixing for #1204
@Test
public void testCrossFunctionEval() {
String test = "cross(\"Mary\", \"My Address Book\", \"friend\")";
try {
Evaluable eval = MetaParser.parse("grel:" + test);
Object result = eval.evaluate(bindings);
Assert.assertTrue(result instanceof EvalError);
} catch (ParsingException e) {
Assert.fail("Unexpected parse failure for cross function: " + test);
}
}
Aggregations