-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
875 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
src/main/java/io/github/millij/poi/ss/handler/RowContentsAsMapHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package io.github.millij.poi.ss.handler; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import io.github.millij.poi.util.Spreadsheet; | ||
|
||
|
||
/** | ||
* SheetContentsHandler impl for reading row as {@link Map} | ||
* | ||
* @since 3.1.0 | ||
*/ | ||
public class RowContentsAsMapHandler extends AbstractSheetContentsHandler { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(RowContentsAsMapHandler.class); | ||
|
||
private final RowListener<Map<String, Object>> listener; | ||
|
||
private final int headerRowNum; | ||
private final Map<String, String> headerCellRefsMap; | ||
|
||
private final int lastRowNum; | ||
|
||
|
||
// Constructors | ||
// ------------------------------------------------------------------------ | ||
|
||
public RowContentsAsMapHandler(RowListener<Map<String, Object>> listener, int headerRowNum, int lastRowNum) { | ||
super(); | ||
|
||
// init | ||
this.listener = listener; | ||
|
||
this.headerRowNum = headerRowNum; | ||
this.headerCellRefsMap = new HashMap<>(); | ||
|
||
this.lastRowNum = lastRowNum; | ||
} | ||
|
||
|
||
// AbstractSheetContentsHandler Methods | ||
// ------------------------------------------------------------------------ | ||
|
||
@Override | ||
void beforeRowStart(final int rowNum) { | ||
try { | ||
// Row Callback | ||
listener.beforeRow(rowNum); | ||
} catch (Exception ex) { | ||
String errMsg = String.format("Error calling #beforeRow callback row - %d", rowNum); | ||
LOGGER.error(errMsg, ex); | ||
} | ||
} | ||
|
||
|
||
@Override | ||
void afterRowEnd(final int rowNum, final Map<String, Object> rowDataMap) { | ||
// Sanity Checks | ||
if (Objects.isNull(rowDataMap) || rowDataMap.isEmpty()) { | ||
LOGGER.debug("INVALID Row data Passed - Row #{}", rowNum); | ||
return; | ||
} | ||
|
||
// Skip rows before Header ROW and after Last ROW | ||
if (rowNum < headerRowNum || rowNum > lastRowNum) { | ||
return; | ||
} | ||
|
||
// Process Header ROW | ||
if (rowNum == headerRowNum) { | ||
final Map<String, String> headerCellRefs = this.asHeaderNameToCellRefMap(rowDataMap); | ||
headerCellRefsMap.putAll(headerCellRefs); | ||
return; | ||
} | ||
|
||
// Check for Column Definitions before processing NON-Header ROWs | ||
|
||
// Row As Bean | ||
final Map<String, Object> rowBean = Spreadsheet.rowAsMap(headerCellRefsMap, rowDataMap); | ||
if (Objects.isNull(rowBean)) { | ||
LOGGER.debug("Unable to construct Row data Bean object - Row #{}", rowNum); | ||
return; | ||
} | ||
|
||
// Row Callback | ||
try { | ||
listener.row(rowNum, rowBean); | ||
} catch (Exception ex) { | ||
String errMsg = String.format("Error calling #row callback row - %d, bean - %s", rowNum, rowBean); | ||
LOGGER.error(errMsg, ex); | ||
} | ||
} | ||
|
||
|
||
// Private Methods | ||
// ------------------------------------------------------------------------ | ||
|
||
private Map<String, String> asHeaderNameToCellRefMap(final Map<String, Object> headerRowData) { | ||
// Sanity checks | ||
if (Objects.isNull(headerRowData) || headerRowData.isEmpty()) { | ||
return new HashMap<>(); | ||
} | ||
|
||
// Get Bean Column definitions | ||
final Map<String, String> headerCellRefs = new HashMap<String, String>(); | ||
for (final String colRef : headerRowData.keySet()) { | ||
final Object header = headerRowData.get(colRef); | ||
|
||
final String headerName = Objects.isNull(header) ? "" : String.valueOf(header); | ||
headerCellRefs.put(headerName, colRef); | ||
} | ||
|
||
LOGGER.debug("Header Name to Cell Refs : {}", headerCellRefs); | ||
return headerCellRefs; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.