From: Nick Burch Date: Fri, 26 Feb 2016 23:43:59 +0000 (+0000) Subject: Refactor out the POIFS directory entry name for Excel 1-95 entries, and have Extracto... X-Git-Tag: REL_3_14_FINAL~12 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=05d455310c2e24adf9db251a032cfa0f66e56809;p=poi.git Refactor out the POIFS directory entry name for Excel 1-95 entries, and have ExtractorFactory detect (but not support) these old files git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1732583 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java b/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java index 9dc89c8116..66c9d4a7c3 100644 --- a/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java +++ b/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java @@ -17,6 +17,9 @@ package org.apache.poi.hssf.extractor; +import static org.apache.poi.hssf.model.InternalWorkbook.WORKBOOK_DIR_ENTRY_NAMES; +import static org.apache.poi.hssf.model.InternalWorkbook.OLD_WORKBOOK_DIR_ENTRY_NAME; + import java.io.BufferedInputStream; import java.io.Closeable; import java.io.File; @@ -99,10 +102,10 @@ public class OldExcelExtractor implements Closeable { private void open(DirectoryNode directory) throws IOException { DocumentNode book; try { - book = (DocumentNode)directory.getEntry("Book"); + book = (DocumentNode)directory.getEntry(OLD_WORKBOOK_DIR_ENTRY_NAME); } catch (FileNotFoundException e) { // some files have "Workbook" instead - book = (DocumentNode)directory.getEntry("Workbook"); + book = (DocumentNode)directory.getEntry(WORKBOOK_DIR_ENTRY_NAMES[0]); } if (book == null) { diff --git a/src/java/org/apache/poi/hssf/model/InternalWorkbook.java b/src/java/org/apache/poi/hssf/model/InternalWorkbook.java index b932836cb6..0b11818fdf 100644 --- a/src/java/org/apache/poi/hssf/model/InternalWorkbook.java +++ b/src/java/org/apache/poi/hssf/model/InternalWorkbook.java @@ -37,6 +37,7 @@ import org.apache.poi.ddf.EscherRecord; import org.apache.poi.ddf.EscherSimpleProperty; import org.apache.poi.ddf.EscherSpRecord; import org.apache.poi.ddf.EscherSplitMenuColorsRecord; +import org.apache.poi.hssf.extractor.OldExcelExtractor; import org.apache.poi.hssf.record.BOFRecord; import org.apache.poi.hssf.record.BackupRecord; import org.apache.poi.hssf.record.BookBoolRecord; @@ -133,6 +134,12 @@ public final class InternalWorkbook { "WORKBOOK", // Typically from third party programs "BOOK", // Typically odd Crystal Reports exports }; + /** + * Name of older (pre-Excel 97) Workbook streams, which + * aren't supported by HSSFWorkbook, only by + * {@link OldExcelExtractor} + */ + public static final String OLD_WORKBOOK_DIR_ENTRY_NAME = "Book"; private static final POILogger log = POILogFactory.getLogger(InternalWorkbook.class); private static final int DEBUG = POILogger.DEBUG; diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index ec66d5d4a6..ca4e79a8e4 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -18,6 +18,7 @@ package org.apache.poi.hssf.usermodel; import static org.apache.poi.hssf.model.InternalWorkbook.WORKBOOK_DIR_ENTRY_NAMES; +import static org.apache.poi.hssf.model.InternalWorkbook.OLD_WORKBOOK_DIR_ENTRY_NAME; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -267,7 +268,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss // check for previous version of file format try { - directory.getEntry("Book"); + directory.getEntry(OLD_WORKBOOK_DIR_ENTRY_NAME); throw new OldExcelFormatException("The supplied spreadsheet seems to be Excel 5.0/7.0 (BIFF5) format. " + "POI only supports BIFF8 format (from Excel versions 97/2000/XP/2003)"); } catch (FileNotFoundException e) { diff --git a/src/ooxml/java/org/apache/poi/extractor/ExtractorFactory.java b/src/ooxml/java/org/apache/poi/extractor/ExtractorFactory.java index 73efc94d38..e88eaaff82 100644 --- a/src/ooxml/java/org/apache/poi/extractor/ExtractorFactory.java +++ b/src/ooxml/java/org/apache/poi/extractor/ExtractorFactory.java @@ -16,6 +16,9 @@ ==================================================================== */ package org.apache.poi.extractor; +import static org.apache.poi.hssf.model.InternalWorkbook.OLD_WORKBOOK_DIR_ENTRY_NAME; +import static org.apache.poi.hssf.model.InternalWorkbook.WORKBOOK_DIR_ENTRY_NAMES; + import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileNotFoundException; @@ -37,6 +40,7 @@ import org.apache.poi.hsmf.datatypes.AttachmentChunks; import org.apache.poi.hsmf.extractor.OutlookTextExtactor; import org.apache.poi.hssf.extractor.EventBasedExcelExtractor; import org.apache.poi.hssf.extractor.ExcelExtractor; +import org.apache.poi.hssf.extractor.OldExcelExtractor; import org.apache.poi.hwpf.OldWordFileFormatException; import org.apache.poi.hwpf.extractor.Word6Extractor; import org.apache.poi.hwpf.extractor.WordExtractor; @@ -66,8 +70,6 @@ import org.apache.poi.xwpf.extractor.XWPFWordExtractor; import org.apache.poi.xwpf.usermodel.XWPFRelation; import org.apache.xmlbeans.XmlException; -import static org.apache.poi.hssf.model.InternalWorkbook.WORKBOOK_DIR_ENTRY_NAMES; - /** * Figures out the correct POITextExtractor for your supplied * document, and returns it. @@ -311,6 +313,9 @@ public class ExtractorFactory { return new ExcelExtractor(poifsDir); } } + if (poifsDir.hasEntry(OLD_WORKBOOK_DIR_ENTRY_NAME)) { + throw new IllegalArgumentException("Excel 1-95 file found, call OldExcelExtractor directly"); + } if (poifsDir.hasEntry("WordDocument")) { // Old or new style word document?