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.object.Titles;
|
|
|
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 boolean vIsOpen;
|
|
|
public SheetHandler(SharedStringsTable sharedStrings, StylesTable stylesTable, DataBucket dataBucket, int titleRowNo, int dataRowNo) {
|
this.dataBucket = dataBucket;
|
this.titleRowNo = titleRowNo;
|
this.dataRowNo = dataRowNo;
|
|
cellRuntime = new CellRuntime(sharedStrings, stylesTable);
|
titles = dataBucket.getTitles();
|
rowNo = 0;
|
}
|
|
private void outputOneRow() throws SAXException {
|
|
}
|
|
private void onComplete() {
|
|
}
|
|
@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 ("inlineStr".equals(name) || "v".equals(name)) {
|
vIsOpen = true;
|
}
|
else if ("row".equals(name)) {
|
rowNo = parseRowno(attributes);
|
|
if (rowNo >= dataRowNo) {
|
entity = dataBucket.addOneLine(rowNo);
|
}
|
}
|
}
|
|
@Override
|
public void endElement(String uri, String localName, String name) throws SAXException {
|
try {
|
if ("v".equals(name)) {
|
if (cellRuntime.isIgnore()) {
|
return;
|
}
|
|
if (titleRowNo == rowNo) {
|
titles.add(cellRuntime.parseToString());
|
}
|
else if (entity != null) {
|
entity.set(cellRuntime.getColumnIndex(), cellRuntime.parseToString());
|
}
|
}
|
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;
|
}
|
|
}
|