" cells in column " + column.getName();
}
@Override
protected RowVisitor createRowVisitor(Project project, List<CellChange> cellChanges, long historyEntryID) throws Exception {
Column column = project.columnModel.getColumnByName(_columnName);
Evaluable eval = MetaParser.parse(_expression);
Properties bindings = ExpressionUtils.createBindings(project);
Map<String, Serializable> fromTo = new HashMap<String, Serializable>();
Serializable fromBlankTo = null;
Serializable fromErrorTo = null;
for (Edit edit : _edits) {
for (String s : edit.from) {
fromTo.put(s, edit.to);
}
// the last edit wins
if (edit.fromBlank) {
fromBlankTo = edit.to;
}
if (edit.fromError) {
fromErrorTo = edit.to;
}
}
return new RowVisitor() {
int cellIndex;
Properties bindings;
List<CellChange> cellChanges;
Evaluable eval;
Map<String, Serializable> fromTo;
Serializable fromBlankTo;
Serializable fromErrorTo;
public RowVisitor init(
int cellIndex,
Properties bindings,
List<CellChange> cellChanges,
Evaluable eval,
Map<String, Serializable> fromTo,
Serializable fromBlankTo,
Serializable fromErrorTo
) {
this.cellIndex = cellIndex;
this.bindings = bindings;
this.cellChanges = cellChanges;
this.eval = eval;
this.fromTo = fromTo;
this.fromBlankTo = fromBlankTo;
this.fromErrorTo = fromErrorTo;
return this;
}
@Override
public void start(Project project) {
// nothing to do
}
@Override
public void end(Project project) {
// nothing to do
}
@Override
public boolean visit(Project project, int rowIndex, Row row) {
Cell cell = row.getCell(cellIndex);
Cell newCell = null;
ExpressionUtils.bind(bindings, row, rowIndex, _columnName, cell);
Object v = eval.evaluate(bindings);
if (ExpressionUtils.isError(v)) {
if (fromErrorTo != null) {
newCell = new Cell(fromErrorTo, (cell != null) ? cell.recon : null);
}
} else if (ExpressionUtils.isNonBlankData(v)) {
String from = v.toString();
Serializable to = fromTo.get(from);
if (to != null) {
newCell = new Cell(to, (cell != null) ? cell.recon : null);
}
} else {
if (fromBlankTo != null) {
newCell = new Cell(fromBlankTo, (cell != null) ? cell.recon : null);
}
}
if (newCell != null) {
CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell);
cellChanges.add(cellChange);
}
return false;
}
}.init(column.getCellIndex(), bindings, cellChanges, eval, fromTo, fromBlankTo, fromErrorTo);
}