]> source.dussan.org Git - poi.git/commitdiff
refix 51686 - fix possible NPE, add writeTo() to ObjectPoolImpl.
authorSergey Vladimirov <sergey@apache.org>
Mon, 22 Aug 2011 08:55:21 +0000 (08:55 +0000)
committerSergey Vladimirov <sergey@apache.org>
Mon, 22 Aug 2011 08:55:21 +0000 (08:55 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1160152 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java
src/scratchpad/src/org/apache/poi/hwpf/HWPFDocumentCore.java
src/scratchpad/src/org/apache/poi/hwpf/usermodel/ObjectPoolImpl.java
src/scratchpad/testcases/org/apache/poi/hwpf/extractor/TestWordExtractor.java

index fc979f46d9a39e455fcd4898704fd4171c033909..99ab39c9ac627269e05d850a0cc66f877528decc 100644 (file)
@@ -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<Entry> 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();
index d67588dc080149067fb107d1d7f82f56db3d6773..4779cad53e754c3b68088dab903ff8e4a0f30f41 100644 (file)
@@ -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);
   }
 
   /**
index 2241f9644b9a9a0ed3f3aed079a2a82eea85f516..aa33fd504cb4e93f9fa8ebbda5ba7999309a1070 100644 (file)
 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 );
+    }
 }
index 2f7a2d577198353bd63b6f2c2dead4f58b583e54..b10076c81567ede31cce20b132a9411ae91ca524 100644 (file)
@@ -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" );