[bug-65452] fix issue where WorkbookFactory.create(File, ...) returns null if file type not recognised

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1894097 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2021-10-10 09:58:02 +00:00
parent 69b2f31374
commit 2514e4d0ab
3 changed files with 26 additions and 8 deletions

View File

@ -3623,4 +3623,25 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
} }
} }
} }
@Test
void testBug65452() throws IOException {
File file = XSSFTestDataSamples.getSampleFile("workbook.xml");
try (FileInputStream fis = new FileInputStream(file)) {
try {
Workbook wb = WorkbookFactory.create(fis);
if (wb != null) wb.close();
fail("WorkbookFactory.create should have failed");
} catch (IOException ie) {
assertEquals("Can't open workbook - unsupported file type: XML", ie.getMessage());
}
}
try {
Workbook wb = WorkbookFactory.create(file);
if (wb != null) wb.close();
fail("WorkbookFactory.create should have failed");
} catch (IOException ie) {
assertEquals("Can't open workbook - unsupported file type: XML", ie.getMessage());
}
}
} }

View File

@ -19,11 +19,7 @@ package org.apache.poi.ss.usermodel;
import static org.apache.poi.extractor.ExtractorFactory.OOXML_PACKAGE; import static org.apache.poi.extractor.ExtractorFactory.OOXML_PACKAGE;
import static org.apache.poi.poifs.crypt.Decryptor.DEFAULT_POIFS_ENTRY; import static org.apache.poi.poifs.crypt.Decryptor.DEFAULT_POIFS_ENTRY;
import java.io.BufferedInputStream; import java.io.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.ServiceLoader; import java.util.ServiceLoader;
@ -291,9 +287,9 @@ public final class WorkbookFactory {
ooxmlEnc = root.hasEntry(DEFAULT_POIFS_ENTRY) || root.hasEntry(OOXML_PACKAGE); ooxmlEnc = root.hasEntry(DEFAULT_POIFS_ENTRY) || root.hasEntry(OOXML_PACKAGE);
} }
return wp(ooxmlEnc ? FileMagic.OOXML : fm, w -> w.create(file, password, readOnly)); return wp(ooxmlEnc ? FileMagic.OOXML : fm, w -> w.create(file, password, readOnly));
} else {
throw new IOException("Can't open workbook - unsupported file type: "+fm);
} }
return null;
} }

View File

@ -0,0 +1 @@
<?xml version="1.0"?><Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel"><TEST_REPORT><Author>EXAMPLE</Author></TEST_REPORT><Styles><Style ss:ID="Header"><Font ss:Bold="1"/><Interior ss:Color="#92D050" ss:Pattern="Solid"/><Alignment ss:Vertical="Center" ss:WrapText="1"/><Borders><Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/><Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/><Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/><Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/></Borders></Style><Style ss:ID="Data"><Borders><Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/><Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/><Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/><Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/></Borders></Style><Style ss:ID="Data1"><Alignment ss:Horizontal="Right"/><NumberFormat ss:Format="Standard"/><Borders><Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/><Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/><Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/><Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/></Borders></Style></Styles><Worksheet ss:Name="Sheet1"><Table x:FullColumns="1" x:FullRows="1"><Column ss:Width="120"/><Column ss:Width="120"/><Column ss:Width="120"/><Row/><Row ss:AutoFitHeight="1"><Cell ss:StyleID="Header"><Data ss:Type="String">Konto</Data></Cell><Cell ss:StyleID="Header"><Data ss:Type="String">Zuordnung</Data></Cell><Cell ss:StyleID="Header"><Data ss:Type="String">Nummer</Data></Cell></Row><Row><Cell ss:StyleID="Data"/><Cell ss:StyleID="Data"/><Cell ss:StyleID="Data"/><Cell ss:StyleID="Data"/><Cell ss:StyleID="Data"/><Cell ss:StyleID="Data"/></Row><Row><Cell ss:StyleID="Data"><Data ss:Type="String">KONTO0001</Data></Cell><Cell ss:StyleID="Data"><Data ss:Type="String">123456789</Data></Cell><Cell ss:StyleID="Data"><Data ss:Type="String">PROJEKT 1</Data></Cell></Row><Row><Cell ss:StyleID="Data"><Data ss:Type="String">KONTO0001</Data></Cell><Cell ss:StyleID="Data"><Data ss:Type="String">123456789</Data></Cell><Cell ss:StyleID="Data"><Data ss:Type="String"/></Cell></Row></Table></Worksheet></Workbook>