KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > read > biff > BooleanFormulaRecord


1 /*********************************************************************
2 *
3 * Copyright (C) 2002 Andrew Khan
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ***************************************************************************/

19
20 package jxl.read.biff;
21
22 import common.Assert;
23
24 import jxl.BooleanCell;
25 import jxl.CellType;
26 import jxl.BooleanFormulaCell;
27 import jxl.biff.FormattingRecords;
28 import jxl.biff.FormulaData;
29 import jxl.biff.WorkbookMethods;
30 import jxl.biff.formula.FormulaParser;
31 import jxl.biff.formula.ExternalSheet;
32 import jxl.biff.formula.FormulaException;
33
34 /**
35  * A boolean formula's last calculated value
36  */

37 class BooleanFormulaRecord extends CellValue
38   implements BooleanCell, FormulaData, BooleanFormulaCell
39 {
40   /**
41    * The boolean value of this cell. If this cell represents an error,
42    * this will be false
43    */

44   private boolean value;
45
46   /**
47    * A handle to the class needed to access external sheets
48    */

49   private ExternalSheet externalSheet;
50
51   /**
52    * A handle to the name table
53    */

54   private WorkbookMethods nameTable;
55
56   /**
57    * The formula as an excel string
58    */

59   private String JavaDoc formulaString;
60
61   /**
62    * The raw data
63    */

64   private byte[] data;
65
66   /**
67    * Constructs this object from the raw data
68    *
69    * @param t the raw data
70    * @param fr the formatting records
71    * @param si the sheet
72    * @param es the sheet
73    * @param nt the name table
74    */

75   public BooleanFormulaRecord(Record t, FormattingRecords fr,
76                               ExternalSheet es, WorkbookMethods nt,
77                               SheetImpl si)
78   {
79     super(t, fr, si);
80     externalSheet = es;
81     nameTable = nt;
82     value = false;
83
84     data = getRecord().getData();
85
86     Assert.verify(data[6] != 2);
87
88     value = data[8] == 1 ? true : false;
89   }
90
91   /**
92    * Interface method which Gets the boolean value stored in this cell. If
93    * this cell contains an error, then returns FALSE. Always query this cell
94    * type using the accessor method isError() prior to calling this method
95    *
96    * @return TRUE if this cell contains TRUE, FALSE if it contains FALSE or
97    * an error code
98    */

99   public boolean getValue()
100   {
101     return value;
102   }
103
104   /**
105    * Returns the numerical value as a string
106    *
107    * @return The numerical value of the formula as a string
108    */

109   public String JavaDoc getContents()
110   {
111     // return Boolean.toString(value) - only available in 1.4 or later
112
return (new Boolean JavaDoc(value)).toString();
113   }
114
115   /**
116    * Returns the cell type
117    *
118    * @return The cell type
119    */

120   public CellType getType()
121   {
122     return CellType.BOOLEAN_FORMULA;
123   }
124
125   /**
126    * Gets the raw bytes for the formula. This will include the
127    * parsed tokens array
128    *
129    * @return the raw record data
130    */

131   public byte[] getFormulaData() throws FormulaException
132   {
133     if (!getSheet().getWorkbookBof().isBiff8())
134     {
135       throw new FormulaException(FormulaException.biff8Supported);
136     }
137
138     // Lop off the standard information
139
byte[] d = new byte[data.length - 6];
140     System.arraycopy(data, 6, d, 0, data.length - 6);
141
142     return d;
143   }
144
145   /**
146    * Gets the formula as an excel string
147    *
148    * @return the formula as an excel string
149    * @exception FormulaException
150    */

151   public String JavaDoc getFormula() throws FormulaException
152   {
153     if (formulaString == null)
154     {
155       byte[] tokens = new byte[data.length - 22];
156       System.arraycopy(data, 22, tokens, 0, tokens.length);
157       FormulaParser fp = new FormulaParser
158         (tokens, this, externalSheet, nameTable,
159          getSheet().getWorkbook().getSettings());
160       fp.parse();
161       formulaString = fp.getFormula();
162     }
163
164     return formulaString;
165   }
166 }
167
168
Popular Tags