package foundation.io.file.pull;
|
|
import org.apache.poi.xssf.model.SharedStringsTable;
|
import org.apache.poi.xssf.model.StylesTable;
|
import org.xml.sax.Attributes;
|
import org.xml.sax.SAXException;
|
import org.xml.sax.helpers.DefaultHandler;
|
|
import foundation.data.entity.Entity;
|
import foundation.io.mapping.FieldMappingRuntime;
|
import foundation.io.object.Check;
|
import foundation.io.object.Titles;
|
import foundation.monitor.Progressor;
|
import foundation.util.Util;
|
|
|
public class SheetHandler extends DefaultHandler {
|
|
private int titleRowNo;
|
private int dataRowNo;
|
private DataBucket dataBucket;
|
private CellRuntime cellRuntime;
|
private Titles titles;
|
private Entity entity;
|
private int rowNo;
|
private String lineErrorMsg;
|
private boolean lineEmpty;
|
private boolean vIsOpen;
|
|
private String ioBatchId;
|
private Progressor progressor;
|
|
|
public SheetHandler(Progressor progressor, String ioBatchId, SharedStringsTable sharedStrings, StylesTable stylesTable, DataBucket dataBucket, int titleRowNo, int dataRowNo) {
|
this.dataBucket = dataBucket;
|
this.titleRowNo = titleRowNo;
|
this.dataRowNo = dataRowNo;
|
|
this.ioBatchId = ioBatchId;
|
this.progressor = progressor;
|
|
cellRuntime = new CellRuntime(sharedStrings, stylesTable);
|
titles = dataBucket.getTitles();
|
rowNo = 0;
|
}
|
|
private void outputOneRow() throws Exception {
|
if (titleRowNo == rowNo) {
|
boolean matchSuccess = checkMapping();
|
if (!matchSuccess) {
|
throw new Exception("导入列名匹配失败");
|
}
|
}
|
else if (lineEmpty) {
|
lineErrorMsg = "line empty;";
|
}
|
|
if (!Util.isEmpty(lineErrorMsg)) {
|
entity.set("is_error", true);
|
entity.set("error_message", lineErrorMsg);
|
progressor.appendLine("line " + rowNo + ":" + lineErrorMsg);
|
}
|
}
|
|
private void onComplete() {
|
|
}
|
|
private boolean checkMapping() {
|
return dataBucket.checkTitleMapping();
|
}
|
|
@Override
|
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
|
vIsOpen = false;
|
|
if ("c".equals(name)) {
|
cellRuntime.readAttributes(attributes);
|
cellRuntime.clearRawData();
|
}
|
else if ("is".equals(name) || "t".equals(name) || "inlineStr".equals(name) || "v".equals(name)) {
|
vIsOpen = true;
|
}
|
else if ("row".equals(name)) {
|
rowNo = parseRowno(attributes);
|
|
if (rowNo >= dataRowNo) {
|
entity = dataBucket.addOneLine(progressor, rowNo);
|
}
|
}
|
}
|
|
@Override
|
public void endElement(String uri, String localName, String name) throws SAXException {
|
try {
|
if ("v".equals(name) || "t".equals(name) ) {
|
if (cellRuntime.isIgnore()) {
|
return;
|
}
|
|
if (titleRowNo == rowNo) {
|
titles.add(cellRuntime.parseToString());
|
}
|
else if (entity != null) {
|
int columnIndex = cellRuntime.getColumnIndex();
|
FieldMappingRuntime mappingRuntime = dataBucket.getRuntimeMapping(columnIndex);
|
int entityIndex = mappingRuntime.getFromIndex();
|
String fromName = mappingRuntime.getFromName();
|
Check check = dataBucket.getCheckRule(columnIndex);
|
|
String value = cellRuntime.parseToString();
|
String errorMsg = "";
|
|
//1.2 check;
|
if (check.dataType()) {
|
errorMsg = errorMsg + entity.valueCheck(entityIndex, value);
|
}
|
|
if (check.empty()) {
|
if (Util.isEmpty(value)) {
|
errorMsg = errorMsg + "不能为空;";
|
}
|
}
|
|
if (!Util.isEmpty(errorMsg)){
|
lineErrorMsg = lineErrorMsg + "[" + fromName + "]" + errorMsg;
|
}
|
|
entity.set(entityIndex, value);
|
lineEmpty = false;
|
}
|
}
|
else if ("c".equals(name) ) {
|
if (titleRowNo != rowNo && entity != null && Util.isEmpty(cellRuntime.getRawDataString())) {
|
int columnIndex = cellRuntime.getColumnIndex();
|
FieldMappingRuntime mappingRuntime = dataBucket.getRuntimeMapping(columnIndex);
|
int entityIndex = mappingRuntime.getFromIndex();
|
String fromName = mappingRuntime.getFromName();
|
Check check = dataBucket.getCheckRule(columnIndex);
|
|
String value = cellRuntime.parseToString();
|
//1. 若单元格匹配头, 更新
|
String errorMsg = "";
|
|
//1.2 check;
|
if (check.empty()) {
|
if (Util.isEmpty(value)) {
|
errorMsg = errorMsg + "不能为空;";
|
}
|
}
|
|
if (!Util.isEmpty(errorMsg)){
|
lineErrorMsg = lineErrorMsg + "[" + fromName + "]" + errorMsg;
|
}
|
}
|
}
|
else if ("row".equals(name)) {
|
outputOneRow();
|
}
|
else if ("worksheet".equals(name)) {
|
onComplete();
|
}
|
}
|
catch (Exception e) {
|
e.printStackTrace();
|
throw new SAXException(e);
|
}
|
}
|
|
@Override
|
public void characters(char[] ch, int start, int length) throws SAXException {
|
if (vIsOpen) {
|
cellRuntime.appendValue(ch, start, length);
|
}
|
}
|
|
private int parseRowno(Attributes attributes) {
|
rowNo = Integer.parseInt(attributes.getValue(0));
|
return rowNo;
|
}
|
|
}
|