From: Josh Micich Date: Thu, 30 Oct 2008 22:07:26 +0000 (+0000) Subject: Removed dodgy superlcass implementation of Record.getRecordSize() X-Git-Tag: trunk_20081106~18 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b262882d9d4409a19e2daf4659dfc3e980231e74;p=poi.git Removed dodgy superlcass implementation of Record.getRecordSize() git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@709263 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/poi/hssf/eventusermodel/dummyrecord/LastCellOfRowDummyRecord.java b/src/java/org/apache/poi/hssf/eventusermodel/dummyrecord/LastCellOfRowDummyRecord.java index 670fa99d2a..49e77ebf94 100644 --- a/src/java/org/apache/poi/hssf/eventusermodel/dummyrecord/LastCellOfRowDummyRecord.java +++ b/src/java/org/apache/poi/hssf/eventusermodel/dummyrecord/LastCellOfRowDummyRecord.java @@ -18,12 +18,13 @@ package org.apache.poi.hssf.eventusermodel.dummyrecord; import org.apache.poi.hssf.record.Record; +import org.apache.poi.hssf.record.RecordFormatException; /** * A dummy record to indicate that we've now had the last * cell record for this row. */ -public class LastCellOfRowDummyRecord extends Record { +public final class LastCellOfRowDummyRecord extends Record { private int row; private int lastColumnNumber; @@ -50,6 +51,9 @@ public class LastCellOfRowDummyRecord extends Record { return -1; } public int serialize(int offset, byte[] data) { - return -1; + throw new RecordFormatException("Cannot serialize a dummy record"); + } + public int getRecordSize() { + throw new RecordFormatException("Cannot serialize a dummy record"); } } diff --git a/src/java/org/apache/poi/hssf/eventusermodel/dummyrecord/MissingCellDummyRecord.java b/src/java/org/apache/poi/hssf/eventusermodel/dummyrecord/MissingCellDummyRecord.java index 587ccb6596..c8adb4ae88 100644 --- a/src/java/org/apache/poi/hssf/eventusermodel/dummyrecord/MissingCellDummyRecord.java +++ b/src/java/org/apache/poi/hssf/eventusermodel/dummyrecord/MissingCellDummyRecord.java @@ -18,12 +18,13 @@ package org.apache.poi.hssf.eventusermodel.dummyrecord; import org.apache.poi.hssf.record.Record; +import org.apache.poi.hssf.record.RecordFormatException; /** * A dummy record for when we're missing a cell in a row, * but still want to trigger something */ -public class MissingCellDummyRecord extends Record { +public final class MissingCellDummyRecord extends Record { private int row; private int column; @@ -36,7 +37,10 @@ public class MissingCellDummyRecord extends Record { return -1; } public int serialize(int offset, byte[] data) { - return -1; + throw new RecordFormatException("Cannot serialize a dummy record"); + } + public int getRecordSize() { + throw new RecordFormatException("Cannot serialize a dummy record"); } public int getRow() { return row; } diff --git a/src/java/org/apache/poi/hssf/eventusermodel/dummyrecord/MissingRowDummyRecord.java b/src/java/org/apache/poi/hssf/eventusermodel/dummyrecord/MissingRowDummyRecord.java index 46f76b1d34..c268bccfc2 100644 --- a/src/java/org/apache/poi/hssf/eventusermodel/dummyrecord/MissingRowDummyRecord.java +++ b/src/java/org/apache/poi/hssf/eventusermodel/dummyrecord/MissingRowDummyRecord.java @@ -18,13 +18,13 @@ package org.apache.poi.hssf.eventusermodel.dummyrecord; import org.apache.poi.hssf.record.Record; -import org.apache.poi.hssf.record.RecordInputStream; +import org.apache.poi.hssf.record.RecordFormatException; /** * A dummy record for when we're missing a row, but still * want to trigger something */ -public class MissingRowDummyRecord extends Record { +public final class MissingRowDummyRecord extends Record { private int rowNumber; public MissingRowDummyRecord(int rowNumber) { @@ -35,7 +35,10 @@ public class MissingRowDummyRecord extends Record { return -1; } public int serialize(int offset, byte[] data) { - return -1; + throw new RecordFormatException("Cannot serialize a dummy record"); + } + public int getRecordSize() { + throw new RecordFormatException("Cannot serialize a dummy record"); } public int getRowNumber() { diff --git a/src/java/org/apache/poi/hssf/record/ContinueRecord.java b/src/java/org/apache/poi/hssf/record/ContinueRecord.java index e20c4633c3..c67012aa7c 100644 --- a/src/java/org/apache/poi/hssf/record/ContinueRecord.java +++ b/src/java/org/apache/poi/hssf/record/ContinueRecord.java @@ -1,4 +1,3 @@ - /* ==================================================================== Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with @@ -15,7 +14,6 @@ See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ - package org.apache.poi.hssf.record; @@ -31,20 +29,14 @@ import org.apache.poi.util.LittleEndian; */ public final class ContinueRecord extends Record { public final static short sid = 0x003C; - private byte[] _data; + private byte[] _data; public ContinueRecord(byte[] data) { _data = data; } - /** - * USE ONLY within "processContinue" - */ - public byte [] serialize() - { - byte[] retval = new byte[ _data.length + 4 ]; - serialize(0, retval); - return retval; + public int getRecordSize() { + return 4 + _data.length; } public int serialize(int offset, byte[] data) { diff --git a/src/java/org/apache/poi/hssf/record/LabelRecord.java b/src/java/org/apache/poi/hssf/record/LabelRecord.java index f4bbfb85da..9661f991f4 100644 --- a/src/java/org/apache/poi/hssf/record/LabelRecord.java +++ b/src/java/org/apache/poi/hssf/record/LabelRecord.java @@ -114,10 +114,11 @@ public final class LabelRecord extends Record implements CellValueRecordInterfac /** * THROWS A RUNTIME EXCEPTION.. USE LABELSSTRecords. YOU HAVE NO REASON to use LABELRecord!! */ - public int serialize(int offset, byte [] data) - { - throw new RecordFormatException( - "Label Records are supported READ ONLY...convert to LabelSST"); + public int serialize(int offset, byte [] data) { + throw new RecordFormatException("Label Records are supported READ ONLY...convert to LabelSST"); + } + public int getRecordSize() { + throw new RecordFormatException("Label Records are supported READ ONLY...convert to LabelSST"); } public short getSid() diff --git a/src/java/org/apache/poi/hssf/record/MulBlankRecord.java b/src/java/org/apache/poi/hssf/record/MulBlankRecord.java index 8d461fe975..a57628750d 100644 --- a/src/java/org/apache/poi/hssf/record/MulBlankRecord.java +++ b/src/java/org/apache/poi/hssf/record/MulBlankRecord.java @@ -1,4 +1,3 @@ - /* ==================================================================== Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with @@ -15,50 +14,33 @@ See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ - -/* - * MulBlankRecord.java - * - * Created on December 10, 2001, 12:49 PM - */ package org.apache.poi.hssf.record; /** - * Title: Mulitple Blank cell record

+ * Title: Multiple Blank cell record(0x00BE)

* Description: Represents a set of columns in a row with no value but with styling. * In this release we have read-only support for this record type. - * The RecordFactory converts this to a set of BlankRecord objects.

- * REFERENCE: PG 329 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)

+ * The RecordFactory converts this to a set of BlankRecord objects.

+ * REFERENCE: PG 329 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)

* @author Andrew C. Oliver (acoliver at apache dot org) * @author Glen Stampoultzis (glens at apache.org) - * @version 2.0-pre - * @see org.apache.poi.hssf.record.BlankRecord + * @see BlankRecord */ - -public class MulBlankRecord - extends Record -{ - public final static short sid = 0xbe; - //private short field_1_row; - private int field_1_row; +public final class MulBlankRecord extends Record { + public final static short sid = 0x00BE; + + private int field_1_row; private short field_2_first_col; private short[] field_3_xfs; private short field_4_last_col; - /** Creates new MulBlankRecord */ - - public MulBlankRecord() - { - } /** * get the row number of the cells this represents * * @return row number */ - - //public short getRow() public int getRow() { return field_1_row; @@ -68,7 +50,6 @@ public class MulBlankRecord * starting column (first cell this holds in the row) * @return first column number */ - public short getFirstColumn() { return field_2_first_col; @@ -78,7 +59,6 @@ public class MulBlankRecord * ending column (last cell this holds in the row) * @return first column number */ - public short getLastColumn() { return field_4_last_col; @@ -88,7 +68,6 @@ public class MulBlankRecord * get the number of columns this contains (last-first +1) * @return number of columns (last - first +1) */ - public int getNumColumns() { return field_4_last_col - field_2_first_col + 1; @@ -99,7 +78,6 @@ public class MulBlankRecord * @param coffset the column (coffset = column - field_2_first_col) * @return the XF index for the column */ - public short getXFAt(int coffset) { return field_3_xfs[ coffset ]; @@ -108,16 +86,14 @@ public class MulBlankRecord /** * @param in the RecordInputstream to read the record from */ - public MulBlankRecord(RecordInputStream in) - { - //field_1_row = LittleEndian.getShort(data, 0 + offset); + public MulBlankRecord(RecordInputStream in) { field_1_row = in.readUShort(); field_2_first_col = in.readShort(); field_3_xfs = parseXFs(in); field_4_last_col = in.readShort(); } - private short [] parseXFs(RecordInputStream in) + private static short [] parseXFs(RecordInputStream in) { short[] retval = new short[ (in.remaining() - 2) / 2 ]; @@ -128,21 +104,16 @@ public class MulBlankRecord return retval; } - public String toString() - { + public String toString() { StringBuffer buffer = new StringBuffer(); buffer.append("[MULBLANK]\n"); - buffer.append("row = ") - .append(Integer.toHexString(getRow())).append("\n"); - buffer.append("firstcol = ") - .append(Integer.toHexString(getFirstColumn())).append("\n"); - buffer.append(" lastcol = ") - .append(Integer.toHexString(getLastColumn())).append("\n"); - for (int k = 0; k < getNumColumns(); k++) - { - buffer.append("xf").append(k).append(" = ") - .append(Integer.toHexString(getXFAt(k))).append("\n"); + buffer.append("row = ").append(Integer.toHexString(getRow())).append("\n"); + buffer.append("firstcol = ").append(Integer.toHexString(getFirstColumn())).append("\n"); + buffer.append(" lastcol = ").append(Integer.toHexString(getLastColumn())).append("\n"); + for (int k = 0; k < getNumColumns(); k++) { + buffer.append("xf").append(k).append(" = ").append( + Integer.toHexString(getXFAt(k))).append("\n"); } buffer.append("[/MULBLANK]\n"); return buffer.toString(); @@ -153,9 +124,10 @@ public class MulBlankRecord return sid; } - public int serialize(int offset, byte [] data) - { - throw new RecordFormatException( - "Sorry, you can't serialize a MulBlank in this release"); + public int serialize(int offset, byte [] data) { + throw new RecordFormatException( "Sorry, you can't serialize MulBlank in this release"); + } + public int getRecordSize() { + throw new RecordFormatException( "Sorry, you can't serialize MulBlank in this release"); } } diff --git a/src/java/org/apache/poi/hssf/record/MulRKRecord.java b/src/java/org/apache/poi/hssf/record/MulRKRecord.java index 0419f4921d..c6d002ddc8 100644 --- a/src/java/org/apache/poi/hssf/record/MulRKRecord.java +++ b/src/java/org/apache/poi/hssf/record/MulRKRecord.java @@ -113,10 +113,11 @@ public final class MulRKRecord extends Record { return sid; } - public int serialize(int offset, byte [] data) - { - throw new RecordFormatException( - "Sorry, you can't serialize a MulRK in this release"); + public int serialize(int offset, byte [] data) { + throw new RecordFormatException( "Sorry, you can't serialize MulRK in this release"); + } + public int getRecordSize() { + throw new RecordFormatException( "Sorry, you can't serialize MulRK in this release"); } private static final class RkRec { diff --git a/src/java/org/apache/poi/hssf/record/RKRecord.java b/src/java/org/apache/poi/hssf/record/RKRecord.java index c4b25a5bea..af2832b9d4 100644 --- a/src/java/org/apache/poi/hssf/record/RKRecord.java +++ b/src/java/org/apache/poi/hssf/record/RKRecord.java @@ -134,48 +134,12 @@ public final class RKRecord extends Record implements CellValueRecordInterface { return sb.toString(); } -// temporarily just constructs a new number record and returns its value - public int serialize(int offset, byte [] data) - { - NumberRecord rec = new NumberRecord(); - - rec.setColumn(getColumn()); - rec.setRow(getRow()); - rec.setValue(getRKNumber()); - rec.setXFIndex(getXFIndex()); - return rec.serialize(offset, data); - } - - /** - * Debugging main() - *

- * Normally I'd do this in a junit test, but let's face it -- once - * this algorithm has been tested and it works, we are never ever - * going to change it. This is driven by the Faceless Enemy's - * minions, who dare not change the algorithm out from under us. - * - * @param ignored_args command line arguments, which we blithely - * ignore - */ - - public static void main(String ignored_args[]) - { - int[] values = - { - 0x3FF00000, 0x405EC001, 0x02F1853A, 0x02F1853B, 0xFCDD699A - }; - double[] rvalues = - { - 1, 1.23, 12345678, 123456.78, -13149594 - }; - - for (int j = 0; j < values.length; j++) - { - System.out.println("input = " + Integer.toHexString(values[ j ]) - + " -> " + rvalues[ j ] + ": " - + RKUtil.decodeNumber(values[ j ])); - } - } + public int serialize(int offset, byte [] data) { + throw new RecordFormatException( "Sorry, you can't serialize RK in this release"); + } + public int getRecordSize() { + throw new RecordFormatException( "Sorry, you can't serialize RK in this release"); + } public short getSid() { diff --git a/src/java/org/apache/poi/hssf/record/Record.java b/src/java/org/apache/poi/hssf/record/Record.java index 29c37d7047..310cb28b2a 100644 --- a/src/java/org/apache/poi/hssf/record/Record.java +++ b/src/java/org/apache/poi/hssf/record/Record.java @@ -28,7 +28,6 @@ import java.io.ByteArrayInputStream; * @author Andrew C. Oliver * @author Marc Johnson (mjohnson at apache dot org) * @author Jason Height (jheight at chariot dot net dot au) - * @version 2.0-pre */ public abstract class Record extends RecordBase { @@ -48,27 +47,13 @@ public abstract class Record extends RecordBase { * @return byte array containing instance data */ - public byte [] serialize() - { + public final byte[] serialize() { byte[] retval = new byte[ getRecordSize() ]; serialize(0, retval); return retval; } - - /** - * gives the current serialized size of the record. Should include the sid and reclength (4 bytes). - */ - - public int getRecordSize() - { - - // this is kind od a stupid way to do it but for now we just serialize - // the record and return the size of the byte array - return serialize().length; - } - /** * get a string representation of the record (for biffview/debugging) */ diff --git a/src/testcases/org/apache/poi/hssf/util/TestRKUtil.java b/src/testcases/org/apache/poi/hssf/util/TestRKUtil.java index e158aafc5f..5b845775fe 100644 --- a/src/testcases/org/apache/poi/hssf/util/TestRKUtil.java +++ b/src/testcases/org/apache/poi/hssf/util/TestRKUtil.java @@ -1,4 +1,3 @@ - /* ==================================================================== Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with @@ -15,31 +14,38 @@ See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ - package org.apache.poi.hssf.util; +import junit.framework.AssertionFailedError; import junit.framework.TestCase; /** - * Tests the RKUtil class. + * Tests the {@link RKUtil} class. */ -public class TestRKUtil - extends TestCase -{ - public TestRKUtil(String s) - { - super(s); - } - - /** - * Check we can decode correctly. - */ - public void testDecode() - throws Exception - { - assertEquals(3.0, RKUtil.decodeNumber(1074266112), 0.0000001); - assertEquals(3.3, RKUtil.decodeNumber(1081384961), 0.0000001); - assertEquals(3.33, RKUtil.decodeNumber(1081397249), 0.0000001); - } +public final class TestRKUtil extends TestCase { + + /** + * Check we can decode correctly. + */ + public void testDecode() { + + int[] values = { 1074266112, 1081384961, 1081397249, + 0x3FF00000, 0x405EC001, 0x02F1853A, 0x02F1853B, 0xFCDD699A, + }; + double[] rvalues = { 3.0, 3.3, 3.33, + 1, 1.23, 12345678, 123456.78, -13149594, + }; + + for (int j = 0; j < values.length; j++) { + + int intBits = values[j]; + double expectedValue = rvalues[j]; + double actualValue = RKUtil.decodeNumber(intBits); + if (expectedValue != actualValue) { + throw new AssertionFailedError("0x" + Integer.toHexString(intBits) + + " should decode to " + expectedValue + " but got " + actualValue); + } + } + } }