Search in sources :

Example 16 with ParsingException

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);
    }
}
Also used : ArrayList(java.util.ArrayList) Properties(java.util.Properties) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ParsingException(com.google.refine.expr.ParsingException) Project(com.google.refine.model.Project) Evaluable(com.google.refine.expr.Evaluable) ParsingException(com.google.refine.expr.ParsingException) ArrayList(java.util.ArrayList) List(java.util.List) WrappedRow(com.google.refine.expr.WrappedRow) Row(com.google.refine.model.Row) Cell(com.google.refine.model.Cell) WrappedCell(com.google.refine.expr.WrappedCell)

Example 17 with ParsingException

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);
    }
}
Also used : Evaluable(com.google.refine.expr.Evaluable) ParsingException(com.google.refine.expr.ParsingException) EvalError(com.google.refine.expr.EvalError) RefineTest(com.google.refine.RefineTest) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

ParsingException (com.google.refine.expr.ParsingException)17 Evaluable (com.google.refine.expr.Evaluable)9 Column (com.google.refine.model.Column)8 NumericBinIndex (com.google.refine.browsing.util.NumericBinIndex)6 EvalError (com.google.refine.expr.EvalError)4 Test (org.testng.annotations.Test)4 RefineTest (com.google.refine.RefineTest)3 FilteredRows (com.google.refine.browsing.FilteredRows)3 Color (java.awt.Color)3 BeforeTest (org.testng.annotations.BeforeTest)3 ScatterplotDrawingRowVisitor (com.google.refine.browsing.facets.ScatterplotDrawingRowVisitor)2 ExpressionBasedRowEvaluable (com.google.refine.browsing.util.ExpressionBasedRowEvaluable)2 NumericBinRowIndex (com.google.refine.browsing.util.NumericBinRowIndex)2 Project (com.google.refine.model.Project)2 BufferedImage (java.awt.image.BufferedImage)2 IOException (java.io.IOException)2 List (java.util.List)2 Properties (java.util.Properties)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 DecoratedValue (com.google.refine.browsing.DecoratedValue)1