Skip to content

Commit

Permalink
Merge 63729f6 into 311da05
Browse files Browse the repository at this point in the history
  • Loading branch information
wilds committed Oct 2, 2020
2 parents 311da05 + 63729f6 commit 2aba097
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 16 deletions.
13 changes: 7 additions & 6 deletions src/main/java/com/poiji/bind/mapping/HSSFUnmarshaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.poiji.option.PoijiOptions;
import com.poiji.util.AnnotationUtil;
import com.poiji.util.ReflectUtil;
import com.poiji.util.StringUtil;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
Expand Down Expand Up @@ -125,9 +127,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 = StringUtil.getTitleName(options, cell.getStringCellValue());
columnIndexPerTitle.put(titleName, columnIndex);
titlePerColumnIndex.put(columnIndex, getTitleNameForMap(titleName, columnIndex));
}
Expand All @@ -136,6 +136,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 Down Expand Up @@ -211,9 +214,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 = StringUtil.getTitleName(options, excelCellName.value());
Integer column = columnIndexPerTitle.get(titleName);
annotationDetail.setColumn(column);
}
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/poiji/bind/mapping/PoijiHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.poiji.option.PoijiOptions;
import com.poiji.util.AnnotationUtil;
import com.poiji.util.ReflectUtil;
import com.poiji.util.StringUtil;

import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler.SheetContentsHandler;
import org.apache.poi.xssf.usermodel.XSSFComment;
Expand Down Expand Up @@ -122,7 +124,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 Down Expand Up @@ -169,9 +171,7 @@ private boolean setValue(Field field, int column, String content, Object ins) {
if (excelCellName != null) {
excelCellNames.add(excelCellName);
Class<?> fieldType = field.getType();
final String titleName = options.getCaseInsensitive()
? excelCellName.value().toLowerCase()
: excelCellName.value();
final String titleName = StringUtil.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) {
Expand Down Expand Up @@ -210,10 +210,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(StringUtil.getTitleName(options, formattedValue), column);
titlePerColumnIndex.put(column, getTitleNameForMap(formattedValue, column));
}
if (row + 1 <= options.skip()) {
Expand All @@ -228,6 +225,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
2 changes: 1 addition & 1 deletion src/main/java/com/poiji/config/DefaultCasting.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ private LocalDateTime localDateTimeValue(String value, String sheetName, int row
}


private Object enumValue(String value, String sheetName, int row, int col, Class type) {
private Object enumValue(String value, String sheetName, int row, int col, Class<?> type) {
return Arrays.stream(type.getEnumConstants())
.filter(o -> ((Enum<?>) o).name().equals(value))
.findFirst()
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/poiji/option/PoijiOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public final class PoijiOptions {
private int headerStart;
private String sheetName;
private boolean caseInsensitive;
private boolean ignoreWhitespaces;
private PoijiLogCellFormat poijiLogCellFormat;
private PoijiNumberFormat numberFormat;
private boolean namedHeaderMandatory;
Expand Down Expand Up @@ -221,6 +222,15 @@ private PoijiOptions setCaseInsensitive(final boolean caseInsensitive) {
return this;
}

public boolean getIgnoreWhitespaces() {
return ignoreWhitespaces;
}

private PoijiOptions setIgnoreWhitespaces(final boolean ignoreWhitespaces) {
this.ignoreWhitespaces = ignoreWhitespaces;
return this;
}

public boolean getNamedHeaderMandatory() {
return namedHeaderMandatory;
}
Expand Down Expand Up @@ -260,6 +270,7 @@ public static class PoijiOptionsBuilder {
private int limit = 0;
private String sheetName;
private boolean caseInsensitive;
private boolean ignoreWhitespaces;
private boolean namedHeaderMandatory;
private boolean disabledXLSXNumberCellFormat;

Expand Down Expand Up @@ -356,6 +367,7 @@ public PoijiOptions build() {
.setPoijiLogCellFormat(cellFormat)
.setPoijiNumberFormat(numberFormat)
.setCaseInsensitive(caseInsensitive)
.setIgnoreWhitespaces(ignoreWhitespaces)
.setNamedHeaderMandatory(namedHeaderMandatory)
.disableXLSXNumberCellFormat(disabledXLSXNumberCellFormat);
}
Expand Down Expand Up @@ -527,6 +539,17 @@ public PoijiOptionsBuilder caseInsensitive(final boolean caseInsensitive) {
return this;
}

/**
* Ignore white space before and after column names for annotation {@link ExcelCellName}.
* Default - false.
*
* @param ignoreWhitespaces true or false
*/
public PoijiOptionsBuilder ignoreWhitespaces(final boolean ignoreWhitespaces) {
this.ignoreWhitespaces = ignoreWhitespaces;
return this;
}

/**
* Add cell format option to see each internal cell's excel format for files ending with xlsx format.
* This option should be enabled for debugging purpose.
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/poiji/util/AnnotationUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ public static <T> void validateMandatoryNameColumns(PoijiOptions options,

Set<String> missingHeaders = excelCellNames.stream()
.filter(excelCellName -> headerNames.stream()
.noneMatch(title -> comparator.test(excelCellName.value(), title)))
.noneMatch(title -> comparator.test(
options.getIgnoreWhitespaces() ? excelCellName.value().trim() : excelCellName.value(),
options.getIgnoreWhitespaces() ? title.trim() : title
)))
.map(ExcelCellName::value)
.collect(Collectors.toSet());

Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/poiji/util/StringUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.poiji.util;

import com.poiji.option.PoijiOptions;

public final class StringUtil {

private StringUtil() {
}

public static String getTitleName(PoijiOptions options, String value) {
String titleName = options.getCaseInsensitive()
? value.toLowerCase()
: value;
if (options.getIgnoreWhitespaces())
return titleName.trim();
return titleName;
}

}
63 changes: 63 additions & 0 deletions src/test/java/com/poiji/deserialize/IgnoreWhitespacesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.poiji.deserialize;

import com.poiji.bind.Poiji;
import com.poiji.deserialize.model.byname.OrgWithUnknownCellsByName;
import com.poiji.option.PoijiOptions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.io.File;
import java.util.List;

import static java.util.Arrays.asList;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;

@RunWith(Parameterized.class)
public class IgnoreWhitespacesTest {

private String path;

public IgnoreWhitespacesTest(String path) {
this.path = path;
}

@Parameterized.Parameters
public static List<String> excel() {
return asList("src/test/resources/ignore_whitespaces.xlsx", "src/test/resources/ignore_whitespaces.xls");
}

@Test
public void ignoreWhitespaceColumnNames() {
List<OrgWithUnknownCellsByName> organisations = Poiji.fromExcel(
new File(path),
OrgWithUnknownCellsByName.class,
PoijiOptions.PoijiOptionsBuilder.settings()
.sheetName("Organisation")
.caseInsensitive(true)
.ignoreWhitespaces(true)
.build()
);

assertThat(organisations, notNullValue());
assertThat(organisations.size(), is(2));

OrgWithUnknownCellsByName firstRow = organisations.stream()
.filter(org -> org.getId().equals("CrEaTe"))
.findFirst()
.get();
assertThat(firstRow.getUnknownCells().size(), is(1));
assertThat(firstRow.getUnknownCells().get("Region"), is("EMEA"));


OrgWithUnknownCellsByName secondRow = organisations.stream()
.filter(org -> org.getId().equals("8d9e6430-8626-4556-8004-079085d2df2d"))
.findFirst()
.get();
assertThat(secondRow.getUnknownCells().size(), is(1));
assertThat(secondRow.getUnknownCells().get("Region"), is("NA"));
}

}
Binary file added src/test/resources/ignore_whitespaces.xls
Binary file not shown.
Binary file added src/test/resources/ignore_whitespaces.xlsx
Binary file not shown.

0 comments on commit 2aba097

Please sign in to comment.