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.IntList;
021: import org.apache.poi.util.LittleEndian;
022:
023: /**
024: * Title: Index Record<P>
025: * Description: Occurs right after BOF, tells you where the DBCELL records are for a sheet
026: * Important for locating cells<P>
027: * NOT USED IN THIS RELEASE
028: * REFERENCE: PG 323 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
029: * @author Andrew C. Oliver (acoliver at apache dot org)
030: * @author Jason Height (jheight at chariot dot net dot au)
031: * @version 2.0-pre
032: */
033:
034: public class IndexRecord extends Record {
035: public final static short sid = 0x20B;
036: public final static int DBCELL_CAPACITY = 30;
037: public int field_1_zero; // reserved must be 0
038: public int field_2_first_row; // first row on the sheet
039: public int field_3_last_row_add1; // last row
040: public int field_4_zero; // reserved must be 0
041: public IntList field_5_dbcells; // array of offsets to DBCELL records
042:
043: public IndexRecord() {
044: }
045:
046: /**
047: * Constructs an Index record and sets its fields appropriately.
048: * @param in the RecordInputstream to read the record from
049: */
050:
051: public IndexRecord(RecordInputStream in) {
052: super (in);
053: }
054:
055: protected void validateSid(short id) {
056: if (id != sid) {
057: throw new RecordFormatException("NOT An Index RECORD");
058: }
059: }
060:
061: protected void fillFields(RecordInputStream in) {
062: field_5_dbcells = new IntList(DBCELL_CAPACITY); // initial capacity of 30
063: field_1_zero = in.readInt();
064: field_2_first_row = in.readInt();
065: field_3_last_row_add1 = in.readInt();
066: field_4_zero = in.readInt();
067: while (in.remaining() > 0) {
068:
069: // System.out.println("getting " + k);
070: field_5_dbcells.add(in.readInt());
071: }
072: }
073:
074: public void setFirstRow(int row) {
075: field_2_first_row = row;
076: }
077:
078: public void setLastRowAdd1(int row) {
079: field_3_last_row_add1 = row;
080: }
081:
082: public void addDbcell(int cell) {
083: if (field_5_dbcells == null) {
084: field_5_dbcells = new IntList();
085: }
086: field_5_dbcells.add(cell);
087: }
088:
089: public void setDbcell(int cell, int value) {
090: field_5_dbcells.set(cell, value);
091: }
092:
093: public int getFirstRow() {
094: return field_2_first_row;
095: }
096:
097: public int getLastRowAdd1() {
098: return field_3_last_row_add1;
099: }
100:
101: public int getNumDbcells() {
102: if (field_5_dbcells == null) {
103: return 0;
104: }
105: return field_5_dbcells.size();
106: }
107:
108: public int getDbcellAt(int cellnum) {
109: return field_5_dbcells.get(cellnum);
110: }
111:
112: public String toString() {
113: StringBuffer buffer = new StringBuffer();
114:
115: buffer.append("[INDEX]\n");
116: buffer.append(" .firstrow = ").append(
117: Integer.toHexString(getFirstRow())).append("\n");
118: buffer.append(" .lastrowadd1 = ").append(
119: Integer.toHexString(getLastRowAdd1())).append("\n");
120: for (int k = 0; k < getNumDbcells(); k++) {
121: buffer.append(" .dbcell_" + k + " = ").append(
122: Integer.toHexString(getDbcellAt(k))).append("\n");
123: }
124: buffer.append("[/INDEX]\n");
125: return buffer.toString();
126: }
127:
128: public int serialize(int offset, byte[] data) {
129: LittleEndian.putShort(data, 0 + offset, sid);
130: LittleEndian.putShort(data, 2 + offset,
131: (short) (16 + (getNumDbcells() * 4)));
132: LittleEndian.putInt(data, 4 + offset, 0);
133: LittleEndian.putInt(data, 8 + offset, getFirstRow());
134: LittleEndian.putInt(data, 12 + offset, getLastRowAdd1());
135: LittleEndian.putInt(data, 16 + offset, 0);
136: for (int k = 0; k < getNumDbcells(); k++) {
137: LittleEndian.putInt(data, (k * 4) + 20 + offset,
138: getDbcellAt(k));
139: }
140: return getRecordSize();
141: }
142:
143: public int getRecordSize() {
144: return 20 + (getNumDbcells() * 4);
145: }
146:
147: /** Returns the size of an INdexRecord when it needs to index the specified number of blocks
148: *
149: */
150: public static int getRecordSizeForBlockCount(int blockCount) {
151: return 20 + (4 * blockCount);
152: }
153:
154: public short getSid() {
155: return sid;
156: }
157:
158: public Object clone() {
159: IndexRecord rec = new IndexRecord();
160: rec.field_1_zero = field_1_zero;
161: rec.field_2_first_row = field_2_first_row;
162: rec.field_3_last_row_add1 = field_3_last_row_add1;
163: rec.field_4_zero = field_4_zero;
164: rec.field_5_dbcells = new IntList();
165: rec.field_5_dbcells.addAll(field_5_dbcells);
166: return rec;
167: }
168: }
|