More error checking.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@54 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2010-02-06 02:41:31 +00:00
parent 149330fe05
commit 8701d504e6

View File

@ -14,6 +14,7 @@ import org.json.JSONWriter;
import com.metaweb.gridworks.commands.Command; import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.expr.Evaluable; import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils; import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.expr.HasFields;
import com.metaweb.gridworks.expr.Parser; import com.metaweb.gridworks.expr.Parser;
import com.metaweb.gridworks.model.Cell; import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Project; import com.metaweb.gridworks.model.Project;
@ -31,8 +32,6 @@ public class PreviewExpressionCommand extends Command {
int cellIndex = Integer.parseInt(request.getParameter("cellIndex")); int cellIndex = Integer.parseInt(request.getParameter("cellIndex"));
String expression = request.getParameter("expression"); String expression = request.getParameter("expression");
Evaluable eval = new Parser(expression).getExpression();
String rowIndicesString = request.getParameter("rowIndices"); String rowIndicesString = request.getParameter("rowIndices");
if (rowIndicesString == null) { if (rowIndicesString == null) {
respond(response, "{ \"code\" : \"error\", \"message\" : \"No row indices specified\" }"); respond(response, "{ \"code\" : \"error\", \"message\" : \"No row indices specified\" }");
@ -43,35 +42,47 @@ public class PreviewExpressionCommand extends Command {
int length = rowIndices.length(); int length = rowIndices.length();
JSONWriter writer = new JSONWriter(response.getWriter()); JSONWriter writer = new JSONWriter(response.getWriter());
writer.object(); writer.object();
writer.key("code"); writer.value("ok");
writer.key("results"); writer.array();
Properties bindings = ExpressionUtils.createBindings(project); try {
for (int i = 0; i < length; i++) { Evaluable eval = new Parser(expression).getExpression();
Object result = null;
int rowIndex = rowIndices.getInt(i); writer.key("code"); writer.value("ok");
if (rowIndex >= 0 && rowIndex < project.rows.size()) { writer.key("results"); writer.array();
Row row = project.rows.get(rowIndex);
if (cellIndex < row.cells.size()) { Properties bindings = ExpressionUtils.createBindings(project);
Cell cell = row.cells.get(cellIndex); for (int i = 0; i < length; i++) {
if (cell.value != null) { Object result = null;
ExpressionUtils.bind(bindings, row, cell);
int rowIndex = rowIndices.getInt(i);
try { if (rowIndex >= 0 && rowIndex < project.rows.size()) {
result = eval.evaluate(bindings); Row row = project.rows.get(rowIndex);
} catch (Exception e) { if (cellIndex < row.cells.size()) {
// ignore Cell cell = row.cells.get(cellIndex);
if (cell != null && cell.value != null) {
ExpressionUtils.bind(bindings, row, cell);
try {
result = eval.evaluate(bindings);
} catch (Exception e) {
// ignore
}
} }
} }
} }
if (result != null) {
if (result instanceof HasFields) {
result = "[object " + result.getClass().getSimpleName() + "]";
}
}
writer.value(result);
} }
writer.endArray();
writer.value(result); } catch (Exception e) {
writer.key("code"); writer.value("error");
} }
writer.endArray();
writer.endObject(); writer.endObject();
} catch (Exception e) { } catch (Exception e) {
respondException(response, e); respondException(response, e);