KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.poi.util.LittleEndian;
22
23 /**
24  * Title: Unknown Record (for debugging)<P>
25  * Description: Unknown record just tells you the sid so you can figure out
26  * what records you are missing. Also helps us read/modify sheets we
27  * don't know all the records to. (HSSF leaves these alone!) <P>
28  * Company: SuperLink Software, Inc.<P>
29  * @author Andrew C. Oliver (acoliver at apache dot org)
30  * @author Jason Height (jheight at chariot dot net dot au)
31  * @author Glen Stampoultzis (glens at apache.org)
32  */

33
34 public class UnknownRecord
35     extends Record
36 {
37     private short sid = 0;
38     private byte[] thedata = null;
39
40     public UnknownRecord()
41     {
42     }
43
44     /**
45      * construct an unknown record. No fields are interperated and the record will
46      * be serialized in its original form more or less
47      * @param id id of the record -not validated, just stored for serialization
48      * @param size size of the data
49      * @param data the data
50      */

51
52     public UnknownRecord(short id, short size, byte [] data)
53     {
54         sid = id;
55         thedata = data;
56     }
57
58     public UnknownRecord( short id, short size, byte[] data, int offset )
59     {
60         sid = id;
61         thedata = new byte[size];
62         System.arraycopy(data, offset, thedata, 0, size);
63     }
64
65     /**
66      * spit the record out AS IS. no interpretation or identification
67      */

68     public int serialize(int offset, byte [] data)
69     {
70         if (thedata == null)
71         {
72             thedata = new byte[ 0 ];
73         }
74         LittleEndian.putShort(data, 0 + offset, sid);
75         LittleEndian.putShort(data, 2 + offset, ( short ) (thedata.length));
76         if (thedata.length > 0)
77         {
78             System.arraycopy(thedata, 0, data, 4 + offset, thedata.length);
79         }
80         return getRecordSize();
81     }
82
83     public int getRecordSize()
84     {
85         int retval = 4;
86
87         if (thedata != null)
88         {
89             retval += thedata.length;
90         }
91         return retval;
92     }
93
94     protected void fillFields(byte [] data, short sid)
95     {
96         this.sid = sid;
97         thedata = data;
98     }
99
100     /**
101      * NO OP!
102      */

103
104     protected void validateSid(short id)
105     {
106
107         // if we had a valid sid we wouldn't be using the "Unknown Record" record now would we?
108
}
109
110     /**
111      * print a sort of string representation ([UNKNOWN RECORD] id = x [/UNKNOWN RECORD])
112      */

113
114     public String JavaDoc toString()
115     {
116         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
117
118         buffer.append("[UNKNOWN RECORD:" + Integer.toHexString(sid) + "]\n");
119         buffer.append(" .id = ").append(Integer.toHexString(sid))
120             .append("\n");
121         buffer.append("[/UNKNOWN RECORD]\n");
122         return buffer.toString();
123     }
124
125     public short getSid()
126     {
127         return this.sid;
128     }
129
130     /**
131      * called by the constructor, should set class level fields. Should throw
132      * runtime exception for bad/icomplete data.
133      *
134      * @param data raw data
135      * @param size size of data
136      * @param offset of the records data (provided a big array of the file)
137      */

138
139     protected void fillFields(byte [] data, short size, int offset)
140     {
141         throw new RecordFormatException(
142             "Unknown record cannot be constructed via offset -- we need a copy of the data");
143     }
144
145     /** Unlike the other Record.clone methods this is a shallow clone*/
146     public Object JavaDoc clone() {
147       UnknownRecord rec = new UnknownRecord();
148       rec.sid = sid;
149       rec.thedata = thedata;
150       return rec;
151     }
152 }
153
Popular Tags