Search in sources :

Example 11 with ParsingException

use of com.google.refine.expr.ParsingException in project OpenRefine by OpenRefine.

the class ScatterplotFacet method getBinIndex.

public static NumericBinIndex getBinIndex(Project project, Column column, Evaluable eval, String expression, String mode) {
    String key = "numeric-bin:" + mode + ":" + expression;
    if (eval == null) {
        try {
            eval = MetaParser.parse(expression);
        } catch (ParsingException e) {
            logger.warn("Error parsing expression", e);
        }
    }
    NumericBinIndex index = (NumericBinIndex) column.getPrecompute(key);
    if (index == null) {
        index = "row-based".equals(mode) ? new NumericBinRowIndex(project, new ExpressionBasedRowEvaluable(column.getName(), column.getCellIndex(), eval)) : new NumericBinRecordIndex(project, new ExpressionBasedRowEvaluable(column.getName(), column.getCellIndex(), eval));
        column.setPrecompute(key, index);
    }
    return index;
}
Also used : NumericBinRecordIndex(com.google.refine.browsing.util.NumericBinRecordIndex) NumericBinRowIndex(com.google.refine.browsing.util.NumericBinRowIndex) NumericBinIndex(com.google.refine.browsing.util.NumericBinIndex) ParsingException(com.google.refine.expr.ParsingException) ExpressionBasedRowEvaluable(com.google.refine.browsing.util.ExpressionBasedRowEvaluable)

Example 12 with ParsingException

use of com.google.refine.expr.ParsingException 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);
    }
}
Also used : JSONWriter(org.json.JSONWriter) JSONArray(org.json.JSONArray) EvalError(com.google.refine.expr.EvalError) Properties(java.util.Properties) ServletException(javax.servlet.ServletException) JSONException(org.json.JSONException) IOException(java.io.IOException) ParsingException(com.google.refine.expr.ParsingException) Project(com.google.refine.model.Project) Evaluable(com.google.refine.expr.Evaluable) ParsingException(com.google.refine.expr.ParsingException) JSONObject(org.json.JSONObject) WrappedRow(com.google.refine.expr.WrappedRow) Row(com.google.refine.model.Row) Cell(com.google.refine.model.Cell) WrappedCell(com.google.refine.expr.WrappedCell)

Aggregations

ParsingException (com.google.refine.expr.ParsingException)12 Evaluable (com.google.refine.expr.Evaluable)6 Column (com.google.refine.model.Column)6 NumericBinIndex (com.google.refine.browsing.util.NumericBinIndex)4 EvalError (com.google.refine.expr.EvalError)3 JSONObject (org.json.JSONObject)3 FilteredRows (com.google.refine.browsing.FilteredRows)2 ExpressionBasedRowEvaluable (com.google.refine.browsing.util.ExpressionBasedRowEvaluable)2 NumericBinRowIndex (com.google.refine.browsing.util.NumericBinRowIndex)2 Project (com.google.refine.model.Project)2 Color (java.awt.Color)2 IOException (java.io.IOException)2 JSONArray (org.json.JSONArray)2 JSONException (org.json.JSONException)2 JSONWriter (org.json.JSONWriter)2 DecoratedValue (com.google.refine.browsing.DecoratedValue)1 Engine (com.google.refine.browsing.Engine)1 FilteredRecords (com.google.refine.browsing.FilteredRecords)1 RecordVisitor (com.google.refine.browsing.RecordVisitor)1 RowVisitor (com.google.refine.browsing.RowVisitor)1