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
This commit is contained in:
Nick Burch 2016-02-26 23:43:59 +00:00
parent 1cbcfbe026
commit 856261f4d0
4 changed files with 21 additions and 5 deletions

View File

@ -17,6 +17,9 @@
package org.apache.poi.hssf.extractor; 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.BufferedInputStream;
import java.io.Closeable; import java.io.Closeable;
import java.io.File; import java.io.File;
@ -99,10 +102,10 @@ public class OldExcelExtractor implements Closeable {
private void open(DirectoryNode directory) throws IOException { private void open(DirectoryNode directory) throws IOException {
DocumentNode book; DocumentNode book;
try { try {
book = (DocumentNode)directory.getEntry("Book"); book = (DocumentNode)directory.getEntry(OLD_WORKBOOK_DIR_ENTRY_NAME);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
// some files have "Workbook" instead // some files have "Workbook" instead
book = (DocumentNode)directory.getEntry("Workbook"); book = (DocumentNode)directory.getEntry(WORKBOOK_DIR_ENTRY_NAMES[0]);
} }
if (book == null) { if (book == null) {

View File

@ -37,6 +37,7 @@ import org.apache.poi.ddf.EscherRecord;
import org.apache.poi.ddf.EscherSimpleProperty; import org.apache.poi.ddf.EscherSimpleProperty;
import org.apache.poi.ddf.EscherSpRecord; import org.apache.poi.ddf.EscherSpRecord;
import org.apache.poi.ddf.EscherSplitMenuColorsRecord; 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.BOFRecord;
import org.apache.poi.hssf.record.BackupRecord; import org.apache.poi.hssf.record.BackupRecord;
import org.apache.poi.hssf.record.BookBoolRecord; import org.apache.poi.hssf.record.BookBoolRecord;
@ -133,6 +134,12 @@ public final class InternalWorkbook {
"WORKBOOK", // Typically from third party programs "WORKBOOK", // Typically from third party programs
"BOOK", // Typically odd Crystal Reports exports "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 POILogger log = POILogFactory.getLogger(InternalWorkbook.class);
private static final int DEBUG = POILogger.DEBUG; private static final int DEBUG = POILogger.DEBUG;

View File

@ -18,6 +18,7 @@
package org.apache.poi.hssf.usermodel; 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.WORKBOOK_DIR_ENTRY_NAMES;
import static org.apache.poi.hssf.model.InternalWorkbook.OLD_WORKBOOK_DIR_ENTRY_NAME;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; 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 // check for previous version of file format
try { 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. " 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)"); + "POI only supports BIFF8 format (from Excel versions 97/2000/XP/2003)");
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {

View File

@ -16,6 +16,9 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.extractor; 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.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; 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.hsmf.extractor.OutlookTextExtactor;
import org.apache.poi.hssf.extractor.EventBasedExcelExtractor; import org.apache.poi.hssf.extractor.EventBasedExcelExtractor;
import org.apache.poi.hssf.extractor.ExcelExtractor; 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.OldWordFileFormatException;
import org.apache.poi.hwpf.extractor.Word6Extractor; import org.apache.poi.hwpf.extractor.Word6Extractor;
import org.apache.poi.hwpf.extractor.WordExtractor; 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.poi.xwpf.usermodel.XWPFRelation;
import org.apache.xmlbeans.XmlException; 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 * Figures out the correct POITextExtractor for your supplied
* document, and returns it. * document, and returns it.
@ -311,6 +313,9 @@ public class ExtractorFactory {
return new ExcelExtractor(poifsDir); 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")) { if (poifsDir.hasEntry("WordDocument")) {
// Old or new style word document? // Old or new style word document?