View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript-Excel: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2006 Open World Ltd, Kevin Rogers
4   //
5   // This file is part of MillScript-Excel.
6   //
7   // MillScript-Excel is free software; you can redistribute it and/or modify it under
8   // the terms of the GNU General Public License as published by the Free
9   // Software Foundation; either version 2 of the License, or (at your option)
10  // any later version.
11  //
12  // MillScript-Excel is distributed in the hope that it will be useful, but WITHOUT
13  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  // more details.
16  //
17  // You should have received a copy of the GNU General Public License along with
18  // MillScript-Excel; if not, write to the Free Software Foundation, Inc., 59 Temple
19  // Place, Suite 330, Boston, MA  02111-1307  USA
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          // Shared strings are not supported in most BIFF versions
55          throw new Fault(
56              "Shared strings are not available in the current BIFF version"
57          ).mishap();
58      }
59  
60      public Workbook readWorkbook() {
61          // First read the workbook globals BOF record
62          final BOF workbookGlobalsBOF = this.readWorkbookGlobalsBOF();
63          // Make the workbook object
64          this.currentWorkbook = new ExcelWorkbook();
65          // Now we can continue with the globals workbook records
66          while ( !( this.peekRecord() instanceof EOF ) ) {
67              final Record record = this.nextRecord();
68              record.handle( this );
69          }
70          // record is EOF now
71          final Record workbookGlobalsEOF = this.nextRecord();
72          // Read the worksheets in the workbook
73          while ( this.hasMoreRecords() && this.peekRecord() instanceof BOF ) {
74              this.currentWorkbook.addLast( this.readWorksheet() );
75          }
76          // We've got to the end of the workbook, so return it
77          return this.currentWorkbook;
78      }
79  
80      public Worksheet readWorksheet() {
81          // Read worksheet BOF record
82          final Record worksheetBof = this.nextRecord();
83          // Make the worksheet object
84          this.currentWorksheet = new ExcelWorksheet();
85          // Read each sheets records
86          while ( !( this.peekRecord() instanceof EOF ) ) {
87              final Record record = this.nextRecord();
88              record.handle( this );
89          }
90          // record is EOF now, so read it
91          final Record eof = this.nextRecord();
92          // We've got to the end of the worksheet, so return it
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         // Check it's a BOF record
120         if ( record instanceof BOF ) {
121             final BOF bof = (BOF) record;
122             // and that it is a workbook globals BOF record
123             if ( bof.getType() == BOF.Type.WorkbookGlobals ) {
124                 return bof;
125             }
126         }
127         // Oh dear, it's not a workbook globals BOF record
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         // Check it's a BOF record
136         if ( record instanceof BOF ) {
137             final BOF bof = (BOF) record;
138             // and that it is a worksheet BOF record
139             if ( bof.getType() == BOF.Type.Worksheet ) {
140                 return bof;
141             }
142         }
143         // Oh dear, it's not a Worksheet BOF record
144         throw new BIFFAlert(
145             "Worksheet BOF record expected"
146         ).decorate( record ).mishap();
147     }
148 
149     public void setSharedStringTable( final String[] strings ) {
150         // Shared strings are not supported in most BIFF versions
151         throw new Fault(
152             "Shared strings are not available in the current BIFF version"
153         ).mishap();
154     }
155 
156 }