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.records.syntax;
22  
23  import org.millscript.office.excel.RecordSyntax;
24  import org.millscript.office.excel.RecordTokenizer;
25  import org.millscript.office.excel.alerts.BIFFAlert;
26  import org.millscript.office.excel.records.Name;
27  import org.millscript.office.excel.versions.BIFF2;
28  import org.millscript.office.excel.versions.BIFF3;
29  import org.millscript.office.excel.versions.BIFF4S;
30  import org.millscript.office.excel.versions.BIFF4W;
31  import org.millscript.office.excel.versions.BIFF5;
32  import org.millscript.office.excel.versions.BIFF7;
33  import org.millscript.office.excel.versions.BIFF8;
34  import org.millscript.office.excel.versions.BIFF8X;
35  
36  /**
37   * 
38   */
39  public class NameRecordSyntax extends RecordSyntax implements BIFF2, BIFF3, BIFF4S, BIFF4W, BIFF5, BIFF7, BIFF8, BIFF8X {
40  
41      public void handleBIFF2( final RecordTokenizer tokenizer, final Name name ) {
42          final int flag = tokenizer.readUnsignedByte();
43          final int nameFlag = tokenizer.readUnsignedByte();
44          int options = 0;
45          if ( 0 != ( flag & 0x02 ) ) {
46              // It is a macro
47              options |= 0x0008;
48              // Check if it's a function or command macro
49              if ( nameFlag == 0x01 ) {
50                  // It's a function macro
51                  options |= 0x0002;
52              } else if ( nameFlag == 0x02 ) {
53                  // It's a command macro
54              } else {
55                  throw new BIFFAlert(
56                      "Unknown macro type for NAME record"
57                  ).culprit( "type", nameFlag ).mishap();
58              }
59          }
60          if ( 0 != ( flag & 0x04 ) ) {
61              options |= 0x0010;
62          }
63          // Fake an options record to keep things compatible with later
64          // BIFF versions
65          name.setOptions( options );
66          // Set the keyboard shortcut
67          name.setKeyboardShortcut( tokenizer.readByte() );
68          // Read the length of the name
69          final int nameLength = tokenizer.readUnsignedByte();
70          // Read the formula size
71          final int formulaSize = tokenizer.readUnsignedByte();
72          // Set the name
73          name.setName( tokenizer.readString( nameLength ) );
74          // Set the formula
75          name.setFormulaExpr( tokenizer.readFormula( formulaSize, 0 ) );
76          // Skip the duplicate of the formula data size
77          tokenizer.skipBytes( 1 );
78      }
79  
80      public void handleBIFF3BIFF4( final RecordTokenizer tokenizer, final Name name ) {
81          // Set the name options
82          name.setOptions( tokenizer.read2ByteInt() );
83          // Set the keyboard shortcut
84          name.setKeyboardShortcut( tokenizer.readByte() );
85          // Read the length of the name
86          final int nameLength = tokenizer.readUnsignedByte();
87          // Read the formula size
88          final int formulaSize = tokenizer.readUnsigned2Byte();
89          // Set the name
90          name.setName( tokenizer.readString( nameLength ) );
91          // Set the formula
92          name.setFormulaExpr( tokenizer.readFormula( formulaSize, 0 ) );
93      }
94  
95      public void handleBIFF5BIFF7( final RecordTokenizer tokenizer, final Name name ) {
96          // Set the name options
97          name.setOptions( tokenizer.read2ByteInt() );
98          // Set the keyboard shortcut
99          name.setKeyboardShortcut( tokenizer.readByte() );
100         // Read the length of the name
101         final int nameLength = tokenizer.readUnsignedByte();
102         // Read the formula size
103         final int formulaSize = tokenizer.readUnsigned2Byte();
104         // Set the index to the EXTERNSHEET record
105         name.setIndexToExternSheetRecord( tokenizer.readUnsigned2Byte() );
106         // Set the index to the sheet
107         name.setIndexToSheet( tokenizer.readUnsigned2Byte() );
108         // Read the length of the menu text
109         final int menuTextLength = tokenizer.readUnsignedByte();
110         // Read the length of the description text
111         final int descriptionTextLength = tokenizer.readUnsignedByte();
112         // Read the length of the help topic text
113         final int helpTopicTextLength = tokenizer.readUnsignedByte();
114         // Read the length of the status bar text
115         final int statusBarTextLength = tokenizer.readUnsignedByte();
116         // Set the name
117         name.setName( tokenizer.readString( nameLength ) );
118         // Set the formula
119         name.setFormulaExpr( tokenizer.readFormula( formulaSize, 0 ) );
120         // Set the menu text
121         name.setMenuText( tokenizer.readString( menuTextLength ) );
122         // Set the description text
123         name.setDescriptionText(
124             tokenizer.readString( descriptionTextLength )
125         );
126         // Set the help topic text
127         name.setHelpTopicText( tokenizer.readString( helpTopicTextLength ) );
128         // Set the status bar text
129         name.setStatusBarText( tokenizer.readString( statusBarTextLength ) );
130     }
131 
132     public void handleBIFF8( final RecordTokenizer tokenizer, final Name name ) {
133         // Set the name options
134         name.setOptions( tokenizer.read2ByteInt() );
135         // Set the keyboard shortcut
136         name.setKeyboardShortcut( tokenizer.readByte() );
137         // Read the length of the name
138         final int nameLength = tokenizer.readUnsignedByte();
139         // Read the formula size
140         final int formulaSize = tokenizer.readUnsigned2Byte();
141         // Set the index to the EXTERNSHEET record
142         name.setIndexToExternSheetRecord( tokenizer.readUnsigned2Byte() );
143         // Set the index to the sheet
144         name.setIndexToSheet( tokenizer.readUnsigned2Byte() );
145         // Read the length of the menu text
146         final int menuTextLength = tokenizer.readUnsignedByte();
147         // Read the length of the description text
148         final int descriptionTextLength = tokenizer.readUnsignedByte();
149         // Read the length of the help topic text
150         final int helpTopicTextLength = tokenizer.readUnsignedByte();
151         // Read the length of the status bar text
152         final int statusBarTextLength = tokenizer.readUnsignedByte();
153         // Set the name
154         name.setName( tokenizer.readString( nameLength ) );
155         // Set the formula
156         name.setFormulaExpr( tokenizer.readFormula( formulaSize, 0 ) );
157         // Set the menu text
158         if ( menuTextLength > 0 ) {
159             name.setMenuText( tokenizer.readString( menuTextLength ) );
160         }
161         // Set the description text
162         if ( descriptionTextLength > 0 ) {
163             name.setDescriptionText(
164                 tokenizer.readString( descriptionTextLength )
165             );
166         }
167         // Set the help topic text
168         if ( helpTopicTextLength > 0 ) {
169             name.setHelpTopicText( tokenizer.readString( helpTopicTextLength ) );
170         }
171         // Set the status bar text
172         if ( statusBarTextLength > 0 ) {
173             name.setStatusBarText( tokenizer.readString( statusBarTextLength ) );
174         }
175     }
176 
177     /**
178      * @see org.millscript.office.excel.RecordSyntax#newRecord(RecordTokenizer)
179      */
180     @Override
181     public Name newRecord( final RecordTokenizer tokenizer ) {
182         final Name name = new Name();
183         switch ( tokenizer.getBiffVersion() ) {
184             case BIFF2:
185                 this.handleBIFF2( tokenizer, name );
186                 break;
187             case BIFF3:
188             case BIFF4S:
189             case BIFF4W:
190                 this.handleBIFF3BIFF4( tokenizer, name );
191                 break;
192             case BIFF5:
193             case BIFF7:
194                 this.handleBIFF5BIFF7( tokenizer, name );
195                 break;
196             default:
197                 this.handleBIFF8( tokenizer, name );
198                 break;
199         }
200         return name;
201     }
202 
203 }