KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > record > MulRKRecord


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 /*
20  * MulRKRecord.java
21  *
22  * Created on November 9, 2001, 4:53 PM
23  */

24 package org.apache.poi.hssf.record;
25
26 import java.util.ArrayList JavaDoc;
27
28 import org.apache.poi.util.LittleEndian;
29 import org.apache.poi.hssf.util.RKUtil;
30
31 /**
32  * Used to store multiple RK numbers on a row. 1 MulRk = Multiple Cell values.
33  * HSSF just converts this into multiple NUMBER records. READ-ONLY SUPPORT!<P>
34  * REFERENCE: PG 330 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
35  * @author Andrew C. Oliver (acoliver at apache dot org)
36  * @version 2.0-pre
37  */

38
39 public class MulRKRecord
40     extends Record
41 {
42     public final static short sid = 0xbd;
43     //private short field_1_row;
44
private int field_1_row;
45     private short field_2_first_col;
46     private ArrayList JavaDoc field_3_rks;
47     private short field_4_last_col;
48
49     /** Creates new MulRKRecord */
50
51     public MulRKRecord()
52     {
53     }
54
55     /**
56      * Constructs a MulRK record and sets its fields appropriately.
57      *
58      * @param id id must be 0xbd or an exception will be throw upon validation
59      * @param size the size of the data area of the record
60      * @param data data of the record (should not contain sid/len)
61      */

62
63     public MulRKRecord(short id, short size, byte [] data)
64     {
65         super(id, size, data);
66     }
67
68     /**
69      * Constructs a MulRK record and sets its fields appropriately.
70      *
71      * @param id id must be 0xbd or an exception will be throw upon validation
72      * @param size the size of the data area of the record
73      * @param data data of the record (should not contain sid/len)
74      * @param offset of data
75      */

76
77     public MulRKRecord(short id, short size, byte [] data, int offset)
78     {
79         super(id, size, data, offset);
80     }
81
82     //public short getRow()
83
public int getRow()
84     {
85         return field_1_row;
86     }
87
88     /**
89      * starting column (first cell this holds in the row)
90      * @return first column number
91      */

92
93     public short getFirstColumn()
94     {
95         return field_2_first_col;
96     }
97
98     /**
99      * ending column (last cell this holds in the row)
100      * @return first column number
101      */

102
103     public short getLastColumn()
104     {
105         return field_4_last_col;
106     }
107
108     /**
109      * get the number of columns this contains (last-first +1)
110      * @return number of columns (last - first +1)
111      */

112
113     public int getNumColumns()
114     {
115         return field_4_last_col - field_2_first_col + 1;
116     }
117
118     /**
119      * returns the xf index for column (coffset = column - field_2_first_col)
120      * @return the XF index for the column
121      */

122
123     public short getXFAt(int coffset)
124     {
125         return (( RkRec ) field_3_rks.get(coffset)).xf;
126     }
127
128     /**
129      * returns the rk number for column (coffset = column - field_2_first_col)
130      * @return the value (decoded into a double)
131      */

132
133     public double getRKNumberAt(int coffset)
134     {
135         return RKUtil.decodeNumber((( RkRec ) field_3_rks.get(coffset)).rk);
136     }
137
138     /**
139      * called by the constructor, should set class level fields. Should throw
140      * runtime exception for bad/icomplete data.
141      *
142      * @param data raw data
143      * @param size size of data
144      */

145
146     protected void fillFields(byte [] data, short size, int offset)
147     {
148         //field_1_row = LittleEndian.getShort(data, 0 + offset);
149
field_1_row = LittleEndian.getUShort(data, 0 + offset);
150         field_2_first_col = LittleEndian.getShort(data, 2 + offset);
151         field_3_rks = parseRKs(data, 4, offset, size);
152         field_4_last_col = LittleEndian.getShort(data,
153                                                   (field_3_rks.size() * 6)
154                                                   + 4 + offset);
155     }
156
157     private ArrayList JavaDoc parseRKs(byte [] data, int offset, int recoffset,
158                                short size)
159     {
160         ArrayList JavaDoc retval = new ArrayList JavaDoc();
161
162         for (; offset < size - 2; )
163         {
164             RkRec rec = new RkRec();
165
166             rec.xf = LittleEndian.getShort(data, offset + recoffset);
167             offset += 2;
168             rec.rk = LittleEndian.getInt(data, offset + recoffset);
169             offset += 4;
170             retval.add(rec);
171         }
172         return retval;
173     }
174
175     public String JavaDoc toString()
176     {
177         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
178
179         buffer.append("[MULRK]\n");
180         buffer.append("firstcol = ")
181             .append(Integer.toHexString(getFirstColumn())).append("\n");
182         buffer.append(" lastcol = ")
183             .append(Integer.toHexString(getLastColumn())).append("\n");
184         for (int k = 0; k < getNumColumns(); k++)
185         {
186             buffer.append("xf").append(k).append(" = ")
187                 .append(Integer.toHexString(getXFAt(k))).append("\n");
188             buffer.append("rk").append(k).append(" = ")
189                 .append(getRKNumberAt(k)).append("\n");
190         }
191         buffer.append("[/MULRK]\n");
192         return buffer.toString();
193     }
194
195     /**
196      * called by constructor, should throw runtime exception in the event of a
197      * record passed with a differing ID.
198      *
199      * @param id alleged id for this record
200      */

201
202     protected void validateSid(short id)
203     {
204         if (id != sid)
205         {
206             throw new RecordFormatException("Not a MulRKRecord!");
207         }
208     }
209
210     public short getSid()
211     {
212         return this.sid;
213     }
214
215     public int serialize(int offset, byte [] data)
216     {
217         throw new RecordFormatException(
218             "Sorry, you can't serialize a MulRK in this release");
219     }
220 }
221
222 class RkRec
223 {
224     public short xf;
225     public int rk;
226 }
227
Popular Tags