Skip to content

Commit

Permalink
Merge 2e2ba00 into 1ee23f5
Browse files Browse the repository at this point in the history
  • Loading branch information
ozlerhakan committed Oct 11, 2020
2 parents 1ee23f5 + 2e2ba00 commit f70b3ae
Show file tree
Hide file tree
Showing 28 changed files with 489 additions and 154 deletions.
8 changes: 6 additions & 2 deletions src/main/java/com/poiji/bind/mapping/HSSFPropertyFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public <T> T returnFromExcelFile(Class<T> type) {
try (OPCPackage open = OPCPackage.open(file, PackageAccess.READ)) {
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(open);
PropertyHandler propertyHandler = new PropertyHandler();
return propertyHandler.unmarshal(type, xssfWorkbook.getProperties());
T ret = propertyHandler.unmarshal(type, xssfWorkbook.getProperties());
xssfWorkbook.close();
return ret;
} catch (IOException | OpenXML4JException e) {
throw new PoijiException("Problem occurred while reading data", e);
}
Expand All @@ -54,7 +56,9 @@ public <T> T returnFromEncryptedFile(Class<T> type) {
try (OPCPackage open = OPCPackage.open(stream)) {
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(open);
PropertyHandler propertyHandler = new PropertyHandler();
return propertyHandler.unmarshal(type, xssfWorkbook.getProperties());
T ret = propertyHandler.unmarshal(type, xssfWorkbook.getProperties());
xssfWorkbook.close();
return ret;
} catch (IOException | OpenXML4JException e) {
IOUtils.closeQuietly(fs);
throw new PoijiException("Problem occurred while reading data", e);
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/poiji/bind/mapping/HSSFPropertyStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public <T> T returnFromExcelFile(Class<T> type) {
try (OPCPackage open = OPCPackage.open(inputStream)) {
PropertyHandler propertyHandler = new PropertyHandler();
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(open);
return propertyHandler.unmarshal(type, xssfWorkbook.getProperties());
T ret = propertyHandler.unmarshal(type, xssfWorkbook.getProperties());
xssfWorkbook.close();
return ret;
} catch (IOException | OpenXML4JException e) {
throw new PoijiException("Problem occurred while reading data", e);
}
Expand All @@ -52,7 +54,9 @@ public <T> T returnFromEncryptedFile(Class<T> type) {
try (OPCPackage open = OPCPackage.open(stream)) {
PropertyHandler propertyHandler = new PropertyHandler();
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(open);
return propertyHandler.unmarshal(type, xssfWorkbook.getProperties());
T ret = propertyHandler.unmarshal(type, xssfWorkbook.getProperties());
xssfWorkbook.close();
return ret;
} catch (IOException | OpenXML4JException e) {
IOUtils.closeQuietly(fs);
throw new PoijiException("Problem occurred while reading data", e);
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/com/poiji/bind/mapping/HSSFUnmarshaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import com.poiji.option.PoijiOptions;
import com.poiji.util.AnnotationUtil;
import com.poiji.util.ReflectUtil;
import com.poiji.util.Strings;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
Expand Down Expand Up @@ -49,6 +51,7 @@ abstract class HSSFUnmarshaller extends PoijiWorkBook implements Unmarshaller {
private final Map<Integer, String> caseSensitiveTitlePerColumnIndex;
private final int limit;
private int internalCount;
private HSSFFormulaEvaluator hssfFormulaEvaluator;

HSSFUnmarshaller(PoijiOptions options) {
this.options = options;
Expand All @@ -65,6 +68,7 @@ public <T> void unmarshal(Class<T> type, Consumer<? super T> consumer) {
HSSFWorkbook workbook = (HSSFWorkbook) workbook();
Optional<String> maybeSheetName = this.getSheetName(type, options);

hssfFormulaEvaluator = HSSFFormulaEvaluator.create(workbook, null, null);
Sheet sheet = this.getSheetToProcess(workbook, options, maybeSheetName.orElse(null));

int skip = options.skip();
Expand Down Expand Up @@ -125,9 +129,7 @@ private void loadColumnTitles(Sheet sheet, int maxPhysicalNumberOfRows) {
for (Cell cell : firstRow) {
final int columnIndex = cell.getColumnIndex();
caseSensitiveTitlePerColumnIndex.put(columnIndex, getTitleNameForMap(cell.getStringCellValue(), columnIndex));
final String titleName = options.getCaseInsensitive()
? cell.getStringCellValue().toLowerCase()
: cell.getStringCellValue();
final String titleName = Strings.getTitleName(options, cell.getStringCellValue());
columnIndexPerTitle.put(titleName, columnIndex);
titlePerColumnIndex.put(columnIndex, getTitleNameForMap(titleName, columnIndex));
}
Expand All @@ -136,6 +138,9 @@ private void loadColumnTitles(Sheet sheet, int maxPhysicalNumberOfRows) {

private String getTitleNameForMap(String cellContent, int columnIndex) {
String titleName;
if (options.getIgnoreWhitespaces()) {
cellContent = cellContent.trim();
}
if (titlePerColumnIndex.containsValue(cellContent)
|| cellContent.isEmpty()) {
titleName = cellContent + "@" + columnIndex;
Expand All @@ -157,7 +162,7 @@ private <T> T tailSetFieldValue(Row currentRow, Class<? super T> type, T instanc
for (Field field : type.getDeclaredFields()) {
if (field.getAnnotation(ExcelRow.class) != null) {
final int rowNum = currentRow.getRowNum();
final Object data = casting.castValue(field.getType(), valueOf(rowNum), rowNum, -1, options);
final Object data = casting.castValue(field, valueOf(rowNum), rowNum, -1, options);
setFieldData(instance, field, data);
} else if (field.getAnnotation(ExcelCellRange.class) != null) {

Expand Down Expand Up @@ -211,9 +216,7 @@ private FieldAnnotationDetail getFieldColumn(final Field field) {
} else {
ExcelCellName excelCellName = field.getAnnotation(ExcelCellName.class);
if (excelCellName != null) {
final String titleName = options.getCaseInsensitive()
? excelCellName.value().toLowerCase()
: excelCellName.value();
final String titleName = Strings.getTitleName(options, excelCellName.value());
Integer column = columnIndexPerTitle.get(titleName);
annotationDetail.setColumn(column);
}
Expand All @@ -228,8 +231,8 @@ private <T> void constructTypeValue(Row currentRow, T instance, Field field, Fie
if (annotationDetail.isDisabledCellFormat()) {
cell.setCellStyle(null);
}
String value = dataFormatter.formatCellValue(cell);
Object data = casting.castValue(field.getType(), value, currentRow.getRowNum(), annotationDetail.getColumn(), options);
String value = dataFormatter.formatCellValue(cell, hssfFormulaEvaluator);
Object data = casting.castValue(field, value, currentRow.getRowNum(), annotationDetail.getColumn(), options);
setFieldData(instance, field, data);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.poiji.bind.mapping;

import com.poiji.bind.PoijiFile;
import com.poiji.bind.Unmarshaller;
import com.poiji.exception.PoijiException;
import com.poiji.option.PoijiOptions;
import org.apache.poi.ss.usermodel.Workbook;
Expand All @@ -13,7 +12,7 @@
*
* Created by hakan on 16/01/2017.
*/
final class HSSFUnmarshallerFile extends HSSFUnmarshaller implements Unmarshaller {
final class HSSFUnmarshallerFile extends HSSFUnmarshaller {

private final PoijiFile<?> poijiFile;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.poiji.bind.mapping;

import com.poiji.bind.PoijiInputStream;
import com.poiji.bind.Unmarshaller;
import com.poiji.exception.PoijiException;
import com.poiji.option.PoijiOptions;
import org.apache.poi.ss.usermodel.Workbook;
Expand All @@ -12,7 +11,7 @@
/**
* Created by hakan on 08/03/2018.
*/
final class HSSFUnmarshallerStream extends HSSFUnmarshaller implements Unmarshaller {
final class HSSFUnmarshallerStream extends HSSFUnmarshaller {

private final PoijiInputStream<?> poijiInputStream;

Expand Down
54 changes: 23 additions & 31 deletions src/main/java/com/poiji/bind/mapping/PoijiHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.poiji.bind.mapping;

import com.poiji.annotation.DisableCellFormatXLS;
import com.poiji.annotation.ExcelCell;
import com.poiji.annotation.ExcelCellName;
import com.poiji.annotation.ExcelCellRange;
Expand All @@ -11,6 +10,7 @@
import com.poiji.option.PoijiOptions;
import com.poiji.util.AnnotationUtil;
import com.poiji.util.ReflectUtil;
import com.poiji.util.Strings;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler.SheetContentsHandler;
import org.apache.poi.xssf.usermodel.XSSFComment;
Expand All @@ -33,20 +33,20 @@
*/
final class PoijiHandler<T> implements SheetContentsHandler {
private T instance;
private Consumer<? super T> consumer;
private final Consumer<? super T> consumer;
private int internalRow;
private int internalCount;
private int limit;
private Class<T> type;
private PoijiOptions options;
private final int limit;
private final Class<T> type;
private final PoijiOptions options;
private final Casting casting;
private Map<String, Integer> columnIndexPerTitle;
private Map<Integer, String> titlePerColumnIndex;
private final Map<String, Integer> columnIndexPerTitle;
private final Map<Integer, String> titlePerColumnIndex;
// New maps used to speed up computing and handle inner objects
private Map<String, Object> fieldInstances;
private Map<Integer, Field> columnToField;
private Map<Integer, Field> columnToSuperClassField;
private Set<ExcelCellName> excelCellNames;
private final Map<Integer, Field> columnToField;
private final Map<Integer, Field> columnToSuperClassField;
private final Set<ExcelCellName> excelCellNames;

PoijiHandler(Class<T> type, PoijiOptions options, Consumer<? super T> consumer) {
this.type = type;
Expand Down Expand Up @@ -74,7 +74,7 @@ private void setFieldValue(String content, Class<? super T> subclass, int column
* Using this to hold inner objects that will be mapped to the main object
**/
private Object getInstance(Field field) {
Object ins = null;
Object ins;
if (fieldInstances.containsKey(field.getName())) {
ins = fieldInstances.get(field.getName());
} else {
Expand All @@ -84,19 +84,20 @@ private Object getInstance(Field field) {
return ins;
}

@SuppressWarnings("unchecked")
private boolean setValue(String content, Class<? super T> type, int column) {
Stream.of(type.getDeclaredFields())
.filter(field -> field.getAnnotation(ExcelUnknownCells.class) == null)
.forEach(field -> {
ExcelRow excelRow = field.getAnnotation(ExcelRow.class);
if (excelRow != null) {
Object o = casting.castValue(field.getType(), valueOf(internalRow), internalRow, column, options);
Object o = casting.castValue(field, valueOf(internalRow), internalRow, column, options);
ReflectUtil.setFieldData(field, o, instance);
columnToField.put(-1, field);
}
ExcelCellRange range = field.getAnnotation(ExcelCellRange.class);
if (range != null) {
Object ins = null;
Object ins;
ins = getInstance(field);
for (Field f : field.getType().getDeclaredFields()) {
if (setValue(f, column, content, ins)) {
Expand All @@ -122,7 +123,7 @@ private boolean setValue(String content, Class<? super T> type, int column) {
excelUnknownCellsMap = new HashMap<>();
ReflectUtil.setFieldData(field, excelUnknownCellsMap, instance);
} else {
excelUnknownCellsMap = (Map) field.get(instance);
excelUnknownCellsMap = (Map<String, String>) field.get(instance);
}
excelUnknownCellsMap.put(titlePerColumnIndex.get(column), content);
} catch (IllegalAccessException e) {
Expand All @@ -133,7 +134,7 @@ private boolean setValue(String content, Class<? super T> type, int column) {
// For ExcelRow annotation
if (columnToField.containsKey(-1)) {
Field field = columnToField.get(-1);
Object o = casting.castValue(field.getType(), valueOf(internalRow), internalRow, column, options);
Object o = casting.castValue(field, valueOf(internalRow), internalRow, column, options);
ReflectUtil.setFieldData(field, o, instance);
}
if (columnToField.containsKey(column) && columnToSuperClassField.containsKey(column)) {
Expand All @@ -151,31 +152,22 @@ private boolean setValue(String content, Class<? super T> type, int column) {

private boolean setValue(Field field, int column, String content, Object ins) {
ExcelCell index = field.getAnnotation(ExcelCell.class);
DisableCellFormatXLS disableCellFormat = field.getAnnotation(DisableCellFormatXLS.class);
boolean isCloseCellFormat = false;
if (disableCellFormat != null) {
isCloseCellFormat = disableCellFormat.value();
}

if (index != null) {
Class<?> fieldType = field.getType();
if (column == index.value()) {
Object o = casting.castValue(fieldType, content, internalRow, column, options);
Object o = casting.castValue(field, content, internalRow, column, options);
ReflectUtil.setFieldData(field, o, ins);
return true;
}
} else {
ExcelCellName excelCellName = field.getAnnotation(ExcelCellName.class);
if (excelCellName != null) {
excelCellNames.add(excelCellName);
Class<?> fieldType = field.getType();
final String titleName = options.getCaseInsensitive()
? excelCellName.value().toLowerCase()
: excelCellName.value();
final String titleName = Strings.getTitleName(options, excelCellName.value());
final Integer titleColumn = columnIndexPerTitle.get(titleName);
//Fix both columns mapped to name passing this condition below
if (titleColumn != null && titleColumn == column) {
Object o = casting.castValue(fieldType, content, internalRow, column, options);
Object o = casting.castValue(field, content, internalRow, column, options);
ReflectUtil.setFieldData(field, o, ins);
return true;
}
Expand Down Expand Up @@ -210,10 +202,7 @@ public void cell(String cellReference, String formattedValue, XSSFComment commen
int header = options.getHeaderStart();
int column = cellAddress.getColumn();
if (row == header) {
columnIndexPerTitle.put(
options.getCaseInsensitive() ? formattedValue.toLowerCase() : formattedValue,
column
);
columnIndexPerTitle.put(Strings.getTitleName(options, formattedValue), column);
titlePerColumnIndex.put(column, getTitleNameForMap(formattedValue, column));
}
if (row + 1 <= options.skip()) {
Expand All @@ -228,6 +217,9 @@ public void cell(String cellReference, String formattedValue, XSSFComment commen

private String getTitleNameForMap(String cellContent, int columnIndex) {
String titleName;
if (options.getIgnoreWhitespaces()) {
cellContent = cellContent.trim();
}
if (titlePerColumnIndex.containsValue(cellContent)
|| cellContent.isEmpty()) {
titleName = cellContent + "@" + columnIndex;
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/poiji/bind/mapping/PoijiLogCellFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,15 @@ public void setCellType(String cellType) {
this.cellType = cellType;
}

@Override
public String toString() {
return "InternalCellFormat{" +
"cellType='" + cellType + '\'' +
", formatIndex=" + formatIndex +
", formatString='" + formatString + '\'' +
", cellStypeStr='" + cellStypeStr + '\'' +
", cellAddress=" + cellAddress +
'}';
}
}
}
4 changes: 3 additions & 1 deletion src/main/java/com/poiji/config/Casting.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import com.poiji.option.PoijiOptions;

import java.lang.reflect.Field;

/**
* A casting interface to build a custom poiji configuration.
*
* Created by hakan on 22/01/2017.
*/
public interface Casting {

Object castValue(Class<?> fieldType, String value, int row, int column, PoijiOptions options);
Object castValue(Field fieldType, String value, int row, int column, PoijiOptions options);
}
Loading

0 comments on commit f70b3ae

Please sign in to comment.