From c8c2d0139e6848ec1061754a6e212ffb6d87483d Mon Sep 17 00:00:00 2001 From: Josh Micich Date: Fri, 23 May 2008 05:28:54 +0000 Subject: Fix for bug 45046 - allowed DEFINEDNAME records without EXTERNALBOOK records git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@659429 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/poi/hssf/model/LinkTable.java | 34 +++++++++++++---------- src/java/org/apache/poi/hssf/model/Workbook.java | 3 +- 2 files changed, 20 insertions(+), 17 deletions(-) (limited to 'src/java') diff --git a/src/java/org/apache/poi/hssf/model/LinkTable.java b/src/java/org/apache/poi/hssf/model/LinkTable.java index 88c94a61e7..a19971b7d5 100755 --- a/src/java/org/apache/poi/hssf/model/LinkTable.java +++ b/src/java/org/apache/poi/hssf/model/LinkTable.java @@ -41,7 +41,7 @@ import org.apache.poi.hssf.record.SupBookRecord; * * In BIFF8 the Link Table consists of * * @@ -63,6 +63,7 @@ import org.apache.poi.hssf.record.SupBookRecord; * @author Josh Micich */ final class LinkTable { + // TODO make this class into a record aggregate private static final class CRNBlock { @@ -79,8 +80,8 @@ final class LinkTable { _crns = crns; } public CRNRecord[] getCrns() { - return (CRNRecord[]) _crns.clone(); - } + return (CRNRecord[]) _crns.clone(); + } } private static final class ExternalBookBlock { @@ -136,16 +137,19 @@ final class LinkTable { while(rs.peekNextClass() == SupBookRecord.class) { temp.add(new ExternalBookBlock(rs)); } - if(temp.size() < 1) { - throw new RuntimeException("Need at least one EXTERNALBOOK blocks"); - } + _externalBookBlocks = new ExternalBookBlock[temp.size()]; temp.toArray(_externalBookBlocks); temp.clear(); - - // If link table is present, there is always 1 of ExternSheetRecord - Record next = rs.getNext(); - _externSheetRecord = (ExternSheetRecord)next; + + if (_externalBookBlocks.length > 0) { + // If any ExternalBookBlock present, there is always 1 of ExternSheetRecord + Record next = rs.getNext(); + _externSheetRecord = (ExternSheetRecord) next; + } else { + _externSheetRecord = null; + } + _definedNames = new ArrayList(); // collect zero or more DEFINEDNAMEs id=0x18 while(rs.peekNextClass() == NameRecord.class) { @@ -222,7 +226,7 @@ final class LinkTable { public void addName(NameRecord name) { _definedNames.add(name); - // TODO - this is messy + // TODO - this is messy // Not the most efficient way but the other way was causing too many bugs int idx = findFirstRecordLocBySid(ExternSheetRecord.sid); if (idx == -1) idx = findFirstRecordLocBySid(SupBookRecord.sid); @@ -242,8 +246,8 @@ final class LinkTable { public int getSheetIndexFromExternSheetIndex(int externSheetNumber) { if (externSheetNumber >= _externSheetRecord.getNumOfREFStructures()) { - return -1; - } + return -1; + } return _externSheetRecord.getREFRecordAt(externSheetNumber).getIndexToFirstSupBook(); } @@ -265,7 +269,7 @@ final class LinkTable { ExternSheetSubRecord esr = _externSheetRecord.getREFRecordAt(i); if (esr.getIndexToFirstSupBook() == sheetNumber - && esr.getIndexToLastSupBook() == sheetNumber){ + && esr.getIndexToLastSupBook() == sheetNumber){ return i; } } diff --git a/src/java/org/apache/poi/hssf/model/Workbook.java b/src/java/org/apache/poi/hssf/model/Workbook.java index 08f2263182..d87fc2c632 100644 --- a/src/java/org/apache/poi/hssf/model/Workbook.java +++ b/src/java/org/apache/poi/hssf/model/Workbook.java @@ -191,12 +191,11 @@ public class Workbook implements Model case ExternSheetRecord.sid : throw new RuntimeException("Extern sheet is part of LinkTable"); case NameRecord.sid : - throw new RuntimeException("DEFINEDNAME is part of LinkTable"); case SupBookRecord.sid : + // LinkTable can start with either of these if (log.check( POILogger.DEBUG )) log.log(DEBUG, "found SupBook record at " + k); retval.linkTable = new LinkTable(recs, k, retval.records); - // retval.records.supbookpos = k; k+=retval.linkTable.getRecordCount() - 1; continue; case FormatRecord.sid : -- cgit v1.2.3