You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@fesod.apache.org by be...@apache.org on 2026/10/12 11:56:21 UTC

(fesod) branch main updated: fix(sheet): skip the trailing endSheet when no XLS sheet matched (#1201)

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

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


The following commit(s) were added to refs/heads/main by this push:
     new 10320442 fix(sheet): skip the trailing endSheet when no XLS sheet matched (#1201)
10320442 is described below

commit 10320442dc35c56defd7de457200dbde14efedbf
Author: Sean-Walker0 <ja...@foxmail.com>
AuthorDate: Mon Oct 12 19:56:15 2026 +0800

    fix(sheet): skip the trailing endSheet when no XLS sheet matched (#1201)
    
    XlsSaxAnalyser.execute() ends with an unconditional endSheet call that
    covers sheet substreams lacking the EOF terminator. When the requested
    sheetNo or sheetName matches nothing in the workbook, no sheet was
    ever started and readSheetHolder() is still null, so the call throws a
    NullPointerException from DefaultAnalysisEventProcessor.endSheet and
    the whole read fails. The xlsx and csv readers guard the equivalent
    call and return an empty read for the same request.
    
    Guard the trailing call with a readSheetHolder null check, matching
    the xlsx reader: an unmatched sheet reads as empty.
    
    Verified with NoMatchSheetReadTest (the two xls cases throw NPE before
    the change and pass after; the xlsx case pins the expected behavior on
    both sides) plus the full fesod-sheet suite (951 tests) and
    spotless:check.
    
    Co-authored-by: Sean-Walker0 <30...@users.noreply.github.com>
---
 .../fesod/sheet/analysis/v03/XlsSaxAnalyser.java   |  4 +-
 .../fesod/sheet/read/NoMatchSheetReadTest.java     | 71 ++++++++++++++++++++++
 2 files changed, 74 insertions(+), 1 deletion(-)

diff --git a/fesod-sheet/src/main/java/org/apache/fesod/sheet/analysis/v03/XlsSaxAnalyser.java b/fesod-sheet/src/main/java/org/apache/fesod/sheet/analysis/v03/XlsSaxAnalyser.java
index f2c57160..4c75627b 100644
--- a/fesod-sheet/src/main/java/org/apache/fesod/sheet/analysis/v03/XlsSaxAnalyser.java
+++ b/fesod-sheet/src/main/java/org/apache/fesod/sheet/analysis/v03/XlsSaxAnalyser.java
@@ -216,7 +216,9 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
         }
 
         // There are some special xls that do not have the terminator "[EOF]", so an additional
-        xlsReadContext.analysisEventProcessor().endSheet(xlsReadContext);
+        if (xlsReadContext.readSheetHolder() != null) {
+            xlsReadContext.analysisEventProcessor().endSheet(xlsReadContext);
+        }
     }
 
     private void setCurrentUserPassword() {
diff --git a/fesod-sheet/src/test/java/org/apache/fesod/sheet/read/NoMatchSheetReadTest.java b/fesod-sheet/src/test/java/org/apache/fesod/sheet/read/NoMatchSheetReadTest.java
new file mode 100644
index 00000000..673b7ab7
--- /dev/null
+++ b/fesod-sheet/src/test/java/org/apache/fesod/sheet/read/NoMatchSheetReadTest.java
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.fesod.sheet.read;
+
+import java.io.File;
+import java.util.List;
+import java.util.Map;
+import org.apache.fesod.sheet.FesodSheet;
+import org.apache.fesod.sheet.support.ExcelTypeEnum;
+import org.apache.fesod.sheet.testkit.Tags;
+import org.apache.fesod.sheet.testkit.base.AbstractExcelTest;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Requesting a sheet that the file does not contain must behave the same for every reader type: an empty read without
+ * an exception.
+ */
+@Tag(Tags.READ)
+public class NoMatchSheetReadTest extends AbstractExcelTest {
+
+    @Test
+    public void readXlsWithNonExistingSheetNumberReturnsEmpty() {
+        File file = readFile("converter" + File.separator + "converter03.xls");
+
+        List<Map<Integer, String>> rows =
+                FesodSheet.read(file).excelType(ExcelTypeEnum.XLS).sheet(99).doReadSync();
+
+        Assertions.assertTrue(rows.isEmpty(), "an unmatched sheet must read as empty, not throw");
+    }
+
+    @Test
+    public void readXlsWithNonExistingSheetNameReturnsEmpty() {
+        File file = readFile("converter" + File.separator + "converter03.xls");
+
+        List<Map<Integer, String>> rows = FesodSheet.read(file)
+                .excelType(ExcelTypeEnum.XLS)
+                .sheet("no such sheet")
+                .doReadSync();
+
+        Assertions.assertTrue(rows.isEmpty(), "an unmatched sheet must read as empty, not throw");
+    }
+
+    @Test
+    public void readXlsxWithNonExistingSheetNumberReturnsEmpty() {
+        File file = readFile("converter" + File.separator + "converter07.xlsx");
+
+        List<Map<Integer, String>> rows =
+                FesodSheet.read(file).excelType(ExcelTypeEnum.XLSX).sheet(99).doReadSync();
+
+        Assertions.assertTrue(rows.isEmpty(), "the xlsx reader already behaves this way");
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org
For additional commands, e-mail: commits-help@fesod.apache.org