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: import org.apache.poi.util.LittleEndian;
021:
022: /**
023: * Title: Unknown Record (for debugging)<P>
024: * Description: Unknown record just tells you the sid so you can figure out
025: * what records you are missing. Also helps us read/modify sheets we
026: * don't know all the records to. (HSSF leaves these alone!) <P>
027: * Company: SuperLink Software, Inc.<P>
028: * @author Andrew C. Oliver (acoliver at apache dot org)
029: * @author Jason Height (jheight at chariot dot net dot au)
030: * @author Glen Stampoultzis (glens at apache.org)
031: */
032:
033: public class UnknownRecord extends Record {
034: private short sid = 0;
035: private byte[] thedata = null;
036:
037: public UnknownRecord() {
038: }
039:
040: /**
041: * @param id id of the record -not validated, just stored for serialization
042: * @param data the data
043: */
044: public UnknownRecord(short id, byte[] data) {
045: this .sid = id;
046: this .thedata = data;
047: }
048:
049: /**
050: * construct an unknown record. No fields are interperated and the record will
051: * be serialized in its original form more or less
052: * @param in the RecordInputstream to read the record from
053: */
054:
055: public UnknownRecord(RecordInputStream in) {
056: sid = in.getSid();
057: thedata = in.readRemainder();
058:
059: //System.out.println("UnknownRecord: 0x"+Integer.toHexString(sid));
060: }
061:
062: /**
063: * spit the record out AS IS. no interpretation or identification
064: */
065: public int serialize(int offset, byte[] data) {
066: if (thedata == null) {
067: thedata = new byte[0];
068: }
069: LittleEndian.putShort(data, 0 + offset, sid);
070: LittleEndian.putShort(data, 2 + offset,
071: (short) (thedata.length));
072: if (thedata.length > 0) {
073: System.arraycopy(thedata, 0, data, 4 + offset,
074: thedata.length);
075: }
076: return getRecordSize();
077: }
078:
079: public int getRecordSize() {
080: int retval = 4;
081:
082: if (thedata != null) {
083: retval += thedata.length;
084: }
085: return retval;
086: }
087:
088: protected void fillFields(byte[] data, short sid) {
089: this .sid = sid;
090: thedata = data;
091: }
092:
093: /**
094: * NO OP!
095: */
096:
097: protected void validateSid(short id) {
098:
099: // if we had a valid sid we wouldn't be using the "Unknown Record" record now would we?
100: }
101:
102: /**
103: * print a sort of string representation ([UNKNOWN RECORD] id = x [/UNKNOWN RECORD])
104: */
105:
106: public String toString() {
107: StringBuffer buffer = new StringBuffer();
108:
109: buffer.append("[UNKNOWN RECORD:" + Integer.toHexString(sid)
110: + "]\n");
111: buffer.append(" .id = ").append(
112: Integer.toHexString(sid)).append("\n");
113: buffer.append("[/UNKNOWN RECORD]\n");
114: return buffer.toString();
115: }
116:
117: public short getSid() {
118: return sid;
119: }
120:
121: /**
122: * called by the constructor, should set class level fields. Should throw
123: * runtime exception for bad/icomplete data.
124: *
125: * @param in the RecordInputstream to read the record from
126: */
127:
128: protected void fillFields(RecordInputStream in) {
129: throw new RecordFormatException(
130: "Unknown record cannot be constructed via offset -- we need a copy of the data");
131: }
132:
133: /** Unlike the other Record.clone methods this is a shallow clone*/
134: public Object clone() {
135: UnknownRecord rec = new UnknownRecord();
136: rec.sid = sid;
137: rec.thedata = thedata;
138: return rec;
139: }
140: }
|