KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > write > biff > NameRecord


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.write.biff;
21
22 import jxl.biff.Type;
23 import jxl.biff.IntegerHelper;
24 import jxl.biff.StringHelper;
25 import jxl.biff.DoubleHelper;
26 import jxl.biff.WritableRecordData;
27
28 /**
29  * A name record. Simply takes the binary data from the name
30  * record read in
31  */

32 class NameRecord extends WritableRecordData
33 {
34   /**
35    * The binary data for output to file
36    */

37   private byte[] data;
38
39   /**
40    * The name
41    */

42   private String JavaDoc name;
43
44   /**
45    * The index into the name table
46    */

47   private int index;
48   
49   /**
50    * The 0-based index sheet reference for a record name
51    * 0 is for a global reference
52    */

53   private int sheetRef = 0;
54
55   /**
56    * A nested class to hold range information
57    */

58   class NameRange
59   {
60     private int columnFirst;
61     private int rowFirst;
62     private int columnLast;
63     private int rowLast;
64     private int externalSheet;
65
66     NameRange(jxl.read.biff.NameRecord.NameRange nr)
67     {
68       columnFirst = nr.getFirstColumn();
69       rowFirst = nr.getFirstRow();
70       columnLast = nr.getLastColumn();
71       rowLast = nr.getLastRow();
72       externalSheet = nr.getExternalSheet();
73     }
74     
75     /**
76      * Create a new range for the name record.
77      */

78     NameRange(int theSheet,
79               int theStartRow,
80               int theEndRow,
81               int theStartCol,
82               int theEndCol)
83     {
84       columnFirst = theStartCol;
85       rowFirst = theStartRow;
86       columnLast = theEndCol;
87       rowLast = theEndRow;
88       externalSheet = theSheet;
89     }
90
91     int getFirstColumn() {return columnFirst;}
92     int getFirstRow() {return rowFirst;}
93     int getLastColumn() {return columnLast;}
94     int getLastRow() {return rowLast;}
95     int getExternalSheet() { return externalSheet;}
96
97     byte[] getData()
98     {
99       byte[] d = new byte[10];
100
101       // Sheet index
102
IntegerHelper.getTwoBytes(sheetRef, d, 0);
103
104       // Starting row
105
IntegerHelper.getTwoBytes(rowFirst, d, 2);
106       
107       // End row
108
IntegerHelper.getTwoBytes(rowLast, d, 4);
109       
110       // Start column
111
IntegerHelper.getTwoBytes(columnFirst & 0xff, d, 6);
112
113       // End columns
114
IntegerHelper.getTwoBytes(columnLast & 0xff, d, 8);
115
116       return d;
117     }
118
119   }
120
121   /**
122    * The ranges covered by this name
123    */

124   private NameRange[] ranges;
125
126   // Constants which refer to the parse tokens after the string
127
private static final int cellReference = 0x3a;
128   private static final int areaReference = 0x3b;
129   private static final int subExpression = 0x29;
130   private static final int union = 0x10;
131
132   /**
133    * Constructor - used when copying sheets
134    *
135    * @param index the index into the name table
136    */

137   public NameRecord(jxl.read.biff.NameRecord sr, int ind)
138   {
139     super(Type.NAME);
140
141     data = sr.getData();
142     name = sr.getName();
143     sheetRef = sr.getSheetRef();
144     index = ind;
145
146     // Copy the ranges
147
jxl.read.biff.NameRecord.NameRange[] r = sr.getRanges();
148     ranges = new NameRange[r.length];
149     for (int i = 0 ; i < ranges.length ; i++)
150     {
151       ranges[i] = new NameRange(r[i]);
152     }
153   }
154
155   /**
156    * Create a new name record with the given information.
157    *
158    * @param theName Name to be created.
159    * @param theIndex Index of this name.
160    * @param theSheet Sheet this name refers to.
161    * @param theStartRow First row this name refers to.
162    * @param theEndRow Last row this name refers to.
163    * @param theStartCol First column this name refers to.
164    * @param theEndCol Last column this name refers to.
165    */

166   NameRecord(String JavaDoc theName,
167              int theIndex,
168              int theSheet,
169              int theStartRow,
170              int theEndRow,
171              int theStartCol,
172              int theEndCol)
173   {
174     super(Type.NAME);
175
176     name = theName;
177     index = theIndex; // Name index, not stored in data
178
sheetRef = 0; // Global name.
179

180     ranges = new NameRange[1];
181     ranges[0] = new NameRange(theSheet,
182                               theStartRow,
183                               theEndRow,
184                               theStartCol,
185                               theEndCol);
186   }
187
188   /**
189    * Gets the binary data for output to file
190    *
191    * @return the binary data
192    */

193   public byte[] getData()
194   {
195     if (data != null)
196     {
197       // this is a copy
198
return data;
199     }
200
201     final int NAME_HEADER_LENGTH = 15;
202     final byte AREA_RANGE_LENGTH = 11;
203     final byte AREA_REFERENCE = 0x3b;
204     
205     data = new byte[NAME_HEADER_LENGTH +
206                     name.length() +
207                     AREA_RANGE_LENGTH];
208
209     // Options
210
int options = 0;
211     IntegerHelper.getTwoBytes(options, data, 0);
212
213     // Keyboard shortcut
214
data[2] = 0;
215
216     // Length of the name in chars
217
data[3] = (byte) name.length();
218
219     // Size of the definitions
220
IntegerHelper.getTwoBytes(AREA_RANGE_LENGTH, data, 4);
221
222     // Sheet index
223
IntegerHelper.getTwoBytes(ranges[0].externalSheet, data, 6);
224     IntegerHelper.getTwoBytes(ranges[0].externalSheet, data, 8);
225
226     // Byte 10-13 are optional lengths [0,0,0,0]
227
// Byte 14 is length of name which is not used.
228

229     // The name
230
StringHelper.getBytes(name, data, 15);
231
232     
233     // The actual range definition.
234
int pos = name.length() + 15;
235
236     // Range format - area
237
data[pos] = areaReference;
238
239     // The range data
240
byte[] rd = ranges[0].getData();
241     System.arraycopy(rd, 0, data, pos+1, rd.length);
242
243     return data;
244   }
245
246   /**
247    * Accessor for the name
248    *
249    * @return the name
250    */

251   public String JavaDoc getName()
252   {
253     return name;
254   }
255
256   /**
257    * Accessor for the index of this name in the name table
258    *
259    * @return the index of this name in the name table
260    */

261   public int getIndex()
262   {
263     return index;
264   }
265   
266   /**
267    * The 0-based index sheet reference for a record name
268    * 0 is for a global reference
269    *
270    * @return the sheet reference for name formula
271    */

272   public int getSheetRef()
273   {
274     return sheetRef;
275   }
276   
277   /**
278    * Set the index sheet reference for a record name
279    * 0 is for a global reference
280    *
281    */

282   public void setSheetRef(int i)
283   {
284     sheetRef = i;
285     IntegerHelper.getTwoBytes(sheetRef, data, 8);
286   }
287
288   /**
289    * Gets the array of ranges for this name
290    * @return the ranges
291    */

292   public NameRange[] getRanges()
293   {
294     return ranges;
295   }
296 }
297
298
Popular Tags