P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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;
    }
 
}