001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hssf.record;
019:
020: /**
021: * Title: Record
022: * Description: All HSSF Records inherit from this class. It
023: * populates the fields common to all records (id, size and data).
024: * Subclasses should be sure to validate the id,
025: * Company:
026: * @author Andrew C. Oliver
027: * @author Marc Johnson (mjohnson at apache dot org)
028: * @author Jason Height (jheight at chariot dot net dot au)
029: * @version 2.0-pre
030: */
031:
032: public abstract class Record {
033:
034: /**
035: * instantiates a blank record strictly for ID matching
036: */
037:
038: public Record() {
039: }
040:
041: /**
042: * Constructor Record
043: *
044: * @param in the RecordInputstream to read the record from
045: */
046: public Record(RecordInputStream in) {
047: validateSid(in.getSid());
048: fillFields(in);
049: }
050:
051: /**
052: * called by constructor, should throw runtime exception in the event of a
053: * record passed with a differing ID.
054: *
055: * @param id alleged id for this record
056: */
057:
058: protected abstract void validateSid(short id);
059:
060: /**
061: * called by the constructor, should set class level fields. Should throw
062: * runtime exception for bad/icomplete data.
063: *
064: * @param in the RecordInputstream to read the record from
065: */
066:
067: protected abstract void fillFields(RecordInputStream in);
068:
069: /**
070: * called by the class that is responsible for writing this sucker.
071: * Subclasses should implement this so that their data is passed back in a
072: * byte array.
073: *
074: * @return byte array containing instance data
075: */
076:
077: public byte[] serialize() {
078: byte[] retval = new byte[getRecordSize()];
079:
080: serialize(0, retval);
081: return retval;
082: }
083:
084: /**
085: * called by the class that is responsible for writing this sucker.
086: * Subclasses should implement this so that their data is passed back in a
087: * byte array.
088: *
089: * @param offset to begin writing at
090: * @param data byte array containing instance data
091: * @return number of bytes written
092: */
093:
094: public abstract int serialize(int offset, byte[] data);
095:
096: /**
097: * gives the current serialized size of the record. Should include the sid and reclength (4 bytes).
098: */
099:
100: public int getRecordSize() {
101:
102: // this is kind od a stupid way to do it but for now we just serialize
103: // the record and return the size of the byte array
104: return serialize().length;
105: }
106:
107: /**
108: * tells whether this type of record contains a value
109: */
110:
111: public boolean isValue() {
112: return false;
113: }
114:
115: /**
116: * DBCELL, ROW, VALUES all say yes
117: */
118:
119: public boolean isInValueSection() {
120: return false;
121: }
122:
123: /**
124: * get a string representation of the record (for biffview/debugging)
125: */
126:
127: public String toString() {
128: return super .toString();
129: }
130:
131: /**
132: * return the non static version of the id for this record.
133: */
134:
135: public abstract short getSid();
136:
137: public Object clone() {
138: throw new RuntimeException("The class " + getClass().getName()
139: + " needs to define a clone method");
140: }
141: }
|