]> source.dussan.org Git - poi.git/commitdiff
Fix first part of bug #51514 - HSSF copy nodes from NPOIFS
authorNick Burch <nick@apache.org>
Fri, 15 Jul 2011 15:02:19 +0000 (15:02 +0000)
committerNick Burch <nick@apache.org>
Fri, 15 Jul 2011 15:02:19 +0000 (15:02 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1147179 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/java/org/apache/poi/POIDocument.java
src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java

index da2b6b8423f391b3f72ae7f0a0cd401b09e99a2c..bd5eb5bde1949d7b3ca617f6e5f0eac540c27ee5 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.8-beta4" date="2011-??-??">
+           <action dev="poi-developers" type="fix">51514 - avoid NPE when copying nodes from one HSSF workbook to a new one, when opened from NPOIFS</action>
            <action dev="poi-developers" type="fix">51504 - avoid NPE when DefaultRowHeight or DefaultColumnWidth records are missing</action>
            <action dev="poi-developers" type="fix">51502 - Correct Subtotal function javadoc entry</action>
            <action dev="poi-developers" type="add">Support for hyperlinks in SXSSF</action>
index eda213fb91b3d40c51f60f4c5fde1398e794eead..f3a187f7f711cbdcb3b135521151547b44168883 100644 (file)
@@ -239,19 +239,26 @@ public abstract class POIDocument {
        protected void copyNodes(POIFSFileSystem source, POIFSFileSystem target,
                                  List<String> excepts) throws IOException {
                //System.err.println("CopyNodes called");
-
-               DirectoryEntry root = source.getRoot();
-               DirectoryEntry newRoot = target.getRoot();
-
-               Iterator<Entry> entries = root.getEntries();
-               while (entries.hasNext()) {
-                       Entry entry = entries.next();
-                       if (!excepts.contains(entry.getName())) {
-                               copyNodeRecursively(entry,newRoot);
-                       }
-               }
+          copyNodes(source.getRoot(), target.getRoot(), excepts);
        }
                
+   /**
+    * Copies nodes from one POIFS to the other minus the excepts
+    * @param source is the source POIFS to copy from
+    * @param target is the target POIFS to copy to
+    * @param excepts is a list of Strings specifying what nodes NOT to copy
+    */
+   protected void copyNodes(DirectoryNode sourceRoot, DirectoryNode targetRoot,
+                             List<String> excepts) throws IOException {
+      Iterator<Entry> entries = sourceRoot.getEntries();
+      while (entries.hasNext()) {
+         Entry entry = entries.next();
+         if (!excepts.contains(entry.getName())) {
+            copyNodeRecursively(entry,targetRoot);
+         }
+      }
+   }
+      
        /**
         * Copies an Entry into a target POIFS directory, recursively
         */
index 158656aad17636b6667850f1f59a346365ea9d28..54f9c62e5d78add395f5b5664fca02d7f7aa5a47 100644 (file)
@@ -36,27 +36,27 @@ import org.apache.poi.ddf.EscherBlipRecord;
 import org.apache.poi.ddf.EscherRecord;
 import org.apache.poi.hssf.OldExcelFormatException;
 import org.apache.poi.hssf.model.HSSFFormulaParser;
-import org.apache.poi.hssf.model.RecordStream;
 import org.apache.poi.hssf.model.InternalSheet;
 import org.apache.poi.hssf.model.InternalWorkbook;
+import org.apache.poi.hssf.model.RecordStream;
 import org.apache.poi.hssf.record.*;
 import org.apache.poi.hssf.record.aggregates.RecordAggregate.RecordVisitor;
 import org.apache.poi.hssf.record.common.UnicodeString;
+import org.apache.poi.hssf.util.CellReference;
+import org.apache.poi.poifs.filesystem.DirectoryNode;
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 import org.apache.poi.ss.formula.FormulaShifter;
+import org.apache.poi.ss.formula.FormulaType;
+import org.apache.poi.ss.formula.SheetNameFormatter;
 import org.apache.poi.ss.formula.ptg.Area3DPtg;
 import org.apache.poi.ss.formula.ptg.MemFuncPtg;
 import org.apache.poi.ss.formula.ptg.OperandPtg;
 import org.apache.poi.ss.formula.ptg.Ptg;
 import org.apache.poi.ss.formula.ptg.Ref3DPtg;
-import org.apache.poi.ss.formula.SheetNameFormatter;
 import org.apache.poi.ss.formula.ptg.UnionPtg;
-import org.apache.poi.hssf.util.CellReference;
-import org.apache.poi.poifs.filesystem.DirectoryNode;
-import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 import org.apache.poi.ss.formula.udf.AggregatingUDFFinder;
 import org.apache.poi.ss.formula.udf.UDFFinder;
 import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
-import org.apache.poi.ss.formula.FormulaType;
 import org.apache.poi.ss.util.WorkbookUtil;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
@@ -1244,13 +1244,12 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
             //  out correctly shortly, so don't include the old one
             excepts.add("WORKBOOK");
 
-            POIFSFileSystem srcFs = this.directory.getFileSystem();
             // Copy over all the other nodes to our new poifs
-            copyNodes(srcFs, fs, excepts);
+            copyNodes(this.directory, fs.getRoot(), excepts);
 
             // YK: preserve StorageClsid, it is important for embedded workbooks,
             // see Bugzilla 47920
-            fs.getRoot().setStorageClsid(srcFs.getRoot().getStorageClsid());
+            fs.getRoot().setStorageClsid(this.directory.getStorageClsid());
         }
         fs.writeFilesystem(stream);
     }