260403_01_ENH: 엑셀 매퍼에 스트리밍 옵션 추가하고 기존의 오버로딩 메소드간 연관관계 끊음
This commit is contained in:
@@ -17,6 +17,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.function.UnaryOperator;
|
||||
import org.apache.poi.openxml4j.opc.OPCPackage;
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.RichTextString;
|
||||
@@ -622,39 +623,45 @@ public final class ExcelMapper {
|
||||
* ========================================================= */
|
||||
|
||||
public static void readStreaming(Path path, RowMapHandler handler) {
|
||||
readStreaming(path, DEFAULT_SHEET_INDEX, DEFAULT_HEADER_ROW_INDEX, handler);
|
||||
readStreaming(path, StreamingReadOptions.DEFAULT, handler);
|
||||
}
|
||||
|
||||
public static void readStreaming(Path path, int headerRowIndex, RowMapHandler handler) {
|
||||
readStreaming(path, DEFAULT_SHEET_INDEX, headerRowIndex, handler);
|
||||
readStreaming(path, createStreamingReadOptions(null, DEFAULT_SHEET_INDEX, headerRowIndex), handler);
|
||||
}
|
||||
|
||||
public static void readStreaming(Path path, String sheetName, int headerRowIndex, RowMapHandler handler) {
|
||||
validateHeaderRowIndex(headerRowIndex);
|
||||
|
||||
try (OPCPackage pkg = OPCPackage.open(path.toFile())) {
|
||||
readStreaming(pkg, sheetName, null, headerRowIndex, handler);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Streaming 엑셀 읽기 중 오류", e);
|
||||
}
|
||||
readStreaming(path, createStreamingReadOptions(sheetName, DEFAULT_SHEET_INDEX, headerRowIndex), handler);
|
||||
}
|
||||
|
||||
public static void readStreaming(Path path, int sheetIndex, int headerRowIndex, RowMapHandler handler) {
|
||||
validateHeaderRowIndex(headerRowIndex);
|
||||
readStreaming(path, createStreamingReadOptions(null, sheetIndex, headerRowIndex), handler);
|
||||
}
|
||||
|
||||
public static void readStreaming(
|
||||
Path path,
|
||||
StreamingReadOptions options,
|
||||
RowMapHandler handler
|
||||
) {
|
||||
StreamingReadOptions validatedOptions = validateStreamingReadOptions(options);
|
||||
|
||||
try (OPCPackage pkg = OPCPackage.open(path.toFile())) {
|
||||
readStreaming(pkg, null, sheetIndex, headerRowIndex, handler);
|
||||
readStreaming(pkg, validatedOptions, handler);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Streaming 엑셀 읽기 중 오류", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void readStreaming(InputStream inputStream, RowMapHandler handler) {
|
||||
readStreaming(inputStream, DEFAULT_SHEET_INDEX, DEFAULT_HEADER_ROW_INDEX, handler);
|
||||
readStreaming(inputStream, StreamingReadOptions.DEFAULT, handler);
|
||||
}
|
||||
|
||||
public static void readStreaming(InputStream inputStream, int headerRowIndex, RowMapHandler handler) {
|
||||
readStreaming(inputStream, DEFAULT_SHEET_INDEX, headerRowIndex, handler);
|
||||
readStreaming(
|
||||
inputStream,
|
||||
createStreamingReadOptions(null, DEFAULT_SHEET_INDEX, headerRowIndex),
|
||||
handler
|
||||
);
|
||||
}
|
||||
|
||||
public static void readStreaming(
|
||||
@@ -663,13 +670,11 @@ public final class ExcelMapper {
|
||||
int headerRowIndex,
|
||||
RowMapHandler handler
|
||||
) {
|
||||
validateHeaderRowIndex(headerRowIndex);
|
||||
|
||||
try (OPCPackage pkg = OPCPackage.open(inputStream)) {
|
||||
readStreaming(pkg, sheetName, null, headerRowIndex, handler);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Streaming 엑셀 읽기 중 오류", e);
|
||||
}
|
||||
readStreaming(
|
||||
inputStream,
|
||||
createStreamingReadOptions(sheetName, DEFAULT_SHEET_INDEX, headerRowIndex),
|
||||
handler
|
||||
);
|
||||
}
|
||||
|
||||
public static void readStreaming(
|
||||
@@ -678,10 +683,18 @@ public final class ExcelMapper {
|
||||
int headerRowIndex,
|
||||
RowMapHandler handler
|
||||
) {
|
||||
validateHeaderRowIndex(headerRowIndex);
|
||||
readStreaming(inputStream, createStreamingReadOptions(null, sheetIndex, headerRowIndex), handler);
|
||||
}
|
||||
|
||||
public static void readStreaming(
|
||||
InputStream inputStream,
|
||||
StreamingReadOptions options,
|
||||
RowMapHandler handler
|
||||
) {
|
||||
StreamingReadOptions validatedOptions = validateStreamingReadOptions(options);
|
||||
|
||||
try (OPCPackage pkg = OPCPackage.open(inputStream)) {
|
||||
readStreaming(pkg, null, sheetIndex, headerRowIndex, handler);
|
||||
readStreaming(pkg, validatedOptions, handler);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Streaming 엑셀 읽기 중 오류", e);
|
||||
}
|
||||
@@ -689,19 +702,17 @@ public final class ExcelMapper {
|
||||
|
||||
private static void readStreaming(
|
||||
OPCPackage pkg,
|
||||
String sheetName,
|
||||
Integer sheetIndex,
|
||||
int headerRowIndex,
|
||||
StreamingReadOptions options,
|
||||
RowMapHandler handler
|
||||
) throws Exception {
|
||||
XSSFReader reader = new XSSFReader(pkg);
|
||||
SharedStringsTable sst = (SharedStringsTable) reader.getSharedStringsTable();
|
||||
|
||||
XMLReader parser = XMLReaderFactory.createXMLReader();
|
||||
SheetToMapHandler sheetHandler = new SheetToMapHandler(sst, handler, headerRowIndex);
|
||||
SheetToMapHandler sheetHandler = new SheetToMapHandler(sst, handler, options);
|
||||
parser.setContentHandler(sheetHandler);
|
||||
|
||||
try (InputStream sheetIS = openTargetSheet(reader, sheetName, sheetIndex)) {
|
||||
try (InputStream sheetIS = openTargetSheet(reader, options)) {
|
||||
parser.parse(new InputSource(sheetIS));
|
||||
}
|
||||
}
|
||||
@@ -711,39 +722,40 @@ public final class ExcelMapper {
|
||||
* ========================================================= */
|
||||
|
||||
public static List<String> readHeaderStreaming(Path path) {
|
||||
return readHeaderStreaming(path, DEFAULT_SHEET_INDEX, DEFAULT_HEADER_ROW_INDEX);
|
||||
return readHeaderStreaming(path, StreamingReadOptions.DEFAULT);
|
||||
}
|
||||
|
||||
public static List<String> readHeaderStreaming(Path path, int headerRowIndex) {
|
||||
return readHeaderStreaming(path, DEFAULT_SHEET_INDEX, headerRowIndex);
|
||||
return readHeaderStreaming(path, createStreamingReadOptions(null, DEFAULT_SHEET_INDEX, headerRowIndex));
|
||||
}
|
||||
|
||||
public static List<String> readHeaderStreaming(Path path, int sheetIndex, int headerRowIndex) {
|
||||
validateHeaderRowIndex(headerRowIndex);
|
||||
|
||||
try (OPCPackage pkg = OPCPackage.open(path.toFile())) {
|
||||
return readHeaderStreaming(pkg, null, sheetIndex, headerRowIndex);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Streaming 엑셀 헤더 읽기 중 오류", e);
|
||||
}
|
||||
return readHeaderStreaming(path, createStreamingReadOptions(null, sheetIndex, headerRowIndex));
|
||||
}
|
||||
|
||||
public static List<String> readHeaderStreaming(Path path, String sheetName, int headerRowIndex) {
|
||||
validateHeaderRowIndex(headerRowIndex);
|
||||
return readHeaderStreaming(path, createStreamingReadOptions(sheetName, DEFAULT_SHEET_INDEX, headerRowIndex));
|
||||
}
|
||||
|
||||
public static List<String> readHeaderStreaming(Path path, StreamingReadOptions options) {
|
||||
StreamingReadOptions validatedOptions = validateStreamingReadOptions(options);
|
||||
|
||||
try (OPCPackage pkg = OPCPackage.open(path.toFile())) {
|
||||
return readHeaderStreaming(pkg, sheetName, null, headerRowIndex);
|
||||
return readHeaderStreaming(pkg, validatedOptions);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Streaming 엑셀 헤더 읽기 중 오류", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> readHeaderStreaming(InputStream inputStream) {
|
||||
return readHeaderStreaming(inputStream, DEFAULT_SHEET_INDEX, DEFAULT_HEADER_ROW_INDEX);
|
||||
return readHeaderStreaming(inputStream, StreamingReadOptions.DEFAULT);
|
||||
}
|
||||
|
||||
public static List<String> readHeaderStreaming(InputStream inputStream, int headerRowIndex) {
|
||||
return readHeaderStreaming(inputStream, DEFAULT_SHEET_INDEX, headerRowIndex);
|
||||
return readHeaderStreaming(
|
||||
inputStream,
|
||||
createStreamingReadOptions(null, DEFAULT_SHEET_INDEX, headerRowIndex)
|
||||
);
|
||||
}
|
||||
|
||||
public static List<String> readHeaderStreaming(
|
||||
@@ -751,13 +763,7 @@ public final class ExcelMapper {
|
||||
int sheetIndex,
|
||||
int headerRowIndex
|
||||
) {
|
||||
validateHeaderRowIndex(headerRowIndex);
|
||||
|
||||
try (OPCPackage pkg = OPCPackage.open(inputStream)) {
|
||||
return readHeaderStreaming(pkg, null, sheetIndex, headerRowIndex);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Streaming 엑셀 헤더 읽기 중 오류", e);
|
||||
}
|
||||
return readHeaderStreaming(inputStream, createStreamingReadOptions(null, sheetIndex, headerRowIndex));
|
||||
}
|
||||
|
||||
public static List<String> readHeaderStreaming(
|
||||
@@ -765,30 +771,35 @@ public final class ExcelMapper {
|
||||
String sheetName,
|
||||
int headerRowIndex
|
||||
) {
|
||||
validateHeaderRowIndex(headerRowIndex);
|
||||
return readHeaderStreaming(
|
||||
inputStream,
|
||||
createStreamingReadOptions(sheetName, DEFAULT_SHEET_INDEX, headerRowIndex)
|
||||
);
|
||||
}
|
||||
|
||||
public static List<String> readHeaderStreaming(
|
||||
InputStream inputStream,
|
||||
StreamingReadOptions options
|
||||
) {
|
||||
StreamingReadOptions validatedOptions = validateStreamingReadOptions(options);
|
||||
|
||||
try (OPCPackage pkg = OPCPackage.open(inputStream)) {
|
||||
return readHeaderStreaming(pkg, sheetName, null, headerRowIndex);
|
||||
return readHeaderStreaming(pkg, validatedOptions);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Streaming 엑셀 헤더 읽기 중 오류", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<String> readHeaderStreaming(
|
||||
OPCPackage pkg,
|
||||
String sheetName,
|
||||
Integer sheetIndex,
|
||||
int headerRowIndex
|
||||
) {
|
||||
private static List<String> readHeaderStreaming(OPCPackage pkg, StreamingReadOptions options) {
|
||||
try {
|
||||
XSSFReader reader = new XSSFReader(pkg);
|
||||
SharedStringsTable sst = (SharedStringsTable) reader.getSharedStringsTable();
|
||||
|
||||
XMLReader parser = XMLReaderFactory.createXMLReader();
|
||||
HeaderOnlyHandler headerHandler = new HeaderOnlyHandler(sst, headerRowIndex);
|
||||
HeaderOnlyHandler headerHandler = new HeaderOnlyHandler(sst, options);
|
||||
parser.setContentHandler(headerHandler);
|
||||
|
||||
try (InputStream sheetIS = openTargetSheet(reader, sheetName, sheetIndex)) {
|
||||
try (InputStream sheetIS = openTargetSheet(reader, options)) {
|
||||
try {
|
||||
parser.parse(new InputSource(sheetIS));
|
||||
} catch (StopParsingException ignored) {
|
||||
@@ -804,15 +815,15 @@ public final class ExcelMapper {
|
||||
|
||||
private static InputStream openTargetSheet(
|
||||
XSSFReader reader,
|
||||
String sheetName,
|
||||
Integer sheetIndex
|
||||
StreamingReadOptions options
|
||||
) throws Exception {
|
||||
Iterator<InputStream> iterator = reader.getSheetsData();
|
||||
if (!(iterator instanceof SheetIterator sheetIterator)) {
|
||||
throw new IllegalStateException("SheetIterator를 사용할 수 없습니다.");
|
||||
}
|
||||
|
||||
String normalizedSheetName = sheetName == null ? null : normalizeSheetName(sheetName);
|
||||
String normalizedSheetName = hasText(options.sheetName()) ? normalizeSheetName(options.sheetName()) : null;
|
||||
Integer sheetIndex = normalizedSheetName == null ? options.sheetIndex() : null;
|
||||
int currentIndex = 0;
|
||||
|
||||
while (sheetIterator.hasNext()) {
|
||||
@@ -902,6 +913,39 @@ public final class ExcelMapper {
|
||||
}
|
||||
}
|
||||
|
||||
public static StreamingReadOptions createStreamingReadOptions(
|
||||
String sheetName,
|
||||
int sheetIndex,
|
||||
int headerRowIndex
|
||||
) {
|
||||
return new StreamingReadOptions(sheetName, sheetIndex, headerRowIndex, UnaryOperator.identity());
|
||||
}
|
||||
|
||||
private static StreamingReadOptions validateStreamingReadOptions(StreamingReadOptions options) {
|
||||
StreamingReadOptions validatedOptions = options == null ? StreamingReadOptions.DEFAULT : options;
|
||||
validateHeaderRowIndex(validatedOptions.headerRowIndex());
|
||||
return validatedOptions;
|
||||
}
|
||||
|
||||
private static boolean hasText(String value) {
|
||||
return value != null && !value.isBlank();
|
||||
}
|
||||
|
||||
private static String normalizeStreamingHeader(
|
||||
String header,
|
||||
int columnIndex,
|
||||
UnaryOperator<String> headerNormalizer
|
||||
) {
|
||||
String normalizedHeader = header;
|
||||
if (normalizedHeader != null) {
|
||||
normalizedHeader = headerNormalizer.apply(normalizedHeader);
|
||||
}
|
||||
if (normalizedHeader == null || normalizedHeader.isBlank()) {
|
||||
normalizedHeader = "COL_" + columnIndex;
|
||||
}
|
||||
return normalizedHeader;
|
||||
}
|
||||
|
||||
private static int toPoiRowIndex(int headerRowIndex) {
|
||||
return headerRowIndex - 1;
|
||||
}
|
||||
@@ -1221,6 +1265,7 @@ public final class ExcelMapper {
|
||||
private final SharedStringsTable sst;
|
||||
private final RowMapHandler handler;
|
||||
private final int headerRowIndex; // 1-based
|
||||
private final UnaryOperator<String> headerNormalizer;
|
||||
private final Map<Integer, String> cellMap = new HashMap<>();
|
||||
|
||||
private int currentRowNum;
|
||||
@@ -1230,11 +1275,12 @@ public final class ExcelMapper {
|
||||
private boolean isValueOpen;
|
||||
private List<String> headers = new ArrayList<>();
|
||||
|
||||
SheetToMapHandler(SharedStringsTable sst, RowMapHandler handler, int headerRowIndex) {
|
||||
validateHeaderRowIndex(headerRowIndex);
|
||||
SheetToMapHandler(SharedStringsTable sst, RowMapHandler handler, StreamingReadOptions options) {
|
||||
StreamingReadOptions validatedOptions = validateStreamingReadOptions(options);
|
||||
this.sst = sst;
|
||||
this.handler = handler;
|
||||
this.headerRowIndex = headerRowIndex;
|
||||
this.headerRowIndex = validatedOptions.headerRowIndex();
|
||||
this.headerNormalizer = validatedOptions.headerNormalizer();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -1320,11 +1366,7 @@ public final class ExcelMapper {
|
||||
headers = new ArrayList<>(maxCol + 1);
|
||||
|
||||
for (int c = 0; c <= maxCol; c++) {
|
||||
String header = cellMap.get(c);
|
||||
if (header == null || header.isBlank()) {
|
||||
header = "COL_" + c;
|
||||
}
|
||||
headers.add(header);
|
||||
headers.add(normalizeStreamingHeader(cellMap.get(c), c, headerNormalizer));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1345,6 +1387,7 @@ public final class ExcelMapper {
|
||||
private static class HeaderOnlyHandler extends DefaultHandler {
|
||||
private final SharedStringsTable sst;
|
||||
private final int headerRowIndex; // 1-based
|
||||
private final UnaryOperator<String> headerNormalizer;
|
||||
private final Map<Integer, String> cellMap = new HashMap<>();
|
||||
|
||||
private int currentRowNum;
|
||||
@@ -1354,10 +1397,11 @@ public final class ExcelMapper {
|
||||
private boolean isValueOpen;
|
||||
private List<String> headers = new ArrayList<>();
|
||||
|
||||
HeaderOnlyHandler(SharedStringsTable sst, int headerRowIndex) {
|
||||
validateHeaderRowIndex(headerRowIndex);
|
||||
HeaderOnlyHandler(SharedStringsTable sst, StreamingReadOptions options) {
|
||||
StreamingReadOptions validatedOptions = validateStreamingReadOptions(options);
|
||||
this.sst = sst;
|
||||
this.headerRowIndex = headerRowIndex;
|
||||
this.headerRowIndex = validatedOptions.headerRowIndex();
|
||||
this.headerNormalizer = validatedOptions.headerNormalizer();
|
||||
}
|
||||
|
||||
List<String> getHeaders() {
|
||||
@@ -1421,11 +1465,7 @@ public final class ExcelMapper {
|
||||
List<String> result = new ArrayList<>(maxCol + 1);
|
||||
|
||||
for (int c = 0; c <= maxCol; c++) {
|
||||
String header = cellMap.get(c);
|
||||
if (header == null || header.isBlank()) {
|
||||
header = "COL_" + c;
|
||||
}
|
||||
result.add(header);
|
||||
result.add(normalizeStreamingHeader(cellMap.get(c), c, headerNormalizer));
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -1455,4 +1495,4 @@ public final class ExcelMapper {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.kri.common.feature.excel;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
public record StreamingReadOptions(
|
||||
String sheetName,
|
||||
int sheetIndex,
|
||||
int headerRowIndex,
|
||||
UnaryOperator<String> headerNormalizer
|
||||
) {
|
||||
|
||||
private static final int DEFAULT_SHEET_INDEX = 0;
|
||||
private static final int DEFAULT_HEADER_ROW_INDEX = 1;
|
||||
|
||||
public static final StreamingReadOptions DEFAULT =
|
||||
new StreamingReadOptions(
|
||||
null,
|
||||
DEFAULT_SHEET_INDEX,
|
||||
DEFAULT_HEADER_ROW_INDEX,
|
||||
UnaryOperator.identity()
|
||||
);
|
||||
|
||||
public StreamingReadOptions {
|
||||
headerNormalizer = Objects.requireNonNullElseGet(headerNormalizer, UnaryOperator::identity);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user