You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by ta...@apache.org on 2016/04/04 14:04:40 UTC

tika git commit: TIKA-1916 -- Thanks to fxfixer (Nick C) for opening the issue and submitting a patch and test file. This closes #94.

Repository: tika
Updated Branches:
  refs/heads/master 53f29e0e1 -> 5e170d437


TIKA-1916 -- Thanks to fxfixer (Nick C) for opening the issue and submitting a patch and test file.  This closes #94.


Project: http://git-wip-us.apache.org/repos/asf/tika/repo
Commit: http://git-wip-us.apache.org/repos/asf/tika/commit/5e170d43
Tree: http://git-wip-us.apache.org/repos/asf/tika/tree/5e170d43
Diff: http://git-wip-us.apache.org/repos/asf/tika/diff/5e170d43

Branch: refs/heads/master
Commit: 5e170d4373c28a4b520196591c4d94ab8073bfbf
Parents: 53f29e0
Author: tballison <ta...@mitre.org>
Authored: Mon Apr 4 08:04:22 2016 -0400
Committer: tballison <ta...@mitre.org>
Committed: Mon Apr 4 08:04:22 2016 -0400

----------------------------------------------------------------------
 .../tika/parser/odf/OpenDocumentParser.java     |  62 ++++++++++++-------
 .../apache/tika/parser/odf/ODFParserTest.java   |  10 ++-
 .../resources/test-documents/testODTNoMeta.odt  | Bin 0 -> 5847 bytes
 3 files changed, 49 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tika/blob/5e170d43/tika-parsers/src/main/java/org/apache/tika/parser/odf/OpenDocumentParser.java
----------------------------------------------------------------------
diff --git a/tika-parsers/src/main/java/org/apache/tika/parser/odf/OpenDocumentParser.java b/tika-parsers/src/main/java/org/apache/tika/parser/odf/OpenDocumentParser.java
index 65361f0..00145d2 100644
--- a/tika-parsers/src/main/java/org/apache/tika/parser/odf/OpenDocumentParser.java
+++ b/tika-parsers/src/main/java/org/apache/tika/parser/odf/OpenDocumentParser.java
@@ -16,6 +16,8 @@
  */
 package org.apache.tika.parser.odf;
 
+import static java.nio.charset.StandardCharsets.UTF_8;
+
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Arrays;
@@ -41,8 +43,6 @@ import org.xml.sax.ContentHandler;
 import org.xml.sax.SAXException;
 import org.xml.sax.helpers.DefaultHandler;
 
-import static java.nio.charset.StandardCharsets.UTF_8;
-
 /**
  * OpenOffice parser
  */
@@ -146,28 +146,20 @@ public class OpenDocumentParser extends AbstractParser {
         EndDocumentShieldingContentHandler handler =
                 new EndDocumentShieldingContentHandler(xhtml);
 
-        // If we can, process the metadata first, then the
-        //  rest of the file afterwards
-        // Only possible to guarantee that when opened from a file not a stream
-        ZipEntry entry = null;
         if (zipFile != null) {
-            entry = zipFile.getEntry(META_NAME);
-            handleZipEntry(entry, zipFile.getInputStream(entry), metadata, context, handler);
-
-            Enumeration<? extends ZipEntry> entries = zipFile.entries();
-            while (entries.hasMoreElements()) {
-                entry = entries.nextElement();
-                if (!META_NAME.equals(entry.getName())) {
-                    handleZipEntry(entry, zipFile.getInputStream(entry), metadata, context, handler);
-                }
+            try {
+                handleZipFile(zipFile, metadata, context, handler);
+            } finally {
+                //Do we want to close silently == catch an exception here?
+                zipFile.close();
             }
-            zipFile.close();
         } else {
-            do {
-                entry = zipStream.getNextEntry();
-                handleZipEntry(entry, zipStream, metadata, context, handler);
-            } while (entry != null);
-            zipStream.close();
+            try {
+                handleZipStream(zipStream, metadata, context, handler);
+            } finally {
+                //Do we want to close silently == catch an exception here?
+                zipStream.close();
+            }
         }
 
         // Only now call the end document
@@ -176,6 +168,34 @@ public class OpenDocumentParser extends AbstractParser {
         }
     }
 
+    private void handleZipStream(ZipInputStream zipStream, Metadata metadata, ParseContext context, EndDocumentShieldingContentHandler handler) throws IOException, TikaException, SAXException {
+        ZipEntry entry = zipStream.getNextEntry();
+        while (entry != null) {
+            handleZipEntry(entry, zipStream, metadata, context, handler);
+            entry = zipStream.getNextEntry();
+        }
+    }
+
+    private void handleZipFile(ZipFile zipFile, Metadata metadata,
+                               ParseContext context, EndDocumentShieldingContentHandler handler)
+            throws IOException, TikaException, SAXException {
+        // If we can, process the metadata first, then the
+        //  rest of the file afterwards (TIKA-1353)
+        // Only possible to guarantee that when opened from a file not a stream
+
+        ZipEntry entry = zipFile.getEntry(META_NAME);
+        if (entry != null) {
+            handleZipEntry(entry, zipFile.getInputStream(entry), metadata, context, handler);
+        }
+
+        Enumeration<? extends ZipEntry> entries = zipFile.entries();
+        while (entries.hasMoreElements()) {
+            entry = entries.nextElement();
+            if (!META_NAME.equals(entry.getName())) {
+                handleZipEntry(entry, zipFile.getInputStream(entry), metadata, context, handler);
+            }
+        }
+    }
     private void handleZipEntry(ZipEntry entry, InputStream zip, Metadata metadata,
                                 ParseContext context, EndDocumentShieldingContentHandler handler)
             throws IOException, SAXException, TikaException {

http://git-wip-us.apache.org/repos/asf/tika/blob/5e170d43/tika-parsers/src/test/java/org/apache/tika/parser/odf/ODFParserTest.java
----------------------------------------------------------------------
diff --git a/tika-parsers/src/test/java/org/apache/tika/parser/odf/ODFParserTest.java b/tika-parsers/src/test/java/org/apache/tika/parser/odf/ODFParserTest.java
index 9ef5d44..d20f3e9 100644
--- a/tika-parsers/src/test/java/org/apache/tika/parser/odf/ODFParserTest.java
+++ b/tika-parsers/src/test/java/org/apache/tika/parser/odf/ODFParserTest.java
@@ -137,7 +137,7 @@ public class ODFParserTest extends TikaTest {
    }
 
    /**
-    * Similar to {@link #testXMLParser()}, but using a different
+    * Similar to {@link #testOO2()}, but using a different
     *  OO2 file with different metadata in it
     */
     @Test
@@ -197,7 +197,7 @@ public class ODFParserTest extends TikaTest {
    }
 
    /**
-    * Similar to {@link #testXMLParser()}, but using an OO3 file
+    * Similar to {@link #testOO2()} )}, but using an OO3 file
     */
     @Test
    public void testOO3Metadata() throws Exception {
@@ -375,4 +375,10 @@ public class ODFParserTest extends TikaTest {
             assertContains("La page d’accueil permet de faire une recherche simple", content);
         }
     }
+
+    @Test  //TIKA-1916
+    public void testMissingMeta() throws Exception {
+        String xml = getXML("testODTNoMeta.odt").xml;
+        assertContains("Test text", xml);
+    }
 }

http://git-wip-us.apache.org/repos/asf/tika/blob/5e170d43/tika-parsers/src/test/resources/test-documents/testODTNoMeta.odt
----------------------------------------------------------------------
diff --git a/tika-parsers/src/test/resources/test-documents/testODTNoMeta.odt b/tika-parsers/src/test/resources/test-documents/testODTNoMeta.odt
new file mode 100644
index 0000000..220c5b9
Binary files /dev/null and b/tika-parsers/src/test/resources/test-documents/testODTNoMeta.odt differ