]> source.dussan.org Git - poi.git/commitdiff
Fix bug #50118
authorNick Burch <nick@apache.org>
Fri, 22 Oct 2010 17:23:39 +0000 (17:23 +0000)
committerNick Burch <nick@apache.org>
Fri, 22 Oct 2010 17:23:39 +0000 (17:23 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1026412 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/java/org/apache/poi/poifs/filesystem/POIFSDocumentPath.java
src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSDocumentPath.java
src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java
test-data/poifs/Notes.ole2 [new file with mode: 0644]

index c27545d3cfea6a8b5af5783c7f0c2d7035eb7cc0..8ea8eb323b5e5f74bbfd1251caf9f222abae9cab 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.8-beta1" date="2010-??-??">
+           <action dev="poi-developers" type="fix">50118 - OLE2 does allow a directory with an empty name, so support this in POIFS</action>
            <action dev="poi-developers" type="fix">50119 - avoid NPE when XSSFReader comes across chart sheets</action>
         </release>
         <release version="3.7" date="2010-10-25">
index 9617d6836a3fb74f458efe83dba0260d13a53168..dc1539a939873d023abfe90f060d45f14564aa95 100644 (file)
@@ -21,6 +21,9 @@ package org.apache.poi.poifs.filesystem;
 
 import java.io.File;
 
+import org.apache.poi.util.POILogFactory;
+import org.apache.poi.util.POILogger;
+
 /**
  * Class POIFSDocumentPath
  *
@@ -30,6 +33,8 @@ import java.io.File;
 
 public class POIFSDocumentPath
 {
+    private static final POILogger log = POILogFactory.getLogger(POIFSDocumentPath.class);
+          
     private String[] components;
     private int      hashcode = 0;
 
@@ -125,12 +130,17 @@ public class POIFSDocumentPath
         {
             for (int j = 0; j < components.length; j++)
             {
-                if ((components[ j ] == null)
-                        || (components[ j ].length() == 0))
+                if (components[ j ] == null)
                 {
                     throw new IllegalArgumentException(
-                        "components cannot contain null or empty strings");
+                        "components cannot contain null");
+                }
+                if (components[ j ].length() == 0)
+                {
+                    log.log(POILogger.WARN, "Directory under " + path + " has an empty name, " +
+                            "not all OLE2 readers will handle this file correctly!");
                 }
+                
                 this.components[ j + path.components.length ] =
                     components[ j ];
             }
index b09c46945802aa2f402cb69cc16e4b32de53239d..fef200fdcbf76eccd263de84869ecbaa4b2762c1 100644 (file)
@@ -165,24 +165,40 @@ public final class TestPOIFSDocumentPath extends TestCase {
                 }
             }
 
-            // test weird variants
+            // Test weird variants
+            
+            // This one is allowed, even if it's really odd
             assertEquals(n, new POIFSDocumentPath(base, null).length());
+            new POIFSDocumentPath(base, new String[]
+            {
+                 "fu", ""
+            });
+            
+            // This one is allowed too
+            new POIFSDocumentPath(base, new String[]
+            {
+                 "", "fu"
+            });
+            
+            // This one shouldn't be allowed
             try
             {
                 new POIFSDocumentPath(base, new String[]
                 {
-                    "fu", ""
+                    "fu", null
                 });
                 fail("should have caught IllegalArgumentException");
             }
             catch (IllegalArgumentException ignored)
             {
             }
+            
+            // Ditto
             try
             {
                 new POIFSDocumentPath(base, new String[]
                 {
-                    "fu", null
+                    null, "fu"
                 });
                 fail("should have caught IllegalArgumentException");
             }
index 5ebc1689412869b5fd5248189f4f4801bf39fc64..ae9ac644a411762c73f0edb708a65f7309bf1356 100644 (file)
@@ -22,6 +22,7 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.Iterator;
 
 import junit.framework.TestCase;
 
@@ -226,6 +227,43 @@ public final class TestPOIFSFileSystem extends TestCase {
              }
           }
        }
+       
+       /**
+        * Test that we can open files that come via Lotus notes.
+        * These have a top level directory without a name....
+        */
+       public void testNotesOLE2Files() throws Exception {
+      POIDataSamples _samples = POIDataSamples.getPOIFSInstance();
+      
+      // Open the file up
+      POIFSFileSystem fs = new POIFSFileSystem(
+          _samples.openResourceAsStream("Notes.ole2")
+      );
+      
+      // Check the contents
+      assertEquals(1, fs.getRoot().getEntryCount());
+      
+      Entry entry = fs.getRoot().getEntries().next();
+      assertTrue(entry.isDirectoryEntry());
+      assertTrue(entry instanceof DirectoryEntry);
+      
+      // The directory lacks a name!
+      DirectoryEntry dir = (DirectoryEntry)entry;
+      assertEquals("", dir.getName());
+      
+      // Has two children
+      assertEquals(2, dir.getEntryCount());
+      
+      // Check them
+      Iterator<Entry> it = dir.getEntries();
+      entry = it.next();
+      assertEquals(true, entry.isDocumentEntry());
+      assertEquals("\u0001Ole10Native", entry.getName());
+      
+      entry = it.next();
+      assertEquals(true, entry.isDocumentEntry());
+      assertEquals("\u0001CompObj", entry.getName());
+       }
 
        private static InputStream openSampleStream(String sampleFileName) {
                return HSSFTestDataSamples.openSampleFileStream(sampleFileName);
diff --git a/test-data/poifs/Notes.ole2 b/test-data/poifs/Notes.ole2
new file mode 100644 (file)
index 0000000..9aaed99
Binary files /dev/null and b/test-data/poifs/Notes.ole2 differ