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: /*
019: * MulRKRecord.java
020: *
021: * Created on November 9, 2001, 4:53 PM
022: */
023: package org.apache.poi.hssf.record;
024:
025: import java.util.ArrayList;
026:
027: import org.apache.poi.hssf.util.RKUtil;
028:
029: /**
030: * Used to store multiple RK numbers on a row. 1 MulRk = Multiple Cell values.
031: * HSSF just converts this into multiple NUMBER records. READ-ONLY SUPPORT!<P>
032: * REFERENCE: PG 330 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
033: * @author Andrew C. Oliver (acoliver at apache dot org)
034: * @version 2.0-pre
035: */
036:
037: public class MulRKRecord extends Record {
038: public final static short sid = 0xbd;
039: //private short field_1_row;
040: private int field_1_row;
041: private short field_2_first_col;
042: private ArrayList field_3_rks;
043: private short field_4_last_col;
044:
045: /** Creates new MulRKRecord */
046:
047: public MulRKRecord() {
048: }
049:
050: /**
051: * Constructs a MulRK record and sets its fields appropriately.
052: *
053: * @param in the RecordInputstream to read the record from
054: */
055:
056: public MulRKRecord(RecordInputStream in) {
057: super (in);
058: }
059:
060: //public short getRow()
061: public int getRow() {
062: return field_1_row;
063: }
064:
065: /**
066: * starting column (first cell this holds in the row)
067: * @return first column number
068: */
069:
070: public short getFirstColumn() {
071: return field_2_first_col;
072: }
073:
074: /**
075: * ending column (last cell this holds in the row)
076: * @return first column number
077: */
078:
079: public short getLastColumn() {
080: return field_4_last_col;
081: }
082:
083: /**
084: * get the number of columns this contains (last-first +1)
085: * @return number of columns (last - first +1)
086: */
087:
088: public int getNumColumns() {
089: return field_4_last_col - field_2_first_col + 1;
090: }
091:
092: /**
093: * returns the xf index for column (coffset = column - field_2_first_col)
094: * @return the XF index for the column
095: */
096:
097: public short getXFAt(int coffset) {
098: return ((RkRec) field_3_rks.get(coffset)).xf;
099: }
100:
101: /**
102: * returns the rk number for column (coffset = column - field_2_first_col)
103: * @return the value (decoded into a double)
104: */
105:
106: public double getRKNumberAt(int coffset) {
107: return RKUtil
108: .decodeNumber(((RkRec) field_3_rks.get(coffset)).rk);
109: }
110:
111: /**
112: * @param in the RecordInputstream to read the record from
113: */
114: protected void fillFields(RecordInputStream in) {
115: //field_1_row = LittleEndian.getShort(data, 0 + offset);
116: field_1_row = in.readUShort();
117: field_2_first_col = in.readShort();
118: field_3_rks = parseRKs(in);
119: field_4_last_col = in.readShort();
120: }
121:
122: private ArrayList parseRKs(RecordInputStream in) {
123: ArrayList retval = new ArrayList();
124: while ((in.remaining() - 2) > 0) {
125: RkRec rec = new RkRec();
126:
127: rec.xf = in.readShort();
128: rec.rk = in.readInt();
129: retval.add(rec);
130: }
131: return retval;
132: }
133:
134: public String toString() {
135: StringBuffer buffer = new StringBuffer();
136:
137: buffer.append("[MULRK]\n");
138: buffer.append("firstcol = ").append(
139: Integer.toHexString(getFirstColumn())).append("\n");
140: buffer.append(" lastcol = ").append(
141: Integer.toHexString(getLastColumn())).append("\n");
142: for (int k = 0; k < getNumColumns(); k++) {
143: buffer.append("xf").append(k).append(" = ").append(
144: Integer.toHexString(getXFAt(k))).append("\n");
145: buffer.append("rk").append(k).append(" = ").append(
146: getRKNumberAt(k)).append("\n");
147: }
148: buffer.append("[/MULRK]\n");
149: return buffer.toString();
150: }
151:
152: /**
153: * called by constructor, should throw runtime exception in the event of a
154: * record passed with a differing ID.
155: *
156: * @param id alleged id for this record
157: */
158:
159: protected void validateSid(short id) {
160: if (id != sid) {
161: throw new RecordFormatException("Not a MulRKRecord!");
162: }
163: }
164:
165: public short getSid() {
166: return sid;
167: }
168:
169: public int serialize(int offset, byte[] data) {
170: throw new RecordFormatException(
171: "Sorry, you can't serialize a MulRK in this release");
172: }
173: }
174:
175: class RkRec {
176: public short xf;
177: public int rk;
178: }
|