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: Recalc Id Record<P>
024: * Description: This record contains an ID that marks when a worksheet was last
025: * recalculated. It's an optimization Excel uses to determine if it
026: * needs to recalculate the spreadsheet when it's opened. So far, only
027: * the two values <code>0xC1 0x01 0x00 0x00 0x80 0x38 0x01 0x00</code>
028: * (do not recalculate) and <code>0xC1 0x01 0x00 0x00 0x60 0x69 0x01
029: * 0x00</code> have been seen. If the field <code>isNeeded</code> is
030: * set to false (default), then this record is swallowed during the
031: * serialization process<P>
032: * REFERENCE: http://chicago.sourceforge.net/devel/docs/excel/biff8.html<P>
033: * @author Luc Girardin (luc dot girardin at macrofocus dot com)
034: * @version 2.0-pre
035: * @see org.apache.poi.hssf.model.Workbook
036: */
037:
038: public class RecalcIdRecord extends Record {
039: public final static short sid = 0x1c1;
040: public short[] field_1_recalcids;
041:
042: private boolean isNeeded = true;
043:
044: public RecalcIdRecord() {
045: }
046:
047: /**
048: * Constructs a RECALCID record and sets its fields appropriately.
049: * @param in the RecordInputstream to read the record from
050: */
051:
052: public RecalcIdRecord(RecordInputStream in) {
053: super (in);
054: }
055:
056: protected void validateSid(short id) {
057: if (id != sid) {
058: throw new RecordFormatException("NOT A RECALCID RECORD");
059: }
060: }
061:
062: protected void fillFields(RecordInputStream in) {
063: field_1_recalcids = new short[in.remaining() / 2];
064: for (int k = 0; k < field_1_recalcids.length; k++) {
065: field_1_recalcids[k] = in.readShort();
066: }
067: }
068:
069: /**
070: * set the recalc array.
071: * @param array of recalc id's
072: */
073:
074: public void setRecalcIdArray(short[] array) {
075: field_1_recalcids = array;
076: }
077:
078: /**
079: * get the recalc array.
080: * @return array of recalc id's
081: */
082:
083: public short[] getRecalcIdArray() {
084: return field_1_recalcids;
085: }
086:
087: public void setIsNeeded(boolean isNeeded) {
088: this .isNeeded = isNeeded;
089: }
090:
091: public boolean isNeeded() {
092: return isNeeded;
093: }
094:
095: public String toString() {
096: StringBuffer buffer = new StringBuffer();
097:
098: buffer.append("[RECALCID]\n");
099: buffer.append(" .elements = ").append(
100: field_1_recalcids.length).append("\n");
101: for (int k = 0; k < field_1_recalcids.length; k++) {
102: buffer.append(" .element_" + k + " = ").append(
103: field_1_recalcids[k]).append("\n");
104: }
105: buffer.append("[/RECALCID]\n");
106: return buffer.toString();
107: }
108:
109: public int serialize(int offset, byte[] data) {
110: short[] tabids = getRecalcIdArray();
111: short length = (short) (tabids.length * 2);
112: int byteoffset = 4;
113:
114: LittleEndian.putShort(data, 0 + offset, sid);
115: LittleEndian.putShort(data, 2 + offset, ((short) length));
116:
117: // 2 (num bytes in a short)
118: for (int k = 0; k < (length / 2); k++) {
119: LittleEndian.putShort(data, byteoffset + offset, tabids[k]);
120: byteoffset += 2;
121: }
122: return getRecordSize();
123: }
124:
125: public int getRecordSize() {
126: return 4 + (getRecalcIdArray().length * 2);
127: }
128:
129: public short getSid() {
130: return sid;
131: }
132: }
|