KickJava   Java API By Example, From Geeks To Geeks.

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


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.ErrorCell;
25 import jxl.CellType;
26 import jxl.ErrorFormulaCell;
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  * An error resulting from the calculation of a formula
36  */

37 class ErrorFormulaRecord extends CellValue
38   implements ErrorCell, FormulaData, ErrorFormulaCell
39 {
40   /**
41    * The error code of this cell
42    */

43   private int errorCode;
44
45   /**
46    * A handle to the class needed to access external sheets
47    */

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

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

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

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

74   public ErrorFormulaRecord(Record t, FormattingRecords fr, ExternalSheet es,
75                             WorkbookMethods nt,
76                             SheetImpl si)
77   {
78     super(t, fr, si);
79
80     externalSheet = es;
81     nameTable = nt;
82     data = getRecord().getData();
83
84     Assert.verify(data[6] == 2);
85
86     errorCode = data[8];
87   }
88
89   /**
90    * Interface method which gets the error code for this cell. If this cell
91    * does not represent an error, then it returns 0. Always use the
92    * method isError() to determine this prior to calling this method
93    *
94    * @return the error code if this cell contains an error, 0 otherwise
95    */

96   public int getErrorCode()
97   {
98     return errorCode;
99   }
100
101   /**
102    * Returns the numerical value as a string
103    *
104    * @return The numerical value of the formula as a string
105    */

106   public String JavaDoc getContents()
107   {
108     return "ERROR " + errorCode;
109   }
110
111   /**
112    * Returns the cell type
113    *
114    * @return The cell type
115    */

116   public CellType getType()
117   {
118     return CellType.FORMULA_ERROR;
119   }
120
121   /**
122    * Gets the raw bytes for the formula. This will include the
123    * parsed tokens array
124    *
125    * @return the raw record data
126    */

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

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