]> source.dussan.org Git - poi.git/commitdiff
Start on refactoring ready to support NPOIFS Directory/Document nodes
authorNick Burch <nick@apache.org>
Tue, 28 Dec 2010 06:21:40 +0000 (06:21 +0000)
committerNick Burch <nick@apache.org>
Tue, 28 Dec 2010 06:21:40 +0000 (06:21 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1053269 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/poifs/filesystem/DirectoryNode.java
src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java
src/java/org/apache/poi/poifs/filesystem/POIFSDocument.java
src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java
src/testcases/org/apache/poi/poifs/filesystem/TestDocumentNode.java
src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java

index 8d79ef80a888af28cf47bf2dd804ff6fa1da676d..5b0cfb66075cb40c93dcaab738ba871dc42251b3 100644 (file)
@@ -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<Entry>
@@ -45,8 +44,11 @@ public class DirectoryNode
     // Our list of entries, kept sorted to preserve order
     private ArrayList<Entry> _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<String, Entry>();
         _entries    = new ArrayList<Entry>();
         Iterator<Property> 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;
index af1c0127e4ebe2c8a244d59ce2b8a9a0f7e5d8f9..c292b0a92ad5e6fb20dae7d05a5c5f7b99551335 100644 (file)
@@ -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;
     }
index c06e4dc4619b17f3d2436b0287ea2b482c76dac0..5588c8baa93d09fcabf7cbcb2318b54b64e1eeac 100644 (file)
@@ -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
index 751826e3fb02c27509e569de784d0824bd000b4a..0b1b6b096a6a6a2baf2097a2dbad451b4524ac2e 100644 (file)
@@ -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;
index ad077891c52200c3739c31cff885d443fe196975..6101494a07f378e25ea35a7e8331cf189994ebe5 100644 (file)
@@ -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
index 09e6dd78cf032cdc0a73fc1288615fb34a0cf74e..17e2694d00e76eafa8e9be8a20c1f3595c9a5501 100644 (file)
@@ -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<Entry> 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;
+         
+         
       }
    }