@SuppressWarnings({"rawtypes", "unchecked"}) public static Model<?> fillModel(Class<?> clazz, List<String> list, Rule rule) { Model<?> model = Reflect.on(clazz).create().get(); String[] values = list.toArray(new String[] {}); String message = ""; for (int i = 0; i < values.length; i++) { String value = values[i]; Rule.Cell cell = matchCell(rule, i); String name = cell.getAttribute(); String validateClassName = cell.getValidate(); boolean valid = true; if (StrKit.notBlank(validateClassName)) { CellValidate cellValidate = Reflect.on(validateClassName).create().get(); valid = cellValidate.validate(value); if (!valid) { message = message + "value(" + value + ") is invalid in column " + cell.getIndex() + "</br>"; } } if (valid) { Object convertedValue = value; String convertClassName = cell.getConvert(); if (StrKit.notBlank(convertClassName)) { CellConvert cellConvert = Reflect.on(convertClassName).get(); convertedValue = cellConvert.convert(value, model); } model.set(name, convertedValue); } } if (StrKit.notBlank(message)) { throw new ExcelException(message); } return model; }
public static Rule.Cell matchCell(Rule rule, int index) { List<Rule.Cell> cells = rule.getCells(); for (int i = 0; i < cells.size(); i++) { Rule.Cell cell = cells.get(i); if (index + 1 == cell.getIndex()) return cell; } return null; }