From 2ae382b2c91c6537194a49d0239424767e6e1d5e Mon Sep 17 00:00:00 2001 From: Sergey Vladimirov Date: Mon, 22 Aug 2011 08:55:21 +0000 Subject: [PATCH] refix 51686 - fix possible NPE, add writeTo() to ObjectPoolImpl. git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1160152 13f79535-47bb-0310-9956-ffa450edef68 --- .../src/org/apache/poi/hwpf/HWPFDocument.java | 14 +++++++++++++- .../src/org/apache/poi/hwpf/HWPFDocumentCore.java | 6 ++++-- .../apache/poi/hwpf/usermodel/ObjectPoolImpl.java | 9 +++++++++ .../poi/hwpf/extractor/TestWordExtractor.java | 2 +- 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java b/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java index fc979f46d..99ab39c9a 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java @@ -69,6 +69,7 @@ import org.apache.poi.poifs.filesystem.DocumentEntry; import org.apache.poi.poifs.filesystem.Entry; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.util.Internal; +import org.apache.poi.util.POIUtils; /** @@ -947,6 +948,7 @@ public final class HWPFDocument extends HWPFDocumentCore POIFSFileSystem pfs = new POIFSFileSystem(); boolean docWritten = false; boolean dataWritten = false; + boolean objectPoolWritten = false; boolean tableWritten = false; boolean propertiesWritten = false; for ( Iterator iter = directory.getEntries(); iter.hasNext(); ) @@ -961,6 +963,14 @@ public final class HWPFDocument extends HWPFDocumentCore docWritten = true; } } + else if ( entry.getName().equals( STREAM_OBJECT_POOL ) ) + { + if ( !objectPoolWritten ) + { + _objectPool.writeTo( pfs.getRoot() ); + objectPoolWritten = true; + } + } else if ( entry.getName().equals( STREAM_TABLE_0 ) || entry.getName().equals( STREAM_TABLE_1 ) ) { @@ -993,7 +1003,7 @@ public final class HWPFDocument extends HWPFDocumentCore } else { - copyNodeRecursively( entry, pfs.getRoot() ); + POIUtils.copyNodeRecursively( entry, pfs.getRoot() ); } } @@ -1008,6 +1018,8 @@ public final class HWPFDocument extends HWPFDocumentCore if ( !dataWritten ) pfs.createDocument( new ByteArrayInputStream( dataBuf ), STREAM_DATA ); + if ( !objectPoolWritten ) + _objectPool.writeTo( pfs.getRoot() ); pfs.writeFilesystem( out ); this.directory = pfs.getRoot(); diff --git a/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocumentCore.java b/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocumentCore.java index d67588dc0..4779cad53 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocumentCore.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocumentCore.java @@ -158,12 +158,14 @@ public abstract class HWPFDocumentCore extends POIDocument throw new EncryptedDocumentException("Cannot process encrypted word files!"); } + DirectoryEntry objectPoolEntry; try { - DirectoryEntry objectPoolEntry = (DirectoryEntry) directory + objectPoolEntry = (DirectoryEntry) directory .getEntry(STREAM_OBJECT_POOL); - _objectPool = new ObjectPoolImpl(objectPoolEntry); } catch (FileNotFoundException exc) { + objectPoolEntry = null; } + _objectPool = new ObjectPoolImpl(objectPoolEntry); } /** diff --git a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/ObjectPoolImpl.java b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/ObjectPoolImpl.java index 2241f9644..aa33fd504 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/ObjectPoolImpl.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/ObjectPoolImpl.java @@ -17,10 +17,12 @@ package org.apache.poi.hwpf.usermodel; import java.io.FileNotFoundException; +import java.io.IOException; import org.apache.poi.poifs.filesystem.DirectoryEntry; import org.apache.poi.poifs.filesystem.Entry; import org.apache.poi.util.Internal; +import org.apache.poi.util.POIUtils; @Internal public class ObjectPoolImpl implements ObjectsPool @@ -47,4 +49,11 @@ public class ObjectPoolImpl implements ObjectsPool return null; } } + + @Internal + public void writeTo( DirectoryEntry directoryEntry ) throws IOException + { + if ( _objectPool != null ) + POIUtils.copyNodeRecursively( _objectPool, directoryEntry ); + } } diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/extractor/TestWordExtractor.java b/src/scratchpad/testcases/org/apache/poi/hwpf/extractor/TestWordExtractor.java index 2f7a2d577..b10076c81 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/extractor/TestWordExtractor.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/extractor/TestWordExtractor.java @@ -362,7 +362,7 @@ public final class TestWordExtractor extends TestCase { * [RESOLVED FIXED] Bug 51686 - Update to POI 3.8 beta 4 causes * ConcurrentModificationException in Tika's OfficeParser */ - public void testRootEntiesNavigation() throws IOException + public void testBug51686() throws IOException { InputStream is = POIDataSamples.getDocumentInstance() .openResourceAsStream( "Bug51686.doc" );