From 41ba655c7907945a9316fcfa7a1aea7e04b33980 Mon Sep 17 00:00:00 2001 From: Josh Micich Date: Thu, 21 May 2009 21:32:54 +0000 Subject: [PATCH] Added debug methods to decode BIFF record types from record IDs git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@777270 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hssf/record/RecordFactory.java | 59 +++++++++---------- .../apache/poi/hssf/record/UnknownRecord.java | 4 +- 2 files changed, 30 insertions(+), 33 deletions(-) diff --git a/src/java/org/apache/poi/hssf/record/RecordFactory.java b/src/java/org/apache/poi/hssf/record/RecordFactory.java index 48519162f9..84a8883e78 100644 --- a/src/java/org/apache/poi/hssf/record/RecordFactory.java +++ b/src/java/org/apache/poi/hssf/record/RecordFactory.java @@ -17,7 +17,6 @@ package org.apache.poi.hssf.record; -import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; @@ -33,7 +32,6 @@ import java.util.Set; import org.apache.poi.hssf.record.chart.*; import org.apache.poi.hssf.record.pivottable.*; -import org.apache.poi.util.HexDump; /** * Title: Record Factory

@@ -51,7 +49,7 @@ public final class RecordFactory { private interface I_RecordCreator { Record create(RecordInputStream in); - String getRecordClassName(); + Class getRecordClass(); } private static final class ReflectionRecordCreator implements I_RecordCreator { @@ -73,8 +71,8 @@ public final class RecordFactory { throw new RecordFormatException("Unable to construct record instance" , e.getTargetException()); } } - public String getRecordClassName() { - return _c.getDeclaringClass().getName(); + public Class getRecordClass() { + return _c.getDeclaringClass(); } } @@ -222,10 +220,25 @@ public final class RecordFactory { /** * cache of the recordsToMap(); */ - private static Map recordsMap = recordsToMap(recordClasses); + private static final Map _recordCreatorsById = recordsToMap(recordClasses); private static short[] _allKnownRecordSIDs; + /** + * Debug / diagnosis method
+ * Gets the POI implementation class for a given sid. Only a subset of the any BIFF + * records are actually interpreted by POI. A few others are known but not interpreted + * (see {@link UnknownRecord#getBiffName(int)}). + * @return the POI implementation class for the specified record sid. + * null if the specified record is not interpreted by POI. + */ + public static Class getRecordClass(int sid) { + I_RecordCreator rc = _recordCreatorsById.get(new Integer(sid)); + if (rc == null) { + return null; + } + return rc.getRecordClass(); + } /** * create a record, if there are MUL records than multiple records * are returned digested into the non-mul form. @@ -247,7 +260,7 @@ public final class RecordFactory { } static Record createSingleRecord(RecordInputStream in) { - I_RecordCreator constructor = recordsMap.get(new Short(in.getSid())); + I_RecordCreator constructor = _recordCreatorsById.get(new Integer(in.getSid())); if (constructor == null) { return new UnknownRecord(in); @@ -293,11 +306,11 @@ public final class RecordFactory { */ public static short[] getAllKnownRecordSIDs() { if (_allKnownRecordSIDs == null) { - short[] results = new short[ recordsMap.size() ]; + short[] results = new short[ _recordCreatorsById.size() ]; int i = 0; - for (Iterator iterator = recordsMap.keySet().iterator(); iterator.hasNext(); ) { - Short sid = iterator.next(); + for (Iterator iterator = _recordCreatorsById.keySet().iterator(); iterator.hasNext(); ) { + Integer sid = iterator.next(); results[i++] = sid.shortValue(); } @@ -313,8 +326,8 @@ public final class RecordFactory { * @return map of SIDs to short,short,byte[] constructors for Record classes * most of org.apache.poi.hssf.record.* */ - private static Map recordsToMap(Class [] records) { - Map result = new HashMap(); + private static Map recordsToMap(Class [] records) { + Map result = new HashMap(); Set> uniqueRecClasses = new HashSet>(records.length * 3 / 2); for (int i = 0; i < records.length; i++) { @@ -339,33 +352,17 @@ public final class RecordFactory { throw new RecordFormatException( "Unable to determine record types"); } - Short key = new Short(sid); + Integer key = new Integer(sid); if (result.containsKey(key)) { - String prevClassName = result.get(key).getRecordClassName(); + Class prevClass = result.get(key).getRecordClass(); throw new RuntimeException("duplicate record sid 0x" + Integer.toHexString(sid).toUpperCase() - + " for classes (" + recClass.getName() + ") and (" + prevClassName + ")"); + + " for classes (" + recClass.getName() + ") and (" + prevClass.getName() + ")"); } result.put(key, new ReflectionRecordCreator(constructor)); } return result; } - private static void checkZeros(InputStream in, int avail) throws IOException { - int count=0; - while(true) { - int b = in.read(); - if (b < 0) { - break; - } - if (b!=0) { - System.err.print(HexDump.byteToHex(b)); - } - count++; - } - if (avail != count) { - System.err.println("avail!=count (" + avail + "!=" + count + ")."); - } - } /** * Create an array of records from an input stream * diff --git a/src/java/org/apache/poi/hssf/record/UnknownRecord.java b/src/java/org/apache/poi/hssf/record/UnknownRecord.java index 5b6faf3158..b6a0d9040a 100644 --- a/src/java/org/apache/poi/hssf/record/UnknownRecord.java +++ b/src/java/org/apache/poi/hssf/record/UnknownRecord.java @@ -123,9 +123,9 @@ public final class UnknownRecord extends StandardRecord { /** * These BIFF record types are known but still uninterpreted by POI * - * @return the documented name of this BIFF record type + * @return the documented name of this BIFF record type, null if unknown to POI */ - private static String getBiffName(int sid) { + public static String getBiffName(int sid) { // Note to POI developers: // Make sure you delete the corresponding entry from // this method any time a new Record subclass is created. -- 2.39.5