This avoids confusion if people look at this code in the future without knowledge about the historical transition from java.io to java.nio git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1855873 13f79535-47bb-0310-9956-ffa450edef68tags/REL_4_1_0
/** | /** | ||||
* Writes out the updated standard Document Information Properties (HPSF) | * Writes out the updated standard Document Information Properties (HPSF) | ||||
* into the currently open NPOIFSFileSystem | |||||
* into the currently open POIFSFileSystem | |||||
* | * | ||||
* @throws IOException if an error when writing to the open | * @throws IOException if an error when writing to the open | ||||
* {@link POIFSFileSystem} occurs | * {@link POIFSFileSystem} occurs | ||||
} | } | ||||
/** | /** | ||||
* Writes out the standard Document Information Properties (HPSF) | * Writes out the standard Document Information Properties (HPSF) | ||||
* @param outFS the NPOIFSFileSystem to write the properties into | |||||
* @param outFS the {@link POIFSFileSystem} to write the properties into | |||||
* @param writtenEntries a list of POIFS entries to add the property names too | * @param writtenEntries a list of POIFS entries to add the property names too | ||||
* | * | ||||
* @throws IOException if an error when writing to the | * @throws IOException if an error when writing to the | ||||
* | * | ||||
* @param name the (POIFS Level) name of the property to write | * @param name the (POIFS Level) name of the property to write | ||||
* @param set the PropertySet to write out | * @param set the PropertySet to write out | ||||
* @param outFS the NPOIFSFileSystem to write the property into | |||||
* @param outFS the {@link POIFSFileSystem} to write the property into | |||||
* | * | ||||
* @throws IOException if an error when writing to the | * @throws IOException if an error when writing to the | ||||
* {@link POIFSFileSystem} occurs | * {@link POIFSFileSystem} occurs |
* @see #HSSFWorkbook(POIFSFileSystem) | * @see #HSSFWorkbook(POIFSFileSystem) | ||||
* @exception IOException if the stream cannot be read | * @exception IOException if the stream cannot be read | ||||
*/ | */ | ||||
@SuppressWarnings("resource") // NPOIFSFileSystem always closes the stream | |||||
@SuppressWarnings("resource") // POIFSFileSystem always closes the stream | |||||
public HSSFWorkbook(InputStream s, boolean preserveNodes) | public HSSFWorkbook(InputStream s, boolean preserveNodes) | ||||
throws IOException | throws IOException | ||||
{ | { |
} | } | ||||
/** | /** | ||||
* Creates a HSSFWorkbook from the given NPOIFSFileSystem<p> | |||||
* Creates a HSSFWorkbook from the given {@link POIFSFileSystem}<p> | |||||
* Note that in order to properly release resources the | * Note that in order to properly release resources the | ||||
* Workbook should be closed after use. | * Workbook should be closed after use. | ||||
*/ | */ |
} | } | ||||
/** | /** | ||||
* Read from a NPOIFSFileSystem and process the documents we get | |||||
* Read from a {@link POIFSFileSystem} and process the documents we get | |||||
* | * | ||||
* @param poifs the NPOIFSFileSystem from which to read the data | |||||
* @param poifs the POIFSFileSystem from which to read the data | |||||
* | * | ||||
* @exception IOException on errors reading, or on invalid data | * @exception IOException on errors reading, or on invalid data | ||||
*/ | */ |
// Our list of entries, kept sorted to preserve order | // Our list of entries, kept sorted to preserve order | ||||
private final ArrayList<Entry> _entries = new ArrayList<>(); | private final ArrayList<Entry> _entries = new ArrayList<>(); | ||||
// the NPOIFSFileSytem we belong to | |||||
private final POIFSFileSystem _nfilesystem; | |||||
// the POIFSFileSystem we belong to | |||||
private final POIFSFileSystem _filesystem; | |||||
// the path described by this document | // the path described by this document | ||||
private final POIFSDocumentPath _path; | private final POIFSDocumentPath _path; | ||||
* is intended strictly for the internal use of this package | * is intended strictly for the internal use of this package | ||||
* | * | ||||
* @param property the DirectoryProperty for this DirectoryEntry | * @param property the DirectoryProperty for this DirectoryEntry | ||||
* @param nfilesystem the NPOIFSFileSystem we belong to | |||||
* @param filesystem the {@link POIFSFileSystem} we belong to | |||||
* @param parent the parent of this entry | * @param parent the parent of this entry | ||||
*/ | */ | ||||
DirectoryNode(final DirectoryProperty property, | DirectoryNode(final DirectoryProperty property, | ||||
final POIFSFileSystem nfilesystem, | |||||
final POIFSFileSystem filesystem, | |||||
final DirectoryNode parent) | final DirectoryNode parent) | ||||
{ | { | ||||
super(property, parent); | super(property, parent); | ||||
this._nfilesystem = nfilesystem; | |||||
this._filesystem = filesystem; | |||||
if (parent == null) | if (parent == null) | ||||
{ | { | ||||
if (child.isDirectory()) | if (child.isDirectory()) | ||||
{ | { | ||||
DirectoryProperty childDir = (DirectoryProperty) child; | DirectoryProperty childDir = (DirectoryProperty) child; | ||||
childNode = new DirectoryNode(childDir, _nfilesystem, this); | |||||
childNode = new DirectoryNode(childDir, _filesystem, this); | |||||
} | } | ||||
else | else | ||||
{ | { | ||||
*/ | */ | ||||
public POIFSFileSystem getFileSystem() | public POIFSFileSystem getFileSystem() | ||||
{ | { | ||||
return _nfilesystem; | |||||
return _filesystem; | |||||
} | } | ||||
/** | /** | ||||
DocumentNode rval = new DocumentNode(property, this); | DocumentNode rval = new DocumentNode(property, this); | ||||
(( DirectoryProperty ) getProperty()).addChild(property); | (( DirectoryProperty ) getProperty()).addChild(property); | ||||
_nfilesystem.addDocument(document); | |||||
_filesystem.addDocument(document); | |||||
_entries.add(rval); | _entries.add(rval); | ||||
_byname.put(property.getName(), rval); | _byname.put(property.getName(), rval); | ||||
_byname.remove(entry.getName()); | _byname.remove(entry.getName()); | ||||
try { | try { | ||||
_nfilesystem.remove(entry); | |||||
_filesystem.remove(entry); | |||||
} catch (IOException e) { | } catch (IOException e) { | ||||
// TODO Work out how to report this, given we can't change the method signature... | // TODO Work out how to report this, given we can't change the method signature... | ||||
throw new RuntimeException(e); | throw new RuntimeException(e); | ||||
final InputStream stream) | final InputStream stream) | ||||
throws IOException | throws IOException | ||||
{ | { | ||||
return createDocument(new POIFSDocument(name, _nfilesystem, stream)); | |||||
return createDocument(new POIFSDocument(name, _filesystem, stream)); | |||||
} | } | ||||
/** | /** | ||||
final POIFSWriterListener writer) | final POIFSWriterListener writer) | ||||
throws IOException | throws IOException | ||||
{ | { | ||||
return createDocument(new POIFSDocument(name, size, _nfilesystem, writer)); | |||||
return createDocument(new POIFSDocument(name, size, _filesystem, writer)); | |||||
} | } | ||||
/** | /** | ||||
{ | { | ||||
DirectoryProperty property = new DirectoryProperty(name); | DirectoryProperty property = new DirectoryProperty(name); | ||||
DirectoryNode rval = new DirectoryNode(property, _nfilesystem, this); | |||||
_nfilesystem.addDirectory(property); | |||||
DirectoryNode rval = new DirectoryNode(property, _filesystem, this); | |||||
_filesystem.addDirectory(property); | |||||
(( DirectoryProperty ) getProperty()).addChild(property); | (( DirectoryProperty ) getProperty()).addChild(property); | ||||
_entries.add(rval); | _entries.add(rval); |
} | } | ||||
/** | /** | ||||
* Wrap the OLE2 data in the NPOIFSFileSystem into a decrypted stream by using | |||||
* Wrap the OLE2 data in the {@link POIFSFileSystem} into a decrypted stream by using | |||||
* the given password. | * the given password. | ||||
* | * | ||||
* @param fs The OLE2 stream for the document | * @param fs The OLE2 stream for the document | ||||
*/ | */ | ||||
public static InputStream getDecryptedStream(final POIFSFileSystem fs, String password) | public static InputStream getDecryptedStream(final POIFSFileSystem fs, String password) | ||||
throws IOException { | throws IOException { | ||||
// wrap the stream in a FilterInputStream to close the NPOIFSFileSystem | |||||
// wrap the stream in a FilterInputStream to close the POIFSFileSystem | |||||
// as well when the resulting OPCPackage is closed | // as well when the resulting OPCPackage is closed | ||||
return new FilterInputStream(getDecryptedStream(fs.getRoot(), password)) { | return new FilterInputStream(getDecryptedStream(fs.getRoot(), password)) { | ||||
@Override | @Override |
while ((zipEntry = zis.getNextEntry()) != null) { | while ((zipEntry = zis.getNextEntry()) != null) { | ||||
if (endsWithIgnoreCase(zipEntry.getName(), VBA_PROJECT_OOXML)) { | if (endsWithIgnoreCase(zipEntry.getName(), VBA_PROJECT_OOXML)) { | ||||
try { | try { | ||||
// Make a NPOIFS from the contents, and close the stream | |||||
// Make a POIFSFileSystem from the contents, and close the stream | |||||
this.fs = new POIFSFileSystem(zis); | this.fs = new POIFSFileSystem(zis); | ||||
return; | return; | ||||
} catch (IOException e) { | } catch (IOException e) { |
public class SlideShowFactory { | public class SlideShowFactory { | ||||
/** | /** | ||||
* Creates a SlideShow from the given NPOIFSFileSystem. | |||||
* Creates a SlideShow from the given POIFSFileSystem. | |||||
* | * | ||||
* @param fs The {@link POIFSFileSystem} to read the document from | * @param fs The {@link POIFSFileSystem} to read the document from | ||||
* | * | ||||
} | } | ||||
/** | /** | ||||
* Creates a SlideShow from the given NPOIFSFileSystem, which may | |||||
* Creates a SlideShow from the given POIFSFileSystem, which may | |||||
* be password protected | * be password protected | ||||
* | * | ||||
* @param fs The {@link POIFSFileSystem} to read the document from | * @param fs The {@link POIFSFileSystem} to read the document from | ||||
} | } | ||||
/** | /** | ||||
* Creates a SlideShow from the given NPOIFSFileSystem. | |||||
* Creates a SlideShow from the given DirectoryNode. | |||||
* | * | ||||
* @param root The {@link DirectoryNode} to start reading the document from | * @param root The {@link DirectoryNode} to start reading the document from | ||||
* | * | ||||
/** | /** | ||||
* Creates a SlideShow from the given NPOIFSFileSystem, which may | |||||
* Creates a SlideShow from the given DirectoryNode, which may | |||||
* be password protected | * be password protected | ||||
* | * | ||||
* @param root The {@link DirectoryNode} to start reading the document from | * @param root The {@link DirectoryNode} to start reading the document from |
} | } | ||||
/** | /** | ||||
* Creates a HSSFWorkbook from the given NPOIFSFileSystem<p> | |||||
* Creates a HSSFWorkbook from the given POIFSFileSystem<p> | |||||
* | * | ||||
* Note that in order to properly release resources the | * Note that in order to properly release resources the | ||||
* Workbook should be closed after use. | * Workbook should be closed after use. | ||||
} | } | ||||
/** | /** | ||||
* Creates a Workbook from the given NPOIFSFileSystem, which may | |||||
* Creates a Workbook from the given POIFSFileSystem, which may | |||||
* be password protected | * be password protected | ||||
* | * | ||||
* @param fs The {@link POIFSFileSystem} to read the document from | * @param fs The {@link POIFSFileSystem} to read the document from | ||||
/** | /** | ||||
* Creates a Workbook from the given NPOIFSFileSystem. | |||||
* Creates a Workbook from the given DirectoryNode. | |||||
* | * | ||||
* @param root The {@link DirectoryNode} to start reading the document from | * @param root The {@link DirectoryNode} to start reading the document from | ||||
* | * | ||||
/** | /** | ||||
* Creates a Workbook from the given NPOIFSFileSystem, which may | |||||
* Creates a Workbook from the given DirectoryNode, which may | |||||
* be password protected | * be password protected | ||||
* | * | ||||
* @param root The {@link DirectoryNode} to start reading the document from | * @param root The {@link DirectoryNode} to start reading the document from |
} finally { | } finally { | ||||
IOUtils.closeQuietly(is); | IOUtils.closeQuietly(is); | ||||
// also close the NPOIFSFileSystem here as we read all the data | |||||
// also close the POIFSFileSystem here as we read all the data | |||||
// while decrypting | // while decrypting | ||||
fs.close(); | fs.close(); | ||||
} | } |
@Test | @Test | ||||
public void bug55692_stream() throws IOException { | public void bug55692_stream() throws IOException { | ||||
// Directly on a Stream, will go via NPOIFS and spot it's | |||||
// Directly on a Stream, will go via POIFS and spot it's | |||||
// actually a .xlsx file encrypted with the default password, and open | // actually a .xlsx file encrypted with the default password, and open | ||||
Workbook wb = WorkbookFactory.create( | Workbook wb = WorkbookFactory.create( | ||||
POIDataSamples.getPOIFSInstance().openResourceAsStream("protect.xlsx")); | POIDataSamples.getPOIFSInstance().openResourceAsStream("protect.xlsx")); | ||||
} | } | ||||
@Test | @Test | ||||
public void bug55692_npoifs() throws IOException { | |||||
// Via a NPOIFSFileSystem, will spot it's actually a .xlsx file | |||||
public void bug55692_poifs2() throws IOException { | |||||
// Via a POIFSFileSystem, will spot it's actually a .xlsx file | |||||
// encrypted with the default password, and open | // encrypted with the default password, and open | ||||
POIFSFileSystem fsNP = new POIFSFileSystem( | POIFSFileSystem fsNP = new POIFSFileSystem( | ||||
POIDataSamples.getPOIFSInstance().openResourceAsStream("protect.xlsx")); | POIDataSamples.getPOIFSInstance().openResourceAsStream("protect.xlsx")); |
/** | /** | ||||
* Creates a PowerPointExtractor, from a specific place | * Creates a PowerPointExtractor, from a specific place | ||||
* inside an open NPOIFSFileSystem | |||||
* inside an open {@link POIFSFileSystem} | |||||
* | * | ||||
* @param dir the POIFS Directory containing the PowerPoint document | * @param dir the POIFS Directory containing the PowerPoint document | ||||
*/ | */ |
* Constructs a Powerpoint document from an POIFSFileSystem. | * Constructs a Powerpoint document from an POIFSFileSystem. | ||||
*/ | */ | ||||
@SuppressWarnings("resource") | @SuppressWarnings("resource") | ||||
public HSLFSlideShow(POIFSFileSystem npoifs) throws IOException { | |||||
this(new HSLFSlideShowImpl(npoifs)); | |||||
public HSLFSlideShow(POIFSFileSystem poifs) throws IOException { | |||||
this(new HSLFSlideShowImpl(poifs)); | |||||
} | } | ||||
/** | /** |
@Internal | @Internal | ||||
public class HSLFSlideShowFactory extends SlideShowFactory { | public class HSLFSlideShowFactory extends SlideShowFactory { | ||||
/** | /** | ||||
* Creates a HSLFSlideShow from the given NPOIFSFileSystem<p> | |||||
* Creates a HSLFSlideShow from the given {@link POIFSFileSystem}<p> | |||||
* Note that in order to properly release resources the | * Note that in order to properly release resources the | ||||
* SlideShow should be closed after use. | * SlideShow should be closed after use. | ||||
*/ | */ |
// And with NPOIFS | // And with NPOIFS | ||||
sample = _samples.openResourceAsStream("Sample.pub"); | sample = _samples.openResourceAsStream("Sample.pub"); | ||||
POIFSFileSystem fs = new POIFSFileSystem(sample); | POIFSFileSystem fs = new POIFSFileSystem(sample); | ||||
HPBFDocument docNPOIFS = new HPBFDocument(fs); | |||||
ext = new PublisherTextExtractor(docNPOIFS); | |||||
HPBFDocument docPOIFS = new HPBFDocument(fs); | |||||
ext = new PublisherTextExtractor(docPOIFS); | |||||
assertEquals(SAMPLE_TEXT, ext.getText()); | assertEquals(SAMPLE_TEXT, ext.getText()); | ||||
ext.close(); | ext.close(); | ||||
docNPOIFS.close(); | |||||
docPOIFS.close(); | |||||
fs.close(); | fs.close(); | ||||
sample.close(); | sample.close(); | ||||
public void testDifferentPOIFS() throws IOException { | public void testDifferentPOIFS() throws IOException { | ||||
// Open the two filesystems | // Open the two filesystems | ||||
File pptFile = slTests.getFile("basic_test_ppt_file.ppt"); | File pptFile = slTests.getFile("basic_test_ppt_file.ppt"); | ||||
try (final POIFSFileSystem npoifs = new POIFSFileSystem(pptFile, true)) { | |||||
try (final POIFSFileSystem poifs = new POIFSFileSystem(pptFile, true)) { | |||||
// Open directly | // Open directly | ||||
try (SlideShow<?,?> ppt = SlideShowFactory.create(npoifs.getRoot()); | |||||
try (SlideShow<?,?> ppt = SlideShowFactory.create(poifs.getRoot()); | |||||
SlideShowExtractor<?,?> extractor = new SlideShowExtractor<>(ppt)) { | SlideShowExtractor<?,?> extractor = new SlideShowExtractor<>(ppt)) { | ||||
assertEquals(EXPECTED_PAGE1+EXPECTED_PAGE2, extractor.getText()); | assertEquals(EXPECTED_PAGE1+EXPECTED_PAGE2, extractor.getText()); | ||||
} | } |
//It isn't pretty, but it works... | //It isn't pretty, but it works... | ||||
private Map<String, String> getMacrosFromHSLF(String fileName) throws IOException { | private Map<String, String> getMacrosFromHSLF(String fileName) throws IOException { | ||||
InputStream is = null; | InputStream is = null; | ||||
POIFSFileSystem npoifs = null; | |||||
POIFSFileSystem poifs = null; | |||||
try { | try { | ||||
is = new FileInputStream(POIDataSamples.getSlideShowInstance().getFile(fileName)); | is = new FileInputStream(POIDataSamples.getSlideShowInstance().getFile(fileName)); | ||||
npoifs = new POIFSFileSystem(is); | |||||
//TODO: should we run the VBAMacroReader on this npoifs? | |||||
poifs = new POIFSFileSystem(is); | |||||
//TODO: should we run the VBAMacroReader on this poifs? | |||||
//TBD: We know that ppt typically don't store macros in the regular place, | //TBD: We know that ppt typically don't store macros in the regular place, | ||||
//but _can_ they? | //but _can_ they? | ||||
HSLFSlideShow ppt = new HSLFSlideShow(npoifs); | |||||
HSLFSlideShow ppt = new HSLFSlideShow(poifs); | |||||
//get macro persist id | //get macro persist id | ||||
DocInfoListContainer list = (DocInfoListContainer)ppt.getDocumentRecord().findFirstOfType(RecordTypes.List.typeID); | DocInfoListContainer list = (DocInfoListContainer)ppt.getDocumentRecord().findFirstOfType(RecordTypes.List.typeID); | ||||
ppt.close(); | ppt.close(); | ||||
} finally { | } finally { | ||||
IOUtils.closeQuietly(npoifs); | |||||
IOUtils.closeQuietly(poifs); | |||||
IOUtils.closeQuietly(is); | IOUtils.closeQuietly(is); | ||||
} | } | ||||
return null; | return null; |
public void testDifferentPOIFS() throws Exception { | public void testDifferentPOIFS() throws Exception { | ||||
// Open the two filesystems | // Open the two filesystems | ||||
File file = docTests.getFile("test2.doc"); | File file = docTests.getFile("test2.doc"); | ||||
try (POIFSFileSystem npoifs = new POIFSFileSystem(file, true)) { | |||||
try (POIFSFileSystem poifs = new POIFSFileSystem(file, true)) { | |||||
DirectoryNode dir = npoifs.getRoot(); | |||||
DirectoryNode dir = poifs.getRoot(); | |||||
// Open directly | // Open directly | ||||
@SuppressWarnings("resource") | @SuppressWarnings("resource") |
} | } | ||||
/** | /** | ||||
* [RESOLVED FIXED] Bug 51671 - HWPFDocument.write based on NPOIFSFileSystem | |||||
* [RESOLVED FIXED] Bug 51671 - HWPFDocument.write based on POIFSFileSystem | |||||
* throws a NullPointerException | * throws a NullPointerException | ||||
*/ | */ | ||||
@Test | @Test | ||||
{ | { | ||||
InputStream is = POIDataSamples.getDocumentInstance() | InputStream is = POIDataSamples.getDocumentInstance() | ||||
.openResourceAsStream("empty.doc"); | .openResourceAsStream("empty.doc"); | ||||
try (POIFSFileSystem npoifsFileSystem = new POIFSFileSystem(is)) { | |||||
try (POIFSFileSystem poifsFileSystem = new POIFSFileSystem(is)) { | |||||
HWPFDocument hwpfDocument = new HWPFDocument( | HWPFDocument hwpfDocument = new HWPFDocument( | ||||
npoifsFileSystem.getRoot()); | |||||
poifsFileSystem.getRoot()); | |||||
hwpfDocument.write(new ByteArrayOutputStream()); | hwpfDocument.write(new ByteArrayOutputStream()); | ||||
hwpfDocument.close(); | hwpfDocument.close(); | ||||
} | } |
} | } | ||||
@Test(expected=IllegalStateException.class) | @Test(expected=IllegalStateException.class) | ||||
public void testInvalidInPlaceWriteNPOIFS() throws Exception { | |||||
public void testInvalidInPlaceWritePOIFS() throws Exception { | |||||
// Can't work for Read-Only files | // Can't work for Read-Only files | ||||
POIFSFileSystem fs = new POIFSFileSystem(SAMPLES.getFile("SampleDoc.doc"), true); | POIFSFileSystem fs = new POIFSFileSystem(SAMPLES.getFile("SampleDoc.doc"), true); | ||||
HWPFDocument doc = new HWPFDocument(fs.getRoot()); | HWPFDocument doc = new HWPFDocument(fs.getRoot()); |
} | } | ||||
/** | /** | ||||
* Tests that when using NPOIFS, we can do an in-place write | |||||
* Tests that when using POIFS, we can do an in-place write | |||||
* without needing to stream in + out the whole kitchen sink | * without needing to stream in + out the whole kitchen sink | ||||
*/ | */ | ||||
@Test | @Test | ||||
public void inPlaceNPOIFSWrite() throws Exception { | |||||
public void inPlacePOIFSWrite() throws Exception { | |||||
// We need to work on a File for in-place changes, so create a temp one | // We need to work on a File for in-place changes, so create a temp one | ||||
final File copy = TempFile.createTempFile("Test-HPSF", "ole2"); | final File copy = TempFile.createTempFile("Test-HPSF", "ole2"); | ||||
copy.deleteOnExit(); | copy.deleteOnExit(); |
@Test | @Test | ||||
public void test61300Extractor() throws IOException { | public void test61300Extractor() throws IOException { | ||||
try (POIFSFileSystem npoifs = new POIFSFileSystem( | |||||
try (POIFSFileSystem poifs = new POIFSFileSystem( | |||||
POIDataSamples.getPOIFSInstance().getFile("61300.bin"))) { | POIDataSamples.getPOIFSInstance().getFile("61300.bin"))) { | ||||
HPSFPropertiesExtractor ext = new HPSFPropertiesExtractor(npoifs); | |||||
HPSFPropertiesExtractor ext = new HPSFPropertiesExtractor(poifs); | |||||
assertContains(ext.getText(), "PID_CODEPAGE = 1252"); | assertContains(ext.getText(), "PID_CODEPAGE = 1252"); | ||||
} | } | ||||
} | } |
} | } | ||||
@Test | @Test | ||||
public void testNPOIFSFileSystem() throws IOException { | |||||
public void testPOIFSFileSystem() throws IOException { | |||||
File file = HSSFTestDataSamples.getSampleFile("FormulaRefs.xls"); | File file = HSSFTestDataSamples.getSampleFile("FormulaRefs.xls"); | ||||
try (POIFSFileSystem fs = new POIFSFileSystem(file)) { | try (POIFSFileSystem fs = new POIFSFileSystem(file)) { | ||||
OldExcelExtractor extractor = new OldExcelExtractor(fs); | OldExcelExtractor extractor = new OldExcelExtractor(fs); |
* File with exactly 256 data blocks (+header block) | * File with exactly 256 data blocks (+header block) | ||||
* shouldn't break on POIFS loading | * shouldn't break on POIFS loading | ||||
*/ | */ | ||||
@SuppressWarnings("resource") | |||||
@Test | @Test | ||||
public void bug51461() throws Exception { | public void bug51461() throws Exception { | ||||
byte[] data = HSSFITestDataProvider.instance.getTestDataFileContent("51461.xls"); | byte[] data = HSSFITestDataProvider.instance.getTestDataFileContent("51461.xls"); | ||||
HSSFWorkbook wbPOIFS = new HSSFWorkbook(new POIFSFileSystem( | HSSFWorkbook wbPOIFS = new HSSFWorkbook(new POIFSFileSystem( | ||||
new ByteArrayInputStream(data)).getRoot(), false); | new ByteArrayInputStream(data)).getRoot(), false); | ||||
HSSFWorkbook wbNPOIFS = new HSSFWorkbook(new POIFSFileSystem( | |||||
HSSFWorkbook wbPOIFS2 = new HSSFWorkbook(new POIFSFileSystem( | |||||
new ByteArrayInputStream(data)).getRoot(), false); | new ByteArrayInputStream(data)).getRoot(), false); | ||||
assertEquals(2, wbPOIFS.getNumberOfSheets()); | assertEquals(2, wbPOIFS.getNumberOfSheets()); | ||||
assertEquals(2, wbNPOIFS.getNumberOfSheets()); | |||||
assertEquals(2, wbPOIFS2.getNumberOfSheets()); | |||||
} | } | ||||
/** | |||||
* Large row numbers and NPOIFS vs POIFS | |||||
*/ | |||||
@SuppressWarnings("resource") | |||||
@Test | @Test | ||||
public void bug51535() throws Exception { | public void bug51535() throws Exception { | ||||
byte[] data = HSSFITestDataProvider.instance.getTestDataFileContent("51535.xls"); | byte[] data = HSSFITestDataProvider.instance.getTestDataFileContent("51535.xls"); | ||||
HSSFWorkbook wbPOIFS = new HSSFWorkbook(new POIFSFileSystem( | HSSFWorkbook wbPOIFS = new HSSFWorkbook(new POIFSFileSystem( | ||||
new ByteArrayInputStream(data)).getRoot(), false); | new ByteArrayInputStream(data)).getRoot(), false); | ||||
HSSFWorkbook wbNPOIFS = new HSSFWorkbook(new POIFSFileSystem( | |||||
HSSFWorkbook wbPOIFS2 = new HSSFWorkbook(new POIFSFileSystem( | |||||
new ByteArrayInputStream(data)).getRoot(), false); | new ByteArrayInputStream(data)).getRoot(), false); | ||||
for (HSSFWorkbook wb : new HSSFWorkbook[]{wbPOIFS, wbNPOIFS}) { | |||||
for (HSSFWorkbook wb : new HSSFWorkbook[]{wbPOIFS, wbPOIFS2}) { | |||||
assertEquals(3, wb.getNumberOfSheets()); | assertEquals(3, wb.getNumberOfSheets()); | ||||
// Check directly | // Check directly | ||||
@Test(expected = RuntimeException.class) | @Test(expected = RuntimeException.class) | ||||
public void test61300() throws Exception { | public void test61300() throws Exception { | ||||
POIFSFileSystem npoifs = new POIFSFileSystem(HSSFTestDataSamples.openSampleFileStream("61300.xls")); | |||||
POIFSFileSystem poifs = new POIFSFileSystem(HSSFTestDataSamples.openSampleFileStream("61300.xls")); | |||||
DocumentEntry entry = | DocumentEntry entry = | ||||
(DocumentEntry) npoifs.getRoot().getEntry(SummaryInformation.DEFAULT_STREAM_NAME); | |||||
(DocumentEntry) poifs.getRoot().getEntry(SummaryInformation.DEFAULT_STREAM_NAME); | |||||
// this will throw an Exception "RuntimeException: Can't read negative number of bytes" | // this will throw an Exception "RuntimeException: Can't read negative number of bytes" | ||||
new PropertySet(new DocumentInputStream(entry)); | new PropertySet(new DocumentInputStream(entry)); |
DirectoryNode[] files = new DirectoryNode[2]; | DirectoryNode[] files = new DirectoryNode[2]; | ||||
try (POIFSFileSystem poifsFileSystem = new POIFSFileSystem(HSSFTestDataSamples.openSampleFileStream("Simple.xls"))) { | try (POIFSFileSystem poifsFileSystem = new POIFSFileSystem(HSSFTestDataSamples.openSampleFileStream("Simple.xls"))) { | ||||
files[0] = poifsFileSystem.getRoot(); | files[0] = poifsFileSystem.getRoot(); | ||||
try (POIFSFileSystem npoifsFileSystem = new POIFSFileSystem(HSSFTestDataSamples.getSampleFile("Simple.xls"))) { | |||||
files[1] = npoifsFileSystem.getRoot(); | |||||
try (POIFSFileSystem poifsFileSystem2 = new POIFSFileSystem(HSSFTestDataSamples.getSampleFile("Simple.xls"))) { | |||||
files[1] = poifsFileSystem2.getRoot(); | |||||
// Open without preserving nodes | // Open without preserving nodes | ||||
for (DirectoryNode dir : files) { | for (DirectoryNode dir : files) { | ||||
DirectoryNode[] files = new DirectoryNode[2]; | DirectoryNode[] files = new DirectoryNode[2]; | ||||
try (POIFSFileSystem poifsFileSystem = new POIFSFileSystem(HSSFTestDataSamples.openSampleFileStream("WithEmbeddedObjects.xls"))) { | try (POIFSFileSystem poifsFileSystem = new POIFSFileSystem(HSSFTestDataSamples.openSampleFileStream("WithEmbeddedObjects.xls"))) { | ||||
files[0] = poifsFileSystem.getRoot(); | files[0] = poifsFileSystem.getRoot(); | ||||
try (POIFSFileSystem npoifsFileSystem = new POIFSFileSystem(HSSFTestDataSamples.getSampleFile("WithEmbeddedObjects.xls"))) { | |||||
files[1] = npoifsFileSystem.getRoot(); | |||||
try (POIFSFileSystem poifsFileSystem2 = new POIFSFileSystem(HSSFTestDataSamples.getSampleFile("WithEmbeddedObjects.xls"))) { | |||||
files[1] = poifsFileSystem2.getRoot(); | |||||
// Check the embedded parts | // Check the embedded parts | ||||
for (DirectoryNode root : files) { | for (DirectoryNode root : files) { | ||||
} | } | ||||
/** | /** | ||||
* Checks that we can open a workbook with NPOIFS, and write it out | |||||
* Checks that we can open a workbook with POIFS, and write it out | |||||
* again (via POIFS) and have it be valid | * again (via POIFS) and have it be valid | ||||
*/ | */ | ||||
@Test | @Test | ||||
public void writeWorkbookFromNPOIFS() throws IOException { | |||||
public void writeWorkbookFromPOIFS() throws IOException { | |||||
try (InputStream is = HSSFTestDataSamples.openSampleFileStream("WithEmbeddedObjects.xls"); | try (InputStream is = HSSFTestDataSamples.openSampleFileStream("WithEmbeddedObjects.xls"); | ||||
POIFSFileSystem fs = new POIFSFileSystem(is)) { | |||||
// Start as NPOIFS | |||||
POIFSFileSystem fs = new POIFSFileSystem(is)) { | |||||
HSSFWorkbook wb = new HSSFWorkbook(fs.getRoot(), true); | HSSFWorkbook wb = new HSSFWorkbook(fs.getRoot(), true); | ||||
assertEquals(3, wb.getNumberOfSheets()); | assertEquals(3, wb.getNumberOfSheets()); | ||||
assertEquals("Root xls", wb.getSheetAt(0).getRow(0).getCell(0).getStringCellValue()); | assertEquals("Root xls", wb.getSheetAt(0).getRow(0).getCell(0).getStringCellValue()); | ||||
// Will switch to POIFS | |||||
HSSFWorkbook wbBack = HSSFTestDataSamples.writeOutAndReadBack(wb); | HSSFWorkbook wbBack = HSSFTestDataSamples.writeOutAndReadBack(wb); | ||||
assertEquals(3, wbBack.getNumberOfSheets()); | assertEquals(3, wbBack.getNumberOfSheets()); | ||||
assertEquals("Root xls", wbBack.getSheetAt(0).getRow(0).getCell(0).getStringCellValue()); | assertEquals("Root xls", wbBack.getSheetAt(0).getRow(0).getCell(0).getStringCellValue()); | ||||
final File file = POIDataSamples.getSpreadSheetInstance().getFile(filename); | final File file = POIDataSamples.getSpreadSheetInstance().getFile(filename); | ||||
Workbook wb; | Workbook wb; | ||||
// File via POIFileStream (java.io) | |||||
wb = new HSSFWorkbook(new POIFSFileSystem(file)); | |||||
assertCloseDoesNotModifyFile(filename, wb); | |||||
// File via NPOIFileStream (java.nio) | |||||
// File via POIFileStream (java.nio) | |||||
wb = new HSSFWorkbook(new POIFSFileSystem(file)); | wb = new HSSFWorkbook(new POIFSFileSystem(file)); | ||||
assertCloseDoesNotModifyFile(filename, wb); | assertCloseDoesNotModifyFile(filename, wb); | ||||
* Integration test -- really about all we can do | * Integration test -- really about all we can do | ||||
*/ | */ | ||||
@Test | @Test | ||||
public void testNPOIFSDocument() throws IOException { | |||||
public void testPOIFSDocument() throws IOException { | |||||
try (POIFSFileSystem poifs = new POIFSFileSystem()) { | try (POIFSFileSystem poifs = new POIFSFileSystem()) { | ||||
_workbook_data[j] = (byte) (j * j); | _workbook_data[j] = (byte) (j * j); | ||||
} | } | ||||
// Now create the NPOIFS Version | |||||
// Now create the POIFS Version | |||||
byte[] _workbook_data_only = new byte[_workbook_size]; | byte[] _workbook_data_only = new byte[_workbook_size]; | ||||
System.arraycopy(_workbook_data, 0, _workbook_data_only, 0, _workbook_size); | System.arraycopy(_workbook_data, 0, _workbook_data_only, 0, _workbook_size); | ||||
POIFSFileSystem npoifs = new POIFSFileSystem(); | |||||
POIFSFileSystem poifs = new POIFSFileSystem(); | |||||
// Make it easy when debugging to see what isn't the doc | // Make it easy when debugging to see what isn't the doc | ||||
byte[] minus1 = new byte[512]; | byte[] minus1 = new byte[512]; | ||||
Arrays.fill(minus1, (byte) -1); | Arrays.fill(minus1, (byte) -1); | ||||
npoifs.getBlockAt(-1).put(minus1); | |||||
npoifs.getBlockAt(0).put(minus1); | |||||
npoifs.getBlockAt(1).put(minus1); | |||||
poifs.getBlockAt(-1).put(minus1); | |||||
poifs.getBlockAt(0).put(minus1); | |||||
poifs.getBlockAt(1).put(minus1); | |||||
// Create the NPOIFS document | |||||
_workbook_n = (DocumentNode) npoifs.createDocument( | |||||
// Create the POIFS document | |||||
_workbook_n = (DocumentNode) poifs.createDocument( | |||||
new ByteArrayInputStream(_workbook_data_only), | new ByteArrayInputStream(_workbook_data_only), | ||||
"Workbook" | "Workbook" | ||||
); | ); | ||||
DocumentInputStream stream; | DocumentInputStream stream; | ||||
try (POIFSFileSystem npoifs = new POIFSFileSystem(sample)) { | |||||
try (POIFSFileSystem poifs = new POIFSFileSystem(sample)) { | |||||
// Ensure we have what we expect on the root | // Ensure we have what we expect on the root | ||||
assertEquals(npoifs, npoifs.getRoot().getFileSystem()); | |||||
assertEquals(poifs, poifs.getRoot().getFileSystem()); | |||||
// Check inside | // Check inside | ||||
DirectoryNode root = npoifs.getRoot(); | |||||
DirectoryNode root = poifs.getRoot(); | |||||
// Top Level | // Top Level | ||||
Entry top = root.getEntry("Contents"); | Entry top = root.getEntry("Contents"); | ||||
assertTrue(top.isDocumentEntry()); | assertTrue(top.isDocumentEntry()); |
assertTrue(EntryUtils.areDocumentsIdentical(entryA1, entryB1)); | assertTrue(EntryUtils.areDocumentsIdentical(entryA1, entryB1)); | ||||
// Can work with NPOIFS + POIFS | |||||
// Can work with POIFS | |||||
ByteArrayOutputStream tmpO = new ByteArrayOutputStream(); | ByteArrayOutputStream tmpO = new ByteArrayOutputStream(); | ||||
fs.writeFilesystem(tmpO); | fs.writeFilesystem(tmpO); | ||||
import org.junit.Test; | import org.junit.Test; | ||||
/** | /** | ||||
* Tests bugs across both POIFSFileSystem and NPOIFSFileSystem | |||||
* Tests bugs for POIFSFileSystem | |||||
*/ | */ | ||||
public final class TestFileSystemBugs { | public final class TestFileSystemBugs { | ||||
private static POIDataSamples _samples = POIDataSamples.getPOIFSInstance(); | private static POIDataSamples _samples = POIDataSamples.getPOIFSInstance(); | ||||
* Ensure that a file with a corrupted property in the | * Ensure that a file with a corrupted property in the | ||||
* properties table can still be loaded, and the remaining | * properties table can still be loaded, and the remaining | ||||
* properties used | * properties used | ||||
* Note - only works for NPOIFSFileSystem, POIFSFileSystem | |||||
* Note - only works for POIFSFileSystem | |||||
* can't cope with this level of corruption | * can't cope with this level of corruption | ||||
*/ | */ | ||||
@Test | @Test |
} | } | ||||
/** | /** | ||||
* Test that we can read a file with NPOIFS, create a new NPOIFS instance, | |||||
* Test that we can read a file with POIFS, create a new POIFS instance, | |||||
* write it out, read it with POIFS, and see the original data | * write it out, read it with POIFS, and see the original data | ||||
*/ | */ | ||||
@Test | @Test | ||||
public void NPOIFSReadCopyWritePOIFSRead() throws IOException { | |||||
public void POIFSReadCopyWritePOIFSRead() throws IOException { | |||||
File testFile = POIDataSamples.getSpreadSheetInstance().getFile("Simple.xls"); | File testFile = POIDataSamples.getSpreadSheetInstance().getFile("Simple.xls"); | ||||
POIFSFileSystem src = new POIFSFileSystem(testFile); | POIFSFileSystem src = new POIFSFileSystem(testFile); | ||||
byte[] wbDataExp = IOUtils.toByteArray(src.createDocumentInputStream("Workbook")); | byte[] wbDataExp = IOUtils.toByteArray(src.createDocumentInputStream("Workbook")); |
fromFile(POIDataSamples.getDiagramInstance(), "SimpleMacro.vsdm"); | fromFile(POIDataSamples.getDiagramInstance(), "SimpleMacro.vsdm"); | ||||
} | } | ||||
//////////////////////////////// From NPOIFS ///////////////////////////// | |||||
//////////////////////////////// From POIFS ///////////////////////////// | |||||
@Test | @Test | ||||
public void HSSFFromNPOIFS() throws Exception { | |||||
fromNPOIFS(POIDataSamples.getSpreadSheetInstance(), "SimpleMacro.xls"); | |||||
public void HSSFFromPOIFS() throws Exception { | |||||
fromPOIFS(POIDataSamples.getSpreadSheetInstance(), "SimpleMacro.xls"); | |||||
} | } | ||||
@Ignore("bug 59302: Found 0 macros") | @Ignore("bug 59302: Found 0 macros") | ||||
@Test | @Test | ||||
public void HSLFFromNPOIFS() throws Exception { | |||||
fromNPOIFS(POIDataSamples.getSlideShowInstance(), "SimpleMacro.ppt"); | |||||
public void HSLFFromPOIFS() throws Exception { | |||||
fromPOIFS(POIDataSamples.getSlideShowInstance(), "SimpleMacro.ppt"); | |||||
} | } | ||||
@Test | @Test | ||||
public void HWPFFromNPOIFS() throws Exception { | |||||
fromNPOIFS(POIDataSamples.getDocumentInstance(), "SimpleMacro.doc"); | |||||
public void HWPFFromPOIFS() throws Exception { | |||||
fromPOIFS(POIDataSamples.getDocumentInstance(), "SimpleMacro.doc"); | |||||
} | } | ||||
@Ignore("Found 0 macros") | @Ignore("Found 0 macros") | ||||
@Test | @Test | ||||
public void HDGFFromNPOIFS() throws Exception { | |||||
fromNPOIFS(POIDataSamples.getDiagramInstance(), "SimpleMacro.vsd"); | |||||
public void HDGFFromPOIFS() throws Exception { | |||||
fromPOIFS(POIDataSamples.getDiagramInstance(), "SimpleMacro.vsd"); | |||||
} | } | ||||
private void fromFile(POIDataSamples dataSamples, String filename) throws IOException { | private void fromFile(POIDataSamples dataSamples, String filename) throws IOException { | ||||
} | } | ||||
} | } | ||||
private void fromNPOIFS(POIDataSamples dataSamples, String filename) throws IOException { | |||||
private void fromPOIFS(POIDataSamples dataSamples, String filename) throws IOException { | |||||
File f = dataSamples.getFile(filename); | File f = dataSamples.getFile(filename); | ||||
try (POIFSFileSystem fs = new POIFSFileSystem(f)) { | try (POIFSFileSystem fs = new POIFSFileSystem(f)) { | ||||
try (VBAMacroReader r = new VBAMacroReader(fs)) { | try (VBAMacroReader r = new VBAMacroReader(fs)) { |
@SuppressWarnings("resource") | @SuppressWarnings("resource") | ||||
protected static void testFactoryFromNative(String file) throws Exception { | protected static void testFactoryFromNative(String file) throws Exception { | ||||
SlideShow<?,?> ss; | SlideShow<?,?> ss; | ||||
// from NPOIFS | |||||
// from POIFS | |||||
if (file.endsWith(".ppt")) { | if (file.endsWith(".ppt")) { | ||||
POIFSFileSystem npoifs = new POIFSFileSystem(fromFile(file)); | |||||
ss = SlideShowFactory.create(npoifs); | |||||
POIFSFileSystem poifs = new POIFSFileSystem(fromFile(file)); | |||||
ss = SlideShowFactory.create(poifs); | |||||
assertNotNull(ss); | assertNotNull(ss); | ||||
npoifs.close(); | |||||
poifs.close(); | |||||
assertCloseDoesNotModifyFile(file, ss); | assertCloseDoesNotModifyFile(file, ss); | ||||
} | } | ||||
// from OPCPackage | // from OPCPackage | ||||
@SuppressWarnings("resource") | @SuppressWarnings("resource") | ||||
protected static void testFactoryFromProtectedNative(String protectedFile, String password) throws Exception { | protected static void testFactoryFromProtectedNative(String protectedFile, String password) throws Exception { | ||||
SlideShow<?,?> ss; | SlideShow<?,?> ss; | ||||
// Encryption layer is a BIFF8 binary format that can be read by NPOIFSFileSystem, | |||||
// Encryption layer is a BIFF8 binary format that can be read by POIFSFileSystem, | |||||
// used for both HSLF and XSLF | // used for both HSLF and XSLF | ||||
// from protected NPOIFS | |||||
// from protected POIFS | |||||
if (protectedFile.endsWith(".ppt") || protectedFile.endsWith(".pptx")) { | if (protectedFile.endsWith(".ppt") || protectedFile.endsWith(".pptx")) { | ||||
POIFSFileSystem npoifs = new POIFSFileSystem(fromFile(protectedFile)); | |||||
ss = SlideShowFactory.create(npoifs, password); | |||||
POIFSFileSystem poifs = new POIFSFileSystem(fromFile(protectedFile)); | |||||
ss = SlideShowFactory.create(poifs, password); | |||||
assertNotNull(ss); | assertNotNull(ss); | ||||
npoifs.close(); | |||||
poifs.close(); | |||||
assertCloseDoesNotModifyFile(protectedFile, ss); | assertCloseDoesNotModifyFile(protectedFile, ss); | ||||
} | } | ||||
else { | else { |