修复“sax方式读取excel2003版本,会调用两次doAfterAllAnalysed方法”问题

This commit is contained in:
Looly 2023-02-09 19:43:49 +08:00
parent 0e3cf48875
commit 771f0a4809
5 changed files with 46 additions and 113 deletions

View File

@ -23,6 +23,7 @@
* 【core 】 修复ClassScanner自定义classload无效问题issue#I68TV2@Gitee * 【core 】 修复ClassScanner自定义classload无效问题issue#I68TV2@Gitee
* 【core 】 【重要】删除XmlUtil.readObjectFromXml方法避免漏洞issue#2857@Github * 【core 】 【重要】删除XmlUtil.readObjectFromXml方法避免漏洞issue#2857@Github
* 【core 】 修复Ipv4Util.list()方法的bugpr#929@Gitee * 【core 】 修复Ipv4Util.list()方法的bugpr#929@Gitee
* 【poi 】 修复“sax方式读取excel2003版本会调用两次doAfterAllAnalysed方法”问题。pr#919@Gitee
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------

View File

@ -1659,35 +1659,6 @@ public class CollUtil {
return collection == null || collection.isEmpty(); return collection == null || collection.isEmpty();
} }
/**
* 集合是否为空
* 如果集合中所有元素为null或空串也认为此集合为空
* @param collection
* @return
*/
public static boolean isBlank(Collection<?> collection) {
if(isEmpty(collection)){
return true;
}
for(Object o: collection){
if(ObjectUtil.isNotEmpty(o)){
return false;
}
}
return true;
}
/**
* 集合是否为非空
* 集合长度大于0且所有元素中至少有一个不为null或空串
* @param collection
* @return
*/
public static boolean isNotBlank(Collection<?> collection) {
return false == isBlank(collection);
}
/** /**
* 如果给定集合为空返回默认集合 * 如果给定集合为空返回默认集合
* *

View File

@ -306,7 +306,7 @@ public class CollUtilTest {
final List<String> removed = new ArrayList<>(); final List<String> removed = new ArrayList<>();
final ArrayList<String> filtered = CollUtil.filter(list, t -> { final ArrayList<String> filtered = CollUtil.filter(list, t -> {
if("a".equals(t)){ if ("a".equals(t)) {
removed.add(t); removed.add(t);
return false; return false;
} }
@ -767,7 +767,7 @@ public class CollUtilTest {
} }
@Test @Test
public void mapToMapTest(){ public void mapToMapTest() {
final HashMap<String, String> oldMap = new HashMap<>(); final HashMap<String, String> oldMap = new HashMap<>();
oldMap.put("a", "1"); oldMap.put("a", "1");
oldMap.put("b", "12"); oldMap.put("b", "12");
@ -778,9 +778,9 @@ public class CollUtilTest {
Map.Entry::getKey, Map.Entry::getKey,
entry -> Long.parseLong(entry.getValue())); entry -> Long.parseLong(entry.getValue()));
Assert.assertEquals(1L, (long)map.get("a")); Assert.assertEquals(1L, (long) map.get("a"));
Assert.assertEquals(12L, (long)map.get("b")); Assert.assertEquals(12L, (long) map.get("b"));
Assert.assertEquals(134L, (long)map.get("c")); Assert.assertEquals(134L, (long) map.get("c"));
} }
@Test @Test
@ -834,7 +834,7 @@ public class CollUtilTest {
final List<Long> result = CollUtil.subtractToList(list1, list2); final List<Long> result = CollUtil.subtractToList(list1, list2);
Assert.assertEquals(1, result.size()); Assert.assertEquals(1, result.size());
Assert.assertEquals(1L, (long)result.get(0)); Assert.assertEquals(1L, (long) result.get(0));
} }
@Test @Test
@ -845,7 +845,7 @@ public class CollUtilTest {
} }
@Test @Test
public void setValueByMapTest(){ public void setValueByMapTest() {
// https://gitee.com/dromara/hutool/pulls/482 // https://gitee.com/dromara/hutool/pulls/482
final List<Person> people = Arrays.asList( final List<Person> people = Arrays.asList(
new Person("aa", 12, "man", 1), new Person("aa", 12, "man", 1),
@ -886,13 +886,13 @@ public class CollUtilTest {
} }
@Test @Test
public void distinctTest(){ public void distinctTest() {
final ArrayList<Integer> distinct = CollUtil.distinct(ListUtil.of(5, 3, 10, 9, 0, 5, 10, 9)); final ArrayList<Integer> distinct = CollUtil.distinct(ListUtil.of(5, 3, 10, 9, 0, 5, 10, 9));
Assert.assertEquals(ListUtil.of(5, 3, 10, 9, 0), distinct); Assert.assertEquals(ListUtil.of(5, 3, 10, 9, 0), distinct);
} }
@Test @Test
public void distinctByFunctionTest(){ public void distinctByFunctionTest() {
final List<Person> people = Arrays.asList( final List<Person> people = Arrays.asList(
new Person("aa", 12, "man", 1), new Person("aa", 12, "man", 1),
new Person("bb", 13, "woman", 2), new Person("bb", 13, "woman", 2),
@ -941,8 +941,7 @@ public class CollUtilTest {
final List<String> list = CollUtil.unionAll(list1, list2, list3); final List<String> list = CollUtil.unionAll(list1, list2, list3);
Assert.assertNotNull(list); Assert.assertNotNull(list);
@SuppressWarnings("ConfusingArgumentToVarargsMethod") @SuppressWarnings("ConfusingArgumentToVarargsMethod") final List<String> resList2 = CollUtil.unionAll(null, null, null);
final List<String> resList2 = CollUtil.unionAll(null, null, null);
Assert.assertNotNull(resList2); Assert.assertNotNull(resList2);
} }
@ -973,8 +972,7 @@ public class CollUtilTest {
public void unionAllOtherIsNullTest() { public void unionAllOtherIsNullTest() {
final List<Integer> list1 = CollectionUtil.newArrayList(1, 2, 2, 3, 3); final List<Integer> list1 = CollectionUtil.newArrayList(1, 2, 2, 3, 3);
final List<Integer> list2 = CollectionUtil.newArrayList(1, 2, 3); final List<Integer> list2 = CollectionUtil.newArrayList(1, 2, 3);
@SuppressWarnings("ConfusingArgumentToVarargsMethod") @SuppressWarnings("ConfusingArgumentToVarargsMethod") final List<Integer> list = CollUtil.unionAll(list1, list2, null);
final List<Integer> list = CollUtil.unionAll(list1, list2, null);
Assert.assertNotNull(list); Assert.assertNotNull(list);
Assert.assertArrayEquals( Assert.assertArrayEquals(
CollectionUtil.newArrayList(1, 2, 2, 3, 3, 1, 2, 3).toArray(), CollectionUtil.newArrayList(1, 2, 2, 3, 3, 1, 2, 3).toArray(),
@ -1042,32 +1040,9 @@ public class CollUtilTest {
} }
@Test @Test
public void getFirstTest(){ public void getFirstTest() {
final List<?> nullList = null; final List<?> nullList = null;
final Object first = CollUtil.getFirst(nullList); final Object first = CollUtil.getFirst(nullList);
Assert.assertNull(first); Assert.assertNull(first);
} }
@Test
public void blankTest() {
List<String> strs = new ArrayList<>();
strs.add(null);
strs.add("");
strs.add("");
boolean c = CollUtil.isBlank(strs);
Assert.assertEquals(true, c );
List<String> arrs = new ArrayList<>();
arrs.add(null);
arrs.add("");
arrs.add(" ");
arrs.add("");
arrs.add(" a ");
boolean d = CollUtil.isNotBlank(arrs);
Assert.assertEquals(true, d );
}
} }

View File

@ -51,7 +51,7 @@ public class ExcelSaxReadTest {
@Test @Test
public void excel03Test() { public void excel03Test() {
Excel03SaxReader reader = new Excel03SaxReader(createRowHandler()); final Excel03SaxReader reader = new Excel03SaxReader(createRowHandler());
reader.read("aaa.xls", 1); reader.read("aaa.xls", 1);
// Console.log("Sheet index: [{}], Sheet name: [{}]", reader.getSheetIndex(), reader.getSheetName()); // Console.log("Sheet index: [{}], Sheet name: [{}]", reader.getSheetIndex(), reader.getSheetName());
@ -60,7 +60,7 @@ public class ExcelSaxReadTest {
@Test @Test
public void excel03ByNameTest() { public void excel03ByNameTest() {
Excel03SaxReader reader = new Excel03SaxReader(createRowHandler()); final Excel03SaxReader reader = new Excel03SaxReader(createRowHandler());
reader.read("aaa.xls", "校园入学"); reader.read("aaa.xls", "校园入学");
reader.read("aaa.xls", "sheetName:校园入学"); reader.read("aaa.xls", "sheetName:校园入学");
} }
@ -68,7 +68,7 @@ public class ExcelSaxReadTest {
@Test(expected = POIException.class) @Test(expected = POIException.class)
public void excel03ByNameErrorTest() { public void excel03ByNameErrorTest() {
// sheet名称不存在则报错 // sheet名称不存在则报错
Excel03SaxReader reader = new Excel03SaxReader(createRowHandler()); final Excel03SaxReader reader = new Excel03SaxReader(createRowHandler());
reader.read("aaa.xls", "校园入学1"); reader.read("aaa.xls", "校园入学1");
} }
@ -120,12 +120,12 @@ public class ExcelSaxReadTest {
ExcelUtil.readBySax("d:/test/test.xlsx", -1, new RowHandler() { ExcelUtil.readBySax("d:/test/test.xlsx", -1, new RowHandler() {
@Override @Override
public void handleCell(int sheetIndex, long rowIndex, int cellIndex, Object value, CellStyle xssfCellStyle) { public void handleCell(final int sheetIndex, final long rowIndex, final int cellIndex, final Object value, final CellStyle xssfCellStyle) {
Console.log("{} {} {}", rowIndex, cellIndex, value); Console.log("{} {} {}", rowIndex, cellIndex, value);
} }
@Override @Override
public void handle(int sheetIndex, long rowIndex, List<Object> rowCells) { public void handle(final int sheetIndex, final long rowIndex, final List<Object> rowCells) {
} }
} }
@ -133,17 +133,16 @@ public class ExcelSaxReadTest {
} }
@Test @Test
@Ignore
public void handle03CellTest() { public void handle03CellTest() {
ExcelUtil.readBySax("d:/test/test.xls", -1, new RowHandler() { ExcelUtil.readBySax("test.xls", -1, new RowHandler() {
@Override @Override
public void handleCell(int sheetIndex, long rowIndex, int cellIndex, Object value, CellStyle xssfCellStyle) { public void handleCell(final int sheetIndex, final long rowIndex, final int cellIndex, final Object value, final CellStyle xssfCellStyle) {
Console.log("{} {} {}", rowIndex, cellIndex, value); //Console.log("{} {} {}", rowIndex, cellIndex, value);
} }
@Override @Override
public void handle(int sheetIndex, long rowIndex, List<Object> rowCells) { public void handle(final int sheetIndex, final long rowIndex, final List<Object> rowCells) {
} }
} }
); );
@ -151,11 +150,11 @@ public class ExcelSaxReadTest {
@Test @Test
public void formulaRead03Test() { public void formulaRead03Test() {
List<Object> rows = new ArrayList<>(); final List<Object> rows = new ArrayList<>();
ExcelUtil.readBySax("data_for_sax_test.xls", -1, (i, i1, list) -> { ExcelUtil.readBySax("data_for_sax_test.xls", -1, (i, i1, list) -> {
if(list.size() > 1){ if (list.size() > 1) {
rows.add(list.get(1)); rows.add(list.get(1));
} else{ } else {
rows.add(""); rows.add("");
} }
}); });
@ -164,7 +163,7 @@ public class ExcelSaxReadTest {
@Test @Test
public void formulaRead07Test() { public void formulaRead07Test() {
List<Object> rows = new ArrayList<>(); final List<Object> rows = new ArrayList<>();
ExcelUtil.readBySax("data_for_sax_test.xlsx", 0, (i, i1, list) -> ExcelUtil.readBySax("data_for_sax_test.xlsx", 0, (i, i1, list) ->
rows.add(list.get(1))); rows.add(list.get(1)));
@ -174,7 +173,7 @@ public class ExcelSaxReadTest {
@Test @Test
public void dateReadXlsTest() { public void dateReadXlsTest() {
List<String> rows = new ArrayList<>(); final List<String> rows = new ArrayList<>();
ExcelUtil.readBySax("data_for_sax_test.xls", 0, ExcelUtil.readBySax("data_for_sax_test.xls", 0,
(i, i1, list) -> rows.add(StrUtil.toString(list.get(0))) (i, i1, list) -> rows.add(StrUtil.toString(list.get(0)))
); );
@ -188,7 +187,7 @@ public class ExcelSaxReadTest {
@Test @Test
public void dateReadXlsxTest() { public void dateReadXlsxTest() {
List<String> rows = new ArrayList<>(); final List<String> rows = new ArrayList<>();
ExcelUtil.readBySax("data_for_sax_test.xlsx", 0, ExcelUtil.readBySax("data_for_sax_test.xlsx", 0,
(i, i1, list) -> rows.add(StrUtil.toString(list.get(0))) (i, i1, list) -> rows.add(StrUtil.toString(list.get(0)))
); );
@ -211,7 +210,7 @@ public class ExcelSaxReadTest {
@Test @Test
@Ignore @Ignore
public void readBlankTest() { public void readBlankTest() {
File file = new File("D:/test/b.xlsx"); final File file = new File("D:/test/b.xlsx");
ExcelUtil.readBySax(file, 0, (sheetIndex, rowIndex, rowList) -> rowList.forEach(Console::log)); ExcelUtil.readBySax(file, 0, (sheetIndex, rowIndex, rowList) -> rowList.forEach(Console::log));
@ -220,7 +219,7 @@ public class ExcelSaxReadTest {
@Test @Test
@Ignore @Ignore
public void readXlsmTest(){ public void readXlsmTest() {
ExcelUtil.readBySax("d:/test/WhiteListTemplate.xlsm", -1, ExcelUtil.readBySax("d:/test/WhiteListTemplate.xlsm", -1,
(sheetIndex, rowIndex, rowlist) -> Console.log("[{}] [{}] {}", sheetIndex, rowIndex, rowlist)); (sheetIndex, rowIndex, rowlist) -> Console.log("[{}] [{}] {}", sheetIndex, rowIndex, rowlist));
} }

View File

@ -1,19 +1,10 @@
package cn.hutool.poi.excel; package cn.hutool.poi.excel;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.cell.CellLocation; import cn.hutool.poi.excel.cell.CellLocation;
import cn.hutool.poi.excel.sax.handler.RowHandler; import cn.hutool.poi.excel.sax.handler.RowHandler;
import org.apache.poi.ss.usermodel.CellStyle;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@ -59,39 +50,35 @@ public class ExcelUtilTest {
@Test @Test
public void readAndWriteTest() { public void readAndWriteTest() {
ExcelReader reader = ExcelUtil.getReader("aaa.xlsx"); final ExcelReader reader = ExcelUtil.getReader("aaa.xlsx");
ExcelWriter writer = reader.getWriter(); final ExcelWriter writer = reader.getWriter();
writer.writeCellValue(1, 2, "设置值"); writer.writeCellValue(1, 2, "设置值");
writer.close(); writer.close();
} }
@Test @Test
public void getReaderByBookFilePathAndSheetNameTest() { public void getReaderByBookFilePathAndSheetNameTest() {
ExcelReader reader = ExcelUtil.getReader("aaa.xlsx", "12"); final ExcelReader reader = ExcelUtil.getReader("aaa.xlsx", "12");
List<Map<String, Object>> list = reader.readAll(); final List<Map<String, Object>> list = reader.readAll();
reader.close(); reader.close();
Assert.assertEquals(1L, list.get(1).get("鞋码")); Assert.assertEquals(1L, list.get(1).get("鞋码"));
} }
@Test @Test
public void doAfterAllAnalysedTest() { public void doAfterAllAnalysedTest() {
String path = "readBySax.xls"; final String path = "readBySax.xls";
AtomicInteger doAfterAllAnalysedTime = new AtomicInteger(0); final AtomicInteger doAfterAllAnalysedTime = new AtomicInteger(0);
try{ ExcelUtil.readBySax(path, -1, new RowHandler() {
ExcelUtil.readBySax(path, -1, new RowHandler() { @Override
@Override public void handle(final int sheetIndex, final long rowIndex, final List<Object> rowCells) {
public void handle(int sheetIndex, long rowIndex, List<Object> rowCells) { //Console.log("sheetIndex={};rowIndex={},rowCells={}",sheetIndex,rowIndex,rowCells);
System.out.println(StrUtil.format("sheetIndex={};rowIndex={},rowCells={}",sheetIndex,rowIndex,rowCells)); }
}
@Override @Override
public void doAfterAllAnalysed() { public void doAfterAllAnalysed() {
doAfterAllAnalysedTime.addAndGet(1); doAfterAllAnalysedTime.addAndGet(1);
} }
}); });
}catch (Exception ex){
ex.printStackTrace();
}
//总共2个sheet页读取所有sheet时一共执行doAfterAllAnalysed2次 //总共2个sheet页读取所有sheet时一共执行doAfterAllAnalysed2次
Assert.assertEquals(2, doAfterAllAnalysedTime.intValue()); Assert.assertEquals(2, doAfterAllAnalysedTime.intValue());
} }