KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.poi.hssf.record;
20
21 /**
22  * Title: Record
23  * Description: All HSSF Records inherit from this class. It
24  * populates the fields common to all records (id, size and data).
25  * Subclasses should be sure to validate the id,
26  * Company:
27  * @author Andrew C. Oliver
28  * @author Marc Johnson (mjohnson at apache dot org)
29  * @author Jason Height (jheight at chariot dot net dot au)
30  * @version 2.0-pre
31  */

32
33 public abstract class Record
34 {
35
36     /**
37      * instantiates a blank record strictly for ID matching
38      */

39
40     public Record()
41     {
42     }
43
44     /**
45      * Constructor Record
46      *
47      * @param id record id
48      * @param size record size
49      * @param data raw data
50      */

51
52     public Record(short id, short size, byte [] data)
53     {
54         validateSid(id);
55         fillFields(data, size);
56     }
57
58     /**
59      * Constructor Record
60      *
61      * @param id record id
62      * @param size record size
63      * @param data raw data
64      */

65
66     public Record(short id, short size, byte [] data, int offset)
67     {
68         validateSid(id);
69         fillFields(data, size, offset);
70     }
71
72     /**
73      * called by constructor, should throw runtime exception in the event of a
74      * record passed with a differing ID.
75      *
76      * @param id alleged id for this record
77      */

78
79     protected abstract void validateSid(short id);
80
81     /**
82      * called by the constructor, should set class level fields. Should throw
83      * runtime exception for bad/icomplete data.
84      *
85      * @param data raw data
86      */

87
88     protected void fillFields(byte [] data, short size)
89     {
90         fillFields(data, size, 0);
91     }
92
93     /**
94      * called by the constructor, should set class level fields. Should throw
95      * runtime exception for bad/icomplete data.
96      *
97      * @param data raw data
98      * @param size size of data
99      * @param offset of the record's data (provided a big array of the file)
100      */

101
102     protected abstract void fillFields(byte [] data, short size, int offset);
103
104     /**
105      * called by the class that is responsible for writing this sucker.
106      * Subclasses should implement this so that their data is passed back in a
107      * byte array.
108      *
109      * @return byte array containing instance data
110      */

111
112     public byte [] serialize()
113     {
114         byte[] retval = new byte[ getRecordSize() ];
115
116         serialize(0, retval);
117         return retval;
118     }
119
120     /**
121      * called by the class that is responsible for writing this sucker.
122      * Subclasses should implement this so that their data is passed back in a
123      * byte array.
124      *
125      * @param offset to begin writing at
126      * @param data byte array containing instance data
127      * @return number of bytes written
128      */

129
130     public abstract int serialize(int offset, byte [] data);
131
132     /**
133      * gives the current serialized size of the record. Should include the sid and reclength (4 bytes).
134      */

135
136     public int getRecordSize()
137     {
138
139         // this is kind od a stupid way to do it but for now we just serialize
140
// the record and return the size of the byte array
141
return serialize().length;
142     }
143
144     /**
145      * tells whether this type of record contains a value
146      */

147
148     public boolean isValue()
149     {
150         return false;
151     }
152
153     /**
154      * DBCELL, ROW, VALUES all say yes
155      */

156
157     public boolean isInValueSection()
158     {
159         return false;
160     }
161
162     /**
163      * get a string representation of the record (for biffview/debugging)
164      */

165
166     public String JavaDoc toString()
167     {
168         return super.toString();
169     }
170
171     /**
172      * Process a continuation record; default handling is to ignore
173      * it -- TODO add logging
174      *
175      * @param record the continuation record's data
176      */

177
178     // made public to satisfy biffviewer
179

180     /* protected */
181     public void processContinueRecord(byte [] record)
182     {
183
184         // System.out.println("Got a continue record ... NOW what??");
185
}
186
187     /**
188      * return the non static version of the id for this record.
189      */

190
191     public abstract short getSid();
192
193     public Object JavaDoc clone() {
194       throw new RuntimeException JavaDoc("The class "+getClass().getName()+" needs to define a clone method");
195     }
196 }
197
Popular Tags