View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript-Excel: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2005 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.versions.biff2;
22  
23  import org.millscript.millscript.expr.Expr;
24  import org.millscript.office.excel.BIFFRecordInputStream;
25  import org.millscript.office.excel.RecordParser;
26  import org.millscript.office.excel.RecordTokenizer;
27  import org.millscript.office.excel.records.substructures.CellRangeAddress;
28  import org.millscript.office.excel.records.substructures.ConstantCachedValue;
29  import org.millscript.office.excel.records.substructures.FormattingRun;
30  import org.millscript.office.excel.versions.BIFFVersion;
31  import org.millscript.office.spreadsheet.formula.AbsoluteCellAddress;
32  import org.millscript.office.spreadsheet.formula.OffsetCellAddress;
33  
34  /**
35   * 
36   */
37  public class Biff2RecordTokenizer extends RecordTokenizer {
38  
39      /**
40       * @param version
41       * @param bis
42       */
43      public Biff2RecordTokenizer( final BIFFVersion version, final BIFFRecordInputStream bis, final byte[] bofData ) {
44          super( version, bis, bofData );
45      }
46  
47      /**
48       * @see org.millscript.office.excel.RecordTokenizer#getRecordParser()
49       */
50      @Override
51      public RecordParser getRecordParser() {
52          return new BIFF2RecordParser( this );
53      }
54  
55      /**
56       * @see org.millscript.office.excel.RecordTokenizer#readAbsoluteCellAddress()
57       */
58      @Override
59      public AbsoluteCellAddress readAbsoluteCellAddress() {
60          final char rowIndexWithRelativeFlags = this.read2ByteChar();
61          final int columnIndex = this.readUnsignedByte() + 1;
62          // Return a new absolute cell address object
63          return new AbsoluteCellAddress(
64              // Row index
65              ( ( rowIndexWithRelativeFlags & 0x3FFF ) + 1 ),
66              // Row index is relative?
67              ( 0 != ( rowIndexWithRelativeFlags & 0x8000 ) ),
68              // Col index
69              columnIndex,
70              // Col index is relative?
71              ( 0 != ( rowIndexWithRelativeFlags & 0x4000 ) )
72          );
73      }
74  
75      /**
76       * @see org.millscript.office.excel.RecordTokenizer#readCellRangeAddress()
77       */
78      @Override
79      public CellRangeAddress readCellRangeAddress() {
80          return this.readCellRangeAddressPreBIFF8();
81      }
82  
83      /**
84       * @see org.millscript.office.excel.RecordTokenizer#readConstantCachedValueArray()
85       */
86      @Override
87      public ConstantCachedValue[] readConstantCachedValueArray() {
88          final int cols = this.readUnsignedByte();
89          return this.readConstantCachedValues(
90              ( cols == 0 ? 256 : cols ) * this.readUnsigned2Byte()
91          );
92      }
93  
94      /**
95       * @see org.millscript.office.excel.RecordTokenizer#readFormattingRun()
96       */
97      @Override
98      public FormattingRun readFormattingRun() {
99          return new FormattingRun(
100             this.readUnsignedByte(),
101             this.readUnsignedByte()
102         );
103     }
104 
105     /**
106      * @see org.millscript.office.excel.RecordTokenizer#readFormula(int)
107      */
108     @Override
109     public Expr< ? > readFormula( final int formulaSize ) {
110         // Read the formula data size
111         final int formulaDataSize = this.readUnsignedByte();
112         // Calculate the additional data size
113         final int additionalDataSize = formulaSize - 1 - formulaDataSize;
114         // Return the formula
115         return this.readFormula( formulaDataSize, additionalDataSize );
116     }
117 
118     /**
119      * @see org.millscript.office.excel.RecordTokenizer#readOffsetCellAddress()
120      */
121     @Override
122     public OffsetCellAddress readOffsetCellAddress() {
123         final char rowIndexWithRelativeFlags = this.readUnsigned2Byte();
124         final boolean columnIndexIsOffet = ( 0 != ( rowIndexWithRelativeFlags & 0x4000 ) );
125         final boolean rowIndexIsOffet = ( 0 != ( rowIndexWithRelativeFlags & 0x8000 ) );
126         int columnIndex = 0;
127         if ( columnIndexIsOffet ) {
128             // Column index is relative offset
129             columnIndex = this.readByte();
130         } else {
131             // Column index is absolute
132             columnIndex = this.readUnsignedByte() + 1;
133         }
134         int rowIndex = 0;
135         if ( rowIndexIsOffet ) {
136             // Row index is relative offset
137             rowIndex = ( rowIndexWithRelativeFlags & 0x3FFF ) | 0xC000;
138         } else {
139             // Row index is absolute
140             rowIndex = ( rowIndexWithRelativeFlags & 0x3FFF ) + 1;
141         }
142         // Return a new absolute cell address object
143         return new OffsetCellAddress(
144             // Row index
145             rowIndex,
146             // Row index is relative?
147             rowIndexIsOffet,
148             // Col index
149             columnIndex,
150             // Col index is relative?
151             columnIndexIsOffet
152         );
153     }
154 
155     /**
156      * @see org.millscript.office.excel.RecordTokenizer#readString(int)
157      */
158     @Override
159     public String readString( final int length ) {
160         return this.decode( this.readBytes( length ) );
161     }
162 
163 }