Skip to content

Commit

Permalink
Merge pull request #295 from ozlerhakan/4.1.2
Browse files Browse the repository at this point in the history
Patch release 4.1.2
  • Loading branch information
ozlerhakan committed Sep 25, 2023
2 parents 60ce106 + 188bc7f commit 64bdf56
Show file tree
Hide file tree
Showing 13 changed files with 243 additions and 101 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.github.ozlerhakan</groupId>
<artifactId>poiji</artifactId>
<version>4.1.1</version>
<version>4.1.2</version>
<packaging>jar</packaging>

<name>poiji</name>
Expand Down
37 changes: 27 additions & 10 deletions src/main/java/com/poiji/bind/mapping/HSSFUnmarshaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,27 @@ private <T> T tailSetFieldValue(Row currentRow, Class<? super T> type, T instanc
throw new PoijiMultiRowException("Problem(s) occurred while reading data", errors);
}

Map<String, String> excelUnknownCellsMap = StreamSupport
.stream(Spliterators.spliteratorUnknownSize(currentRow.cellIterator(), Spliterator.ORDERED), false)
.filter(cell -> indexToTitle.size() != 0)
.filter(cell -> !mappedColumnIndices.contains(cell.getColumnIndex()))
.filter(cell -> !cell.toString().isEmpty())
.collect(Collectors.toMap(
cell -> indexToTitle.get(cell.getColumnIndex()),
Object::toString));
if (unknownCells.isEmpty()) {
return instance;
}

unknownCells.forEach(field -> setFieldData(instance, field, excelUnknownCellsMap));
if (!indexToTitle.isEmpty()) {
Map<String, String> excelUnknownCellsMap = StreamSupport
.stream(Spliterators.spliteratorUnknownSize(currentRow.cellIterator(), Spliterator.ORDERED), false)
.filter(cell -> !mappedColumnIndices.contains(cell.getColumnIndex()))
.collect(Collectors.toMap(
cell -> indexToTitle.get(cell.getColumnIndex()),
Object::toString));
unknownCells.forEach(field -> setFieldData(instance, field, excelUnknownCellsMap));
} else {
Map<String, String> excelUnknownCellsMap = StreamSupport
.stream(Spliterators.spliteratorUnknownSize(currentRow.cellIterator(), Spliterator.ORDERED), false)
.filter(cell -> !mappedColumnIndices.contains(cell.getColumnIndex()))
.collect(Collectors.toMap(
cell -> valueOf(cell.getColumnIndex()),
Object::toString));
unknownCells.forEach(field -> setFieldData(instance, field, excelUnknownCellsMap));
}

return instance;
}
Expand Down Expand Up @@ -271,7 +282,7 @@ private <T> void constructTypeValue(Row currentRow, T instance, Field field,
cell.setCellStyle(null);
}
String value;
if (options.isRawData() && cell.getCellType() == CellType.NUMERIC) {
if (options.isRawData() && isCellNumeric(cell)) {
value = NumberToTextConverter.toText(cell.getNumericCellValue());
} else {
value = dataFormatter.formatCellValue(cell, baseFormulaEvaluator);
Expand All @@ -285,6 +296,12 @@ private <T> void constructTypeValue(Row currentRow, T instance, Field field,
}
}

private boolean isCellNumeric(Cell cell) {
return (cell.getCellType() == CellType.NUMERIC ||
(cell.getCellType() == CellType.FORMULA &&
cell.getCachedFormulaResultType() == CellType.NUMERIC));
}

private <T> void setFieldData(T instance, Field field, Object data) {
try {
field.setAccessible(true);
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/com/poiji/bind/mapping/PoijiHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,15 @@ private boolean setValue(String content, Class<? super T> type, int column) {
} else {
excelUnknownCellsMap = (Map<String, String>) field.get(instance);
}
excelUnknownCellsMap.put(indexToTitle.get(column), content);
String index = indexToTitle.get(column);
if (index == null) {
excelUnknownCellsMap.put(valueOf(column), content);
} else {
excelUnknownCellsMap.put(indexToTitle.get(column), content);
}
} catch (IllegalAccessException e) {
throw new IllegalCastException("Could not read content of field " + field.getName() + " on Object {" + instance + "}");
throw new IllegalCastException("Could not read content of field " + field.getName()
+ " on Object {" + instance + "}");
}
}
});
Expand Down Expand Up @@ -167,7 +173,7 @@ private boolean setValue(Field field, int column, String content, Object ins) {
excelCellNameAnnotations.add(excelCellName);
final String titleName = formatting.transform(options, excelCellName.value());
final Integer titleColumn = titleToIndex.get(titleName);
//Fix both columns mapped to name passing this condition below
// Fix both columns mapped to name passing this condition below
if (titleColumn != null && titleColumn == column) {
Object o = casting.castValue(field, content, internalRow, column, options);
ReflectUtil.setFieldData(field, o, ins);
Expand Down Expand Up @@ -234,7 +240,7 @@ private String getTitleNameForMap(String cellContent, int columnIndex) {

@Override
public void headerFooter(String text, boolean isHeader, String tagName) {
//no-op
// no-op
}

@Override
Expand Down
12 changes: 9 additions & 3 deletions src/test/java/com/poiji/deserialize/FromExcelSheetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public void shouldMapXLSSheet() throws IOException {
assertThat(result, notNullValue());
CellFormatModel cellFormatModel = result.get(1);
assertThat(cellFormatModel.getAge(), is(40));
workbook.close();
}

@Test
Expand All @@ -51,21 +52,23 @@ public void shouldMapXLSXSheet() throws IOException {
assertThat(result, notNullValue());
CellFormatModel cellFormatModel = result.get(1);
assertThat(cellFormatModel.getAge(), is(40));
workbook.close();
}


@Test
public void shouldMapXLSXSheetWithOptions() throws IOException {

File file = new File("src/test/resources/employees_format.xlsx");
FileInputStream fileInputStream = new FileInputStream(file);
Workbook workbook = new XSSFWorkbook(fileInputStream);
Sheet sheet = workbook.getSheetAt(0);
List<CellFormatModel> result = Poiji.fromExcel(sheet, CellFormatModel.class, PoijiOptions.PoijiOptionsBuilder.settings().build());
List<CellFormatModel> result = Poiji.fromExcel(sheet, CellFormatModel.class,
PoijiOptions.PoijiOptionsBuilder.settings().build());

assertThat(result, notNullValue());
CellFormatModel cellFormatModel = result.get(1);
assertThat(cellFormatModel.getAge(), is(40));
workbook.close();
}

@Test
Expand All @@ -76,7 +79,9 @@ public void shouldMapXLSXSheetWithOptionsConsumer() throws IOException {
Workbook workbook = new XSSFWorkbook(fileInputStream);
Sheet sheet = workbook.getSheetAt(0);

Poiji.fromExcel(sheet, CellFormatModel.class, PoijiOptions.PoijiOptionsBuilder.settings().build(), this::printout);
Poiji.fromExcel(sheet, CellFormatModel.class, PoijiOptions.PoijiOptionsBuilder.settings().build(),
this::printout);
workbook.close();
}

private void printout(CellFormatModel instance) {
Expand All @@ -91,5 +96,6 @@ public void shouldMapXLSXSheetError() throws IOException {
Workbook workbook = new SXSSFWorkbook(xssfWorkbook);
Sheet sheet = workbook.getSheetAt(0);
Poiji.fromExcel(sheet, CellFormatModel.class);
workbook.close();
}
}
7 changes: 7 additions & 0 deletions src/test/java/com/poiji/deserialize/PoijiOptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ public void shouldPrintDateTimeFormatter() {
assertThat(options.dateFormatter().toString(), equalTo(formatter.toString()));
}

@Test
public void shouldPrintDateFormatter() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/M/yyyy HH:mm:ss");
PoijiOptions options = PoijiOptions.PoijiOptionsBuilder.settings().dateTimeFormatter(formatter).build();
assertThat(options.dateTimeFormatter().toString(), equalTo(formatter.toString()));
}

@Test
public void shouldUseUsLocaleWhenNotSpecified() {
PoijiOptions options = PoijiOptions.PoijiOptionsBuilder.settings().build();
Expand Down
46 changes: 46 additions & 0 deletions src/test/java/com/poiji/deserialize/RawValueFormulaTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.poiji.deserialize;

import static org.hamcrest.core.Is.is;
import static org.hamcrest.MatcherAssert.assertThat;

import com.poiji.bind.Poiji;
import com.poiji.deserialize.model.RowModelFormula;
import com.poiji.option.PoijiOptions;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

/**
* Created by jmorgan on 12.09.2023
*/
@RunWith(Parameterized.class)
public class RawValueFormulaTest {

private String path;

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

@Parameterized.Parameters(name = "{index}: ({0})={1}")
public static Iterable<Object[]> queries() {
return Arrays.asList(new Object[][] {
{ "src/test/resources/raw_value_formula.xls" },
{ "src/test/resources/raw_value_formula.xlsx" },
});
}

@Test
public void shouldMapCalculations() {
PoijiOptions options = PoijiOptions.PoijiOptionsBuilder.settings().headerCount(0).rawData(true).build();
List<RowModelFormula> models = Poiji.fromExcel(new File(path), RowModelFormula.class, options);

for (RowModelFormula model : models) {
assertThat(model.getCurrencyValue(), is(123.45D));
assertThat(model.getFormulaValue(), is(246.90D));
}
}
}
8 changes: 4 additions & 4 deletions src/test/java/com/poiji/deserialize/RawValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.List;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;

/**
* Created by hakan on 02/08/2018
Expand All @@ -28,9 +28,9 @@ public RawValueTest(String path) {

@Parameterized.Parameters(name = "{index}: ({0})={1}")
public static Iterable<Object[]> queries() {
return Arrays.asList(new Object[][]{
{"src/test/resources/raw_value.xls"},
{"src/test/resources/raw_value.xlsx"},
return Arrays.asList(new Object[][] {
{ "src/test/resources/raw_value.xls" },
{ "src/test/resources/raw_value.xlsx" },
});
}

Expand Down
Loading

0 comments on commit 64bdf56

Please sign in to comment.