1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.millscript.office.excel;
22
23 import org.millscript.commons.alert.alerts.Fault;
24 import org.millscript.office.excel.alerts.BIFFAlert;
25 import org.millscript.office.excel.records.BOF;
26 import org.millscript.office.excel.records.EOF;
27 import org.millscript.office.spreadsheet.Workbook;
28 import org.millscript.office.spreadsheet.Worksheet;
29
30 /**
31 * This class parses BIFF records from the underlying tokenizer.
32 */
33 public abstract class RecordParser {
34
35 private final RecordTokenizer recordTokenizer;
36
37 private ExcelWorkbook currentWorkbook;
38
39 private ExcelWorksheet currentWorksheet;
40
41 public RecordParser( final RecordTokenizer tokenizer ) {
42 this.recordTokenizer = tokenizer;
43 }
44
45 public ExcelWorkbook getCurrentWorkbook() {
46 return this.currentWorkbook;
47 }
48
49 public ExcelWorksheet getCurrentWorksheet() {
50 return this.currentWorksheet;
51 }
52
53 public String getSharedString( final int index ) {
54
55 throw new Fault(
56 "Shared strings are not available in the current BIFF version"
57 ).mishap();
58 }
59
60 public Workbook readWorkbook() {
61
62 final BOF workbookGlobalsBOF = this.readWorkbookGlobalsBOF();
63
64 this.currentWorkbook = new ExcelWorkbook();
65
66 while ( !( this.peekRecord() instanceof EOF ) ) {
67 final Record record = this.nextRecord();
68 record.handle( this );
69 }
70
71 final Record workbookGlobalsEOF = this.nextRecord();
72
73 while ( this.hasMoreRecords() && this.peekRecord() instanceof BOF ) {
74 this.currentWorkbook.addLast( this.readWorksheet() );
75 }
76
77 return this.currentWorkbook;
78 }
79
80 public Worksheet readWorksheet() {
81
82 final Record worksheetBof = this.nextRecord();
83
84 this.currentWorksheet = new ExcelWorksheet();
85
86 while ( !( this.peekRecord() instanceof EOF ) ) {
87 final Record record = this.nextRecord();
88 record.handle( this );
89 }
90
91 final Record eof = this.nextRecord();
92
93 return this.currentWorksheet;
94 }
95
96 /**
97 * @see org.millscript.office.excel.RecordTokenizer#hasMoreRecords()
98 */
99 public boolean hasMoreRecords() {
100 return this.recordTokenizer.hasMoreRecords();
101 }
102
103 /**
104 * @see org.millscript.office.excel.RecordTokenizer#nextRecord()
105 */
106 public Record nextRecord() {
107 return this.recordTokenizer.nextRecord();
108 }
109
110 /**
111 * @see org.millscript.office.excel.RecordTokenizer#peekRecord()
112 */
113 public Record peekRecord() {
114 return recordTokenizer.peekRecord();
115 }
116
117 public BOF readWorkbookGlobalsBOF() {
118 final Record record = this.recordTokenizer.nextRecord();
119
120 if ( record instanceof BOF ) {
121 final BOF bof = (BOF) record;
122
123 if ( bof.getType() == BOF.Type.WorkbookGlobals ) {
124 return bof;
125 }
126 }
127
128 throw new BIFFAlert(
129 "Workbook Globals BOF record expected"
130 ).decorate( record ).mishap();
131 }
132
133 public BOF readWorksheetBOF() {
134 final Record record = this.recordTokenizer.nextRecord();
135
136 if ( record instanceof BOF ) {
137 final BOF bof = (BOF) record;
138
139 if ( bof.getType() == BOF.Type.Worksheet ) {
140 return bof;
141 }
142 }
143
144 throw new BIFFAlert(
145 "Worksheet BOF record expected"
146 ).decorate( record ).mishap();
147 }
148
149 public void setSharedStringTable( final String[] strings ) {
150
151 throw new Fault(
152 "Shared strings are not available in the current BIFF version"
153 ).mishap();
154 }
155
156 }