From f88663e5bc645f64df6ffa328fb83a881c7528dc Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Tue, 28 Dec 2010 06:21:40 +0000 Subject: [PATCH] Start on refactoring ready to support NPOIFS Directory/Document nodes git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1053269 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/poifs/filesystem/DirectoryNode.java | 79 +++++++++++++++---- .../poifs/filesystem/NPOIFSFileSystem.java | 34 +++++--- .../poi/poifs/filesystem/POIFSDocument.java | 2 +- .../filesystem/TestDocumentInputStream.java | 2 +- .../poifs/filesystem/TestDocumentNode.java | 2 +- .../filesystem/TestNPOIFSFileSystem.java | 56 +++++++++---- 6 files changed, 131 insertions(+), 44 deletions(-) diff --git a/src/java/org/apache/poi/poifs/filesystem/DirectoryNode.java b/src/java/org/apache/poi/poifs/filesystem/DirectoryNode.java index 8d79ef80a8..5b0cfb6607 100644 --- a/src/java/org/apache/poi/poifs/filesystem/DirectoryNode.java +++ b/src/java/org/apache/poi/poifs/filesystem/DirectoryNode.java @@ -34,7 +34,6 @@ import org.apache.poi.poifs.property.Property; * * @author Marc Johnson (mjohnson at apache dot org) */ - public class DirectoryNode extends EntryNode implements DirectoryEntry, POIFSViewable, Iterable @@ -45,8 +44,11 @@ public class DirectoryNode // Our list of entries, kept sorted to preserve order private ArrayList _entries; + // Only one of these two will exist // the POIFSFileSystem we belong to - private POIFSFileSystem _filesystem; + private POIFSFileSystem _ofilesystem; + // the NPOIFSFileSytem we belong to + private NPOIFSFileSystem _nfilesystem; // the path described by this document private POIFSDocumentPath _path; @@ -59,10 +61,32 @@ public class DirectoryNode * @param filesystem the POIFSFileSystem we belong to * @param parent the parent of this entry */ - DirectoryNode(final DirectoryProperty property, final POIFSFileSystem filesystem, final DirectoryNode parent) + { + this(property, parent); + _ofilesystem = filesystem; + } + + /** + * create a DirectoryNode. This method is not public by design; it + * is intended strictly for the internal use of this package + * + * @param property the DirectoryProperty for this DirectoryEntry + * @param nfilesystem the NPOIFSFileSystem we belong to + * @param parent the parent of this entry + */ + DirectoryNode(final DirectoryProperty property, + final NPOIFSFileSystem nfilesystem, + final DirectoryNode parent) + { + this(property, parent); + _nfilesystem = nfilesystem; + } + + private DirectoryNode(final DirectoryProperty property, + final DirectoryNode parent) { super(property, parent); if (parent == null) @@ -76,7 +100,6 @@ public class DirectoryNode property.getName() }); } - _filesystem = filesystem; _byname = new HashMap(); _entries = new ArrayList(); Iterator iter = property.getChildren(); @@ -88,8 +111,12 @@ public class DirectoryNode if (child.isDirectory()) { - childNode = new DirectoryNode(( DirectoryProperty ) child, - _filesystem, this); + DirectoryProperty childDir = (DirectoryProperty) child; + if(_ofilesystem != null) { + childNode = new DirectoryNode(childDir, _ofilesystem, this); + } else { + childNode = new DirectoryNode(childDir, _nfilesystem, this); + } } else { @@ -113,10 +140,17 @@ public class DirectoryNode /** * @return the filesystem that this belongs to */ - public POIFSFileSystem getFileSystem() { - return _filesystem; + return _ofilesystem; + } + + /** + * @return the filesystem that this belongs to + */ + public NPOIFSFileSystem getNFileSystem() + { + return _nfilesystem; } /** @@ -161,7 +195,13 @@ public class DirectoryNode DocumentNode rval = new DocumentNode(property, this); (( DirectoryProperty ) getProperty()).addChild(property); - _filesystem.addDocument(document); + + if(_ofilesystem != null) { + _ofilesystem.addDocument(document); + } else { + _nfilesystem.addDocument(document); + } + _entries.add(rval); _byname.put(property.getName(), rval); return rval; @@ -211,8 +251,13 @@ public class DirectoryNode if (rval) { _entries.remove(entry); - _byname.remove(entry.getName()); - _filesystem.remove(entry); + _byname.remove(entry.getName()); + + if(_ofilesystem != null) { + _ofilesystem.remove(entry); + } else { + _nfilesystem.remove(entry); + } } return rval; } @@ -341,12 +386,18 @@ public class DirectoryNode public DirectoryEntry createDirectory(final String name) throws IOException { + DirectoryNode rval; DirectoryProperty property = new DirectoryProperty(name); - DirectoryNode rval = new DirectoryNode(property, _filesystem, - this); + + if(_ofilesystem != null) { + rval = new DirectoryNode(property, _ofilesystem, this); + _ofilesystem.addDirectory(property); + } else { + rval = new DirectoryNode(property, _nfilesystem, this); + _nfilesystem.addDirectory(property); + } (( DirectoryProperty ) getProperty()).addChild(property); - _filesystem.addDirectory(property); _entries.add(rval); _byname.put(name, rval); return rval; diff --git a/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java b/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java index af1c0127e4..c292b0a92a 100644 --- a/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java +++ b/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java @@ -19,7 +19,6 @@ package org.apache.poi.poifs.filesystem; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -45,15 +44,11 @@ import org.apache.poi.poifs.nio.DataSource; import org.apache.poi.poifs.nio.FileBackedDataSource; import org.apache.poi.poifs.property.DirectoryProperty; import org.apache.poi.poifs.property.NPropertyTable; -import org.apache.poi.poifs.property.Property; import org.apache.poi.poifs.storage.BATBlock; import org.apache.poi.poifs.storage.BlockAllocationTableWriter; -import org.apache.poi.poifs.storage.BlockList; -import org.apache.poi.poifs.storage.BlockWritable; import org.apache.poi.poifs.storage.HeaderBlock; import org.apache.poi.poifs.storage.HeaderBlockConstants; import org.apache.poi.poifs.storage.HeaderBlockWriter; -import org.apache.poi.poifs.storage.SmallBlockTableWriter; import org.apache.poi.poifs.storage.BATBlock.BATBlockAndIndex; import org.apache.poi.util.CloseIgnoringInputStream; import org.apache.poi.util.IOUtils; @@ -464,6 +459,26 @@ public class NPOIFSFileSystem extends BlockStore return _mini_store; } + /** + * add a new POIFSDocument to the FileSytem + * + * @param document the POIFSDocument being added + */ + void addDocument(final POIFSDocument document) + { + _property_table.addProperty(document.getDocumentProperty()); + } + + /** + * add a new DirectoryProperty to the FileSystem + * + * @param directory the DirectoryProperty being added + */ + void addDirectory(final DirectoryProperty directory) + { + _property_table.addProperty(directory); + } + /** * Create a new document to be added to the root directory * @@ -610,17 +625,14 @@ public class NPOIFSFileSystem extends BlockStore } /** - * get the root entry + * Get the root entry * * @return the root entry */ - public DirectoryNode getRoot() { - if (_root == null) - { - // TODO -// _root = new DirectoryNode(_property_table.getRoot(), this, null); + if (_root == null) { + _root = new DirectoryNode(_property_table.getRoot(), this, null); } return _root; } diff --git a/src/java/org/apache/poi/poifs/filesystem/POIFSDocument.java b/src/java/org/apache/poi/poifs/filesystem/POIFSDocument.java index c06e4dc461..5588c8baa9 100644 --- a/src/java/org/apache/poi/poifs/filesystem/POIFSDocument.java +++ b/src/java/org/apache/poi/poifs/filesystem/POIFSDocument.java @@ -54,7 +54,7 @@ public final class POIFSDocument implements BATManaged, BlockWritable, POIFSView // one of these stores will be valid private SmallBlockStore _small_store; - private BigBlockStore _big_store; + private BigBlockStore _big_store; /** * Constructor from large blocks diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java b/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java index 751826e3fb..0b1b6b096a 100644 --- a/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java +++ b/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java @@ -57,7 +57,7 @@ public final class TestDocumentInputStream extends TestCase { _workbook = new DocumentNode( document.getDocumentProperty(), new DirectoryNode( - new DirectoryProperty("Root Entry"), null, null)); + new DirectoryProperty("Root Entry"), (POIFSFileSystem)null, null)); } private DocumentNode _workbook; diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentNode.java b/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentNode.java index ad077891c5..6101494a07 100644 --- a/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentNode.java +++ b/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentNode.java @@ -49,7 +49,7 @@ public final class TestDocumentNode extends TestCase { POIFSDocument document = new POIFSDocument("document", rawBlocks, 2000); DocumentProperty property2 = document.getDocumentProperty(); - DirectoryNode parent = new DirectoryNode(property1, null, null); + DirectoryNode parent = new DirectoryNode(property1, (POIFSFileSystem)null, null); DocumentNode node = new DocumentNode(property2, parent); // verify we can retrieve the document diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java b/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java index 09e6dd78cf..17e2694d00 100644 --- a/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java +++ b/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java @@ -402,14 +402,36 @@ public final class TestNPOIFSFileSystem extends TestCase { public void testListEntries() throws Exception { NPOIFSFileSystem fsA = new NPOIFSFileSystem(_inst.getFile("BlockSize512.zvi")); NPOIFSFileSystem fsB = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize512.zvi")); - for(NPOIFSFileSystem fs : new NPOIFSFileSystem[] {fsA,fsB}) { - // TODO - } - - fsA = new NPOIFSFileSystem(_inst.getFile("BlockSize4096.zvi")); - fsB = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize4096.zvi")); - for(NPOIFSFileSystem fs : new NPOIFSFileSystem[] {fsA,fsB}) { - // TODO + NPOIFSFileSystem fsC = new NPOIFSFileSystem(_inst.getFile("BlockSize4096.zvi")); + NPOIFSFileSystem fsD = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize4096.zvi")); + for(NPOIFSFileSystem fs : new NPOIFSFileSystem[] {fsA,fsB,fsC,fsD}) { + DirectoryEntry root = fs.getRoot(); + assertEquals(5, root.getEntryCount()); + + // Check by the names + Entry thumbnail = root.getEntry("Thumbnail"); + Entry dsi = root.getEntry("\u0005DocumentSummaryInformation"); + Entry si = root.getEntry("\u0005SummaryInformation"); + Entry image = root.getEntry("Image"); + Entry tags = root.getEntry("Tags"); + + assertEquals(false, thumbnail.isDirectoryEntry()); + assertEquals(false, dsi.isDirectoryEntry()); + assertEquals(false, si.isDirectoryEntry()); + assertEquals(true, image.isDirectoryEntry()); + assertEquals(false, tags.isDirectoryEntry()); + + // Check via the iterator + Iterator it = root.getEntries(); + assertEquals(thumbnail.getName(), it.next().getName()); + assertEquals(dsi.getName(), it.next().getName()); + assertEquals(si.getName(), it.next().getName()); + assertEquals(image.getName(), it.next().getName()); + assertEquals(tags.getName(), it.next().getName()); + + // Look inside another + DirectoryEntry imageD = (DirectoryEntry)image; + assertEquals(7, imageD.getEntryCount()); } } @@ -420,14 +442,16 @@ public final class TestNPOIFSFileSystem extends TestCase { public void testGetDocumentEntry() throws Exception { NPOIFSFileSystem fsA = new NPOIFSFileSystem(_inst.getFile("BlockSize512.zvi")); NPOIFSFileSystem fsB = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize512.zvi")); - for(NPOIFSFileSystem fs : new NPOIFSFileSystem[] {fsA,fsB}) { - // TODO - } - - fsA = new NPOIFSFileSystem(_inst.getFile("BlockSize4096.zvi")); - fsB = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize4096.zvi")); - for(NPOIFSFileSystem fs : new NPOIFSFileSystem[] {fsA,fsB}) { - // TODO + NPOIFSFileSystem fsC = new NPOIFSFileSystem(_inst.getFile("BlockSize4096.zvi")); + NPOIFSFileSystem fsD = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize4096.zvi")); + for(NPOIFSFileSystem fs : new NPOIFSFileSystem[] {fsA,fsB,fsC,fsD}) { + DirectoryEntry root = fs.getRoot(); + Entry dsi = root.getEntry("\u0005DocumentSummaryInformation"); + + assertEquals(true, dsi.isDocumentEntry()); + DocumentEntry doc = (DocumentEntry)dsi; + + } } -- 2.39.5