KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > biff > formula > NameRange


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.biff.formula;
21
22 import jxl.biff.IntegerHelper;
23 import jxl.biff.WorkbookMethods;
24
25 /**
26  * A name operand
27  */

28 class NameRange extends Operand implements ParsedThing
29 {
30   /**
31    * A handle to the name table
32    */

33   private WorkbookMethods nameTable;
34
35   /**
36    * The string name
37    */

38   private String JavaDoc name;
39   
40   /**
41    * The index into the name table
42    */

43   private int index;
44
45   /**
46    * Constructor
47    */

48   public NameRange(WorkbookMethods nt)
49   {
50     nameTable = nt;
51   }
52
53   /**
54    * Constructor when parsing a string via the api
55    *
56    * @param nm the name string
57    * @param nt the name table
58    */

59   public NameRange(String JavaDoc nm, WorkbookMethods nt) throws FormulaException
60   {
61     name = nm;
62     nameTable = nt;
63
64     index = nameTable.getNameIndex(name);
65
66     if (index < 0 )
67     {
68       throw new FormulaException(FormulaException.cellNameNotFound, name);
69     }
70
71     index += 1; // indexes are 1-based
72
}
73
74   /**
75    * Reads the ptg data from the array starting at the specified position
76    *
77    * @param data the RPN array
78    * @param pos the current position in the array, excluding the ptg identifier
79    * @return the number of bytes read
80    */

81   public int read(byte[] data, int pos)
82   {
83     index = IntegerHelper.getInt(data[pos], data[pos+1]);
84
85     name = nameTable.getName(index - 1); // ilbl is 1-based
86

87     return 4;
88   }
89
90   /**
91    * Gets the token representation of this item in RPN
92    *
93    * @return the bytes applicable to this formula
94    */

95   byte[] getBytes()
96   {
97     byte[] data = new byte[5];
98     data[0] = Token.NAMED_RANGE.getCode();
99
100     IntegerHelper.getTwoBytes(index, data, 1);
101
102     return data;
103   }
104
105   /**
106    * Abstract method implementation to get the string equivalent of this
107    * token
108    *
109    * @param buf the string to append to
110    */

111   public void getString(StringBuffer JavaDoc buf)
112   {
113     buf.append(name);
114   }
115 }
116
Popular Tags