Search in sources :

Example 11 with EvalError

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

the class Reinterpret method call.

@Override
public Object call(Properties bindings, Object[] args) {
    if (args.length == 2 || args.length == 3) {
        Object o1 = args[0];
        Object o2 = args[1];
        if (o1 != null && o2 != null && o2 instanceof String) {
            String str = (o1 instanceof String) ? (String) o1 : o1.toString();
            String decoder;
            String encoder;
            encoder = (String) o2;
            if (args.length == 2) {
                Project project = (Project) bindings.get("project");
                ProjectMetadata metadata = ProjectManager.singleton.getProjectMetadata(project.id);
                // can return "" for broken projects
                decoder = metadata.getEncoding();
            } else {
                decoder = (String) args[2];
            }
            return reinterpret(str, decoder, encoder);
        }
    }
    return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 or 3 arguments");
}
Also used : Project(com.google.refine.model.Project) ProjectMetadata(com.google.refine.ProjectMetadata) EvalError(com.google.refine.expr.EvalError)

Example 12 with EvalError

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

the class Cell method loadStreaming.

public static Cell loadStreaming(JsonParser jp, Pool pool) throws Exception {
    JsonToken t = jp.getCurrentToken();
    if (t == JsonToken.VALUE_NULL || t != JsonToken.START_OBJECT) {
        return null;
    }
    Serializable value = null;
    String type = null;
    Recon recon = null;
    while (jp.nextToken() != JsonToken.END_OBJECT) {
        String fieldName = jp.getCurrentName();
        jp.nextToken();
        if ("r".equals(fieldName)) {
            if (jp.getCurrentToken() == JsonToken.VALUE_STRING) {
                String reconID = jp.getText();
                recon = pool.getRecon(reconID);
            } else {
                // legacy
                recon = Recon.loadStreaming(jp, pool);
            }
        } else if ("e".equals(fieldName)) {
            value = new EvalError(jp.getText());
        } else if ("v".equals(fieldName)) {
            JsonToken token = jp.getCurrentToken();
            if (token == JsonToken.VALUE_STRING) {
                value = jp.getText();
            } else if (token == JsonToken.VALUE_NUMBER_INT) {
                value = jp.getLongValue();
            } else if (token == JsonToken.VALUE_NUMBER_FLOAT) {
                value = jp.getDoubleValue();
            } else if (token == JsonToken.VALUE_TRUE) {
                value = true;
            } else if (token == JsonToken.VALUE_FALSE) {
                value = false;
            }
        } else if ("t".equals(fieldName)) {
            type = jp.getText();
        }
    }
    if (value != null) {
        if (type != null && "date".equals(type)) {
            value = ParsingUtilities.stringToDate((String) value);
        }
        return new Cell(value, recon);
    } else {
        return null;
    }
}
Also used : Serializable(java.io.Serializable) JsonToken(org.codehaus.jackson.JsonToken) EvalError(com.google.refine.expr.EvalError)

Example 13 with EvalError

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

the class GrelTests method testEvalError.

@Test
public void testEvalError() {
    String[] tests = { //                "1=1", // TODO: Throws NullPointerException
    "a.value" };
    for (String test : tests) {
        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: " + test);
        }
    }
}
Also used : Evaluable(com.google.refine.expr.Evaluable) ParsingException(com.google.refine.expr.ParsingException) EvalError(com.google.refine.expr.EvalError) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) RefineTest(com.google.refine.tests.RefineTest)

Example 14 with EvalError

use of com.google.refine.expr.EvalError 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)

Example 15 with EvalError

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

the class ForEachIndex 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 to forEach is not an array");
    }
    String indexName = ((VariableExpr) args[1]).getName();
    String elementName = ((VariableExpr) args[2]).getName();
    Object oldIndexValue = bindings.get(indexName);
    Object oldElementValue = bindings.get(elementName);
    try {
        List<Object> results = null;
        if (o.getClass().isArray()) {
            Object[] values = (Object[]) o;
            results = new ArrayList<Object>(values.length);
            for (int i = 0; i < values.length; i++) {
                Object v = values[i];
                bindings.put(indexName, i);
                bindings.put(elementName, v);
                Object r = args[3].evaluate(bindings);
                results.add(r);
            }
        } 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);
                    bindings.put(indexName, i);
                    bindings.put(elementName, v);
                    Object r = args[3].evaluate(bindings);
                    results.add(r);
                } catch (JSONException e) {
                    results.add(new EvalError(e.getMessage()));
                }
            }
        } else {
            List<Object> list = ExpressionUtils.toObjectList(o);
            results = new ArrayList<Object>(list.size());
            for (int i = 0; i < list.size(); i++) {
                Object v = list.get(i);
                bindings.put(indexName, i);
                bindings.put(elementName, v);
                Object r = args[3].evaluate(bindings);
                results.add(r);
            }
        }
        return results.toArray();
    } finally {
        /*
             *  Restore the old values bound to the variables, if any.
             */
        if (oldIndexValue != null) {
            bindings.put(indexName, oldIndexValue);
        } else {
            bindings.remove(indexName);
        }
        if (oldElementValue != null) {
            bindings.put(elementName, oldElementValue);
        } else {
            bindings.remove(elementName);
        }
    }
}
Also used : JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) EvalError(com.google.refine.expr.EvalError) VariableExpr(com.google.refine.grel.ast.VariableExpr) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

EvalError (com.google.refine.expr.EvalError)23 JSONArray (org.json.JSONArray)6 JSONException (org.json.JSONException)6 VariableExpr (com.google.refine.grel.ast.VariableExpr)4 ArrayList (java.util.ArrayList)4 Calendar (java.util.Calendar)4 Date (java.util.Date)4 Evaluable (com.google.refine.expr.Evaluable)3 ParsingException (com.google.refine.expr.ParsingException)3 Project (com.google.refine.model.Project)3 WrappedCell (com.google.refine.expr.WrappedCell)2 CalendarParserException (com.google.refine.expr.util.CalendarParserException)2 IOException (java.io.IOException)2 Collection (java.util.Collection)2 Element (org.jsoup.nodes.Element)2 CSVParser (au.com.bytecode.opencsv.CSVParser)1 ProjectJoin (com.google.refine.InterProjectModel.ProjectJoin)1 ProjectMetadata (com.google.refine.ProjectMetadata)1 Engine (com.google.refine.browsing.Engine)1 ExpressionNominalValueGrouper (com.google.refine.browsing.util.ExpressionNominalValueGrouper)1