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: Sheet Tab Index Array Record<P>
024: * Description: Contains an array of sheet id's. Sheets always keep their ID
025: * regardless of what their name is.<P>
026: * REFERENCE: PG 412 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
027: * @author Andrew C. Oliver (acoliver at apache dot org)
028: * @version 2.0-pre
029: */
030:
031: public class TabIdRecord extends Record {
032: public final static short sid = 0x13d;
033: public short[] field_1_tabids;
034:
035: public TabIdRecord() {
036: }
037:
038: /**
039: * Constructs a TabID record and sets its fields appropriately.
040: * @param in the RecordInputstream to read the record from
041: */
042:
043: public TabIdRecord(RecordInputStream in) {
044: super (in);
045: }
046:
047: protected void validateSid(short id) {
048: if (id != sid) {
049: throw new RecordFormatException("NOT A TABID RECORD");
050: }
051: }
052:
053: protected void fillFields(RecordInputStream in) {
054: field_1_tabids = new short[in.remaining() / 2];
055: for (int k = 0; k < field_1_tabids.length; k++) {
056: field_1_tabids[k] = in.readShort();
057: }
058: }
059:
060: /**
061: * set the tab array. (0,1,2).
062: * @param array of tab id's {0,1,2}
063: */
064:
065: public void setTabIdArray(short[] array) {
066: field_1_tabids = array;
067: }
068:
069: /**
070: * get the tab array. (0,1,2).
071: * @return array of tab id's {0,1,2}
072: */
073:
074: public short[] getTabIdArray() {
075: return field_1_tabids;
076: }
077:
078: public String toString() {
079: StringBuffer buffer = new StringBuffer();
080:
081: buffer.append("[TABID]\n");
082: buffer.append(" .elements = ").append(
083: field_1_tabids.length).append("\n");
084: for (int k = 0; k < field_1_tabids.length; k++) {
085: buffer.append(" .element_" + k + " = ").append(
086: field_1_tabids[k]).append("\n");
087: }
088: buffer.append("[/TABID]\n");
089: return buffer.toString();
090: }
091:
092: public int serialize(int offset, byte[] data) {
093: short[] tabids = getTabIdArray();
094: short length = (short) (tabids.length * 2);
095: int byteoffset = 4;
096:
097: LittleEndian.putShort(data, 0 + offset, sid);
098: LittleEndian.putShort(data, 2 + offset, ((short) length)); // nubmer tabids *
099:
100: // 2 (num bytes in a short)
101: for (int k = 0; k < (length / 2); k++) {
102: LittleEndian.putShort(data, byteoffset + offset, tabids[k]);
103: byteoffset += 2;
104: }
105: return getRecordSize();
106: }
107:
108: public int getRecordSize() {
109: return 4 + (getTabIdArray().length * 2);
110: }
111:
112: public short getSid() {
113: return sid;
114: }
115: }
|