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.biff8;
22  
23  import org.millscript.office.excel.BIFFRecordInputStream;
24  import org.millscript.office.excel.RecordParser;
25  import org.millscript.office.excel.records.substructures.CellRangeAddress;
26  import org.millscript.office.excel.records.substructures.ConstantCachedValue;
27  import org.millscript.office.excel.records.substructures.FormattingRun;
28  import org.millscript.office.excel.versions.BIFFVersion;
29  import org.millscript.office.excel.versions.biff7.Biff7RecordTokenizer;
30  import org.millscript.office.spreadsheet.formula.AbsoluteCellAddress;
31  import org.millscript.office.spreadsheet.formula.OffsetCellAddress;
32  
33  /**
34   * 
35   */
36  public class Biff8RecordTokenizer extends Biff7RecordTokenizer {
37  
38      /**
39       * @param version
40       * @param bis
41       */
42      public Biff8RecordTokenizer( final BIFFVersion version, final BIFFRecordInputStream bis, final byte[] bofData ) {
43          super( version, bis, bofData );
44      }
45  
46      /**
47       * @see org.millscript.office.excel.versions.biff7.Biff7RecordTokenizer#getRecordParser()
48       */
49      @Override
50      public RecordParser getRecordParser() {
51          return new BIFF8RecordParser( this );
52      }
53  
54      /**
55       * @see org.millscript.office.excel.RecordTokenizer#readAbsoluteCellAddress()
56       */
57      @Override
58      public AbsoluteCellAddress readAbsoluteCellAddress() {
59          final int rowIndex = this.read2ByteChar() + 1;
60          final char colIndexWithRelativeFlags = this.read2ByteChar();
61          // Return a new absolute cell address object
62          return new AbsoluteCellAddress(
63              // Row index
64              rowIndex,
65              // Row index is relative?
66              ( 0 != ( colIndexWithRelativeFlags & 0x8000 ) ),
67              // Col index
68              ( ( colIndexWithRelativeFlags & 0x00FF ) + 1 ),
69              // Col index is relative?
70              ( 0 != ( colIndexWithRelativeFlags & 0x4000 ) )
71          );
72      }
73  
74      /**
75       * @see org.millscript.office.excel.RecordTokenizer#readCellRangeAddress()
76       */
77      @Override
78      public CellRangeAddress readCellRangeAddress() {
79          // In BIFF8 the column indicies are stored as double bytes sequences
80          return new CellRangeAddress(
81              this.readUnsigned2Byte(),
82              this.readUnsigned2Byte(),
83              this.readUnsigned2Byte(),
84              this.readUnsigned2Byte()
85          );
86      }
87  
88      /**
89       * @see org.millscript.office.excel.RecordTokenizer#readConstantCachedValueArray()
90       */
91      @Override
92      public ConstantCachedValue[] readConstantCachedValueArray() {
93          return this.readConstantCachedValues(
94              ( this.readUnsignedByte() + 1 ) * ( this.readUnsigned2Byte() + 1 )
95          );
96      }
97  
98      /**
99       * @see org.millscript.office.excel.RecordTokenizer#readFormattingRun()
100      */
101     @Override
102     public FormattingRun readFormattingRun() {
103         return new FormattingRun(
104             this.read2ByteInt(),
105             this.read2ByteInt()
106         );
107     }
108 
109     /**
110      * @see org.millscript.office.excel.RecordTokenizer#readOffsetCellAddress()
111      */
112     @Override
113     public OffsetCellAddress readOffsetCellAddress() {
114         int rowIndex = this.read2ByteInt();
115         final char colIndexWithRelativeFlags = this.read2ByteChar();
116         final boolean columnIndexIsOffet = ( 0 != ( colIndexWithRelativeFlags & 0x4000 ) );
117         final boolean rowIndexIsOffet = ( 0 != ( colIndexWithRelativeFlags & 0x8000 ) );
118         int columnIndex = 0;
119         if ( columnIndexIsOffet ) {
120             // Column index is relative offset
121             columnIndex = ( colIndexWithRelativeFlags & 0x00FF ) | 0xFF00;
122         } else {
123             // Column index is absolute
124             columnIndex = ( colIndexWithRelativeFlags & 0x00FF ) + 1;
125         }
126         if ( ! rowIndexIsOffet ) {
127             // Row index is absolute
128             rowIndex = ( rowIndex & 0xFFFF ) + 1;
129         }
130         // Return a new absolute cell address object
131         return new OffsetCellAddress(
132             // Row index
133             rowIndex,
134             // Row index is relative?
135             rowIndexIsOffet,
136             // Col index
137             columnIndex,
138             // Col index is relative?
139             columnIndexIsOffet
140         );
141     }
142 
143     /**
144      * @see org.millscript.office.excel.RecordTokenizer#readString(int)
145      */
146     @Override
147     public String readString( final int length ) {
148         // Strings in BIFF8+ are always UTF-16LE, but all the high bytes may be
149         // omitted if they are all zero
150         return this.readUnicodeString( length );
151     }
152 
153 }