From f8aa3bcced6f1f208ba0ac29a6d59d28ea25456a Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Fri, 15 Jul 2011 15:02:19 +0000 Subject: [PATCH] Fix first part of bug #51514 - HSSF copy nodes from NPOIFS git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1147179 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + src/java/org/apache/poi/POIDocument.java | 29 ++++++++++++------- .../poi/hssf/usermodel/HSSFWorkbook.java | 17 +++++------ 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index da2b6b8423..bd5eb5bde1 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 51514 - avoid NPE when copying nodes from one HSSF workbook to a new one, when opened from NPOIFS 51504 - avoid NPE when DefaultRowHeight or DefaultColumnWidth records are missing 51502 - Correct Subtotal function javadoc entry Support for hyperlinks in SXSSF diff --git a/src/java/org/apache/poi/POIDocument.java b/src/java/org/apache/poi/POIDocument.java index eda213fb91..f3a187f7f7 100644 --- a/src/java/org/apache/poi/POIDocument.java +++ b/src/java/org/apache/poi/POIDocument.java @@ -239,19 +239,26 @@ public abstract class POIDocument { protected void copyNodes(POIFSFileSystem source, POIFSFileSystem target, List excepts) throws IOException { //System.err.println("CopyNodes called"); - - DirectoryEntry root = source.getRoot(); - DirectoryEntry newRoot = target.getRoot(); - - Iterator 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 excepts) throws IOException { + Iterator 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 */ diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index 158656aad1..54f9c62e5d 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -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); } -- 2.39.5