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 2023/06/08 14:58:22 UTC

[tika] branch main updated: TIKA-4062 (#1179)

This is an automated email from the ASF dual-hosted git repository.

tallison pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tika.git


The following commit(s) were added to refs/heads/main by this push:
     new ceed7be8b TIKA-4062 (#1179)
ceed7be8b is described below

commit ceed7be8b1bffd697a79590e50a413744a0b108f
Author: Tim Allison <ta...@apache.org>
AuthorDate: Thu Jun 8 10:58:16 2023 -0400

    TIKA-4062 (#1179)
    
    * TIKA-4062 -- ContentHandlerDecorator should delegate handleException if the wrapped handler is itself a ContentHandlerDecorator
    
    * add special handling for handlers that implement ErrorHandler... based on feedback from Ravi Ranjan Jha
    
    * fix bug in WriteLimitReachedException
    
    -- needs a unit test
---
 .../tika/exception/WriteLimitReachedException.java |  6 ++--
 .../apache/tika/sax/ContentHandlerDecorator.java   | 38 +++++++++++++++++++++-
 2 files changed, 40 insertions(+), 4 deletions(-)

diff --git a/tika-core/src/main/java/org/apache/tika/exception/WriteLimitReachedException.java b/tika-core/src/main/java/org/apache/tika/exception/WriteLimitReachedException.java
index fe0621efc..3e661ada5 100644
--- a/tika-core/src/main/java/org/apache/tika/exception/WriteLimitReachedException.java
+++ b/tika-core/src/main/java/org/apache/tika/exception/WriteLimitReachedException.java
@@ -59,7 +59,7 @@ public class WriteLimitReachedException extends SAXException {
         if (t instanceof WriteLimitReachedException) {
             return true;
         } else {
-            return t.getCause() != null && isWriteLimitReached(t.getCause(), depth + 1);
+            return isWriteLimitReached(t.getCause(), depth + 1);
         }
     }
 
@@ -67,7 +67,7 @@ public class WriteLimitReachedException extends SAXException {
         throwIfWriteLimitReached(ex, 0);
     }
 
-    private static void throwIfWriteLimitReached(Exception ex, int depth) throws SAXException {
+    private static void throwIfWriteLimitReached(Throwable ex, int depth) throws SAXException {
         if (ex == null) {
             return;
         }
@@ -77,7 +77,7 @@ public class WriteLimitReachedException extends SAXException {
         if (ex instanceof WriteLimitReachedException) {
             throw (SAXException) ex;
         } else {
-            isWriteLimitReached(ex.getCause(), depth + 1);
+            throwIfWriteLimitReached(ex.getCause(), depth + 1);
         }
     }
 }
diff --git a/tika-core/src/main/java/org/apache/tika/sax/ContentHandlerDecorator.java b/tika-core/src/main/java/org/apache/tika/sax/ContentHandlerDecorator.java
index 258e67bf0..b7ce5c77a 100644
--- a/tika-core/src/main/java/org/apache/tika/sax/ContentHandlerDecorator.java
+++ b/tika-core/src/main/java/org/apache/tika/sax/ContentHandlerDecorator.java
@@ -18,8 +18,10 @@ package org.apache.tika.sax;
 
 import org.xml.sax.Attributes;
 import org.xml.sax.ContentHandler;
+import org.xml.sax.ErrorHandler;
 import org.xml.sax.Locator;
 import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
 import org.xml.sax.helpers.DefaultHandler;
 
 /**
@@ -172,12 +174,46 @@ public class ContentHandlerDecorator extends DefaultHandler {
      * provides a single place to implement custom exception handling. The
      * default behaviour is simply to re-throw the given exception, but
      * subclasses can also provide alternative ways of handling the situation.
+     * 
+     * If the wrapped handler is itself a ContentHandlerDecorator, the call
+     * is delegated to the wrapped handler's {@link ContentHandlerDecorator#handleException(SAXException)}
      *
      * @param exception the exception that was thrown
      * @throws SAXException the exception (if any) thrown to the client
      */
     protected void handleException(SAXException exception) throws SAXException {
-        throw exception;
+        if (handler instanceof ContentHandlerDecorator) {
+            ((ContentHandlerDecorator)handler).handleException(exception);
+        } else {
+            throw exception;
+        }
     }
 
+    @Override
+    public void warning (SAXParseException exception) throws SAXException {
+        if (handler instanceof ErrorHandler) {
+            ((ErrorHandler)handler).warning(exception);
+        } else {
+            super.warning(exception);
+        }
+    }
+
+    @Override
+    public void error (SAXParseException exception) throws SAXException {
+        if (handler instanceof ErrorHandler) {
+            ((ErrorHandler)handler).error(exception);
+        } else {
+            super.error(exception);
+        }
+    }
+
+    @Override
+    public void fatalError (SAXParseException exception)
+            throws SAXException {
+        if (handler instanceof ErrorHandler) {
+            ((ErrorHandler)handler).fatalError(exception);
+        } else {
+            super.fatalError(exception);
+        }
+    }
 }