diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index fd7b0a4..0dfda2e 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -1,6 +1,6 @@ name: Poiji Build -on: [push] +on: [push, pull_request] jobs: build: diff --git a/README.adoc b/README.adoc index d091991..9a5ce62 100644 --- a/README.adoc +++ b/README.adoc @@ -2,11 +2,11 @@ :toclevels: 2 = Poiji -:version: v3.1.4 -:branch: 3.1.4 +:version: v3.1.5 +:branch: 3.1.5 image:https://github.com/ozlerhakan/poiji/actions/workflows/maven.yml/badge.svg["Build Status"] image:https://app.codacy.com/project/badge/Grade/64f7e2cb9e604807b62334a4cfc3952d["Codacy code quality", link="https://www.codacy.com/gh/ozlerhakan/poiji/dashboard?utm_source=github.com&utm_medium=referral&utm_content=ozlerhakan/poiji&utm_campaign=Badge_Grade"] -image:https://codecov.io/gh/ozlerhakan/poiji/branch/{branch}/graph/badge.svg?token=MN6V6xOWBq["Codecov", link="https://codecov.io/gh/ozlerhakan/poiji"] image:https://img.shields.io/badge/apache.poi-5.0.0-brightgreen.svg[] image:https://app.fossa.com/api/projects/git%2Bgithub.com%2Fozlerhakan%2Fpoiji.svg?type=shield["FOSSA Status", link="https://app.fossa.com/projects/git%2Bgithub.com%2Fozlerhakan%2Fpoiji?ref=badge_shield"] +image:https://codecov.io/gh/ozlerhakan/poiji/branch/{branch}/graph/badge.svg?token=MN6V6xOWBq["Codecov", link="https://codecov.io/gh/ozlerhakan/poiji"] image:https://img.shields.io/badge/apache.poi-5.1.0-brightgreen.svg[] image:https://app.fossa.com/api/projects/git%2Bgithub.com%2Fozlerhakan%2Fpoiji.svg?type=shield["FOSSA Status", link="https://app.fossa.com/projects/git%2Bgithub.com%2Fozlerhakan%2Fpoiji?ref=badge_shield"] Poiji is a tiny thread-safe Java library that provides one way mapping from Excel sheets to Java classes. In a way it lets us convert each row of the specified excel data into Java objects. Poiji uses https://poi.apache.org/[Apache Poi] (the Java API for Microsoft Documents) under the hood to fulfill the mapping process. @@ -24,7 +24,7 @@ In your Maven/Gradle project, first add the corresponding dependency: com.github.ozlerhakan poiji - 3.1.4 + 3.1.5 ---- @@ -32,7 +32,7 @@ In your Maven/Gradle project, first add the corresponding dependency: [source,groovy] ---- dependencies { - compile 'com.github.ozlerhakan:poiji:3.1.4' + compile 'com.github.ozlerhakan:poiji:3.1.5' } ---- diff --git a/pom.xml b/pom.xml index c8494b3..101de05 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.github.ozlerhakan poiji - 3.1.4 + 3.1.5 jar poiji @@ -45,7 +45,7 @@ - 5.0.0 + 5.1.0 4.12 UTF-8 UTF-8 diff --git a/src/main/java/com/poiji/bind/mapping/HSSFUnmarshaller.java b/src/main/java/com/poiji/bind/mapping/HSSFUnmarshaller.java index c65ca9d..24ebff6 100644 --- a/src/main/java/com/poiji/bind/mapping/HSSFUnmarshaller.java +++ b/src/main/java/com/poiji/bind/mapping/HSSFUnmarshaller.java @@ -10,7 +10,6 @@ import com.poiji.config.Casting; import com.poiji.config.Formatting; import com.poiji.exception.IllegalCastException; -import com.poiji.exception.PoijiException; import com.poiji.option.PoijiOptions; import com.poiji.util.AnnotationUtil; import com.poiji.util.ReflectUtil; @@ -69,9 +68,6 @@ abstract class HSSFUnmarshaller extends PoijiWorkBook implements Unmarshaller { @Override public void unmarshal(Class type, Consumer consumer) { HSSFWorkbook workbook = (HSSFWorkbook) workbook(); - if (workbook == null) { - throw new PoijiException("The workbook is null."); - } Optional maybeSheetName = this.getSheetName(type, options); baseFormulaEvaluator = HSSFFormulaEvaluator.create(workbook, null, null); diff --git a/src/main/java/com/poiji/bind/mapping/XSSFSheetXMLPoijiHandler.java b/src/main/java/com/poiji/bind/mapping/XSSFSheetXMLPoijiHandler.java index fcf42a6..e5e8825 100644 --- a/src/main/java/com/poiji/bind/mapping/XSSFSheetXMLPoijiHandler.java +++ b/src/main/java/com/poiji/bind/mapping/XSSFSheetXMLPoijiHandler.java @@ -1,7 +1,6 @@ package com.poiji.bind.mapping; import com.poiji.option.PoijiOptions; -import org.apache.poi.ss.usermodel.BuiltinFormats; import org.apache.poi.ss.usermodel.DataFormatter; import org.apache.poi.ss.util.CellAddress; import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler; @@ -13,8 +12,6 @@ import org.xml.sax.SAXException; import org.xml.sax.ext.Attributes2Impl; -import static org.apache.poi.xssf.usermodel.XSSFRelation.NS_SPREADSHEETML; - /** * Created by hakan on 26.04.2020 */ @@ -72,9 +69,6 @@ public void startElement(String uri, String localName, String qName, Attributes if (this.cellFormat == null) { return; } - if (uri != null && !uri.equals(NS_SPREADSHEETML)) { - return; - } if ("c".equals(localName)) { // Set up defaults. @@ -82,10 +76,6 @@ public void startElement(String uri, String localName, String qName, Attributes String cellType = attributes.getValue("t"); String cellStyleStr = attributes.getValue("s"); CellAddress cellAddress = new CellAddress(cellRef); - if ("b".equals(cellType) || "e".equals(cellType) || "inlineStr".equals(cellType)) { - this.cellFormat.addFormat(cellAddress, (short) 0, null, cellType, cellStyleStr); - return; - } if ("s".equals(cellType) || "str".equals(cellType)) { this.cellFormat.addFormat(cellAddress, (short) 0, null, cellType, cellStyleStr); return; @@ -96,15 +86,11 @@ public void startElement(String uri, String localName, String qName, Attributes if (cellStyleStr != null) { int styleIndex = Integer.parseInt(cellStyleStr); style = stylesTable.getStyleAt(styleIndex); - } else if (stylesTable.getNumCellStyles() > 0) { - style = stylesTable.getStyleAt(0); } } if (style != null) { short formatIndex = style.getDataFormat(); String formatString = style.getDataFormatString(); - if (formatString == null) - formatString = BuiltinFormats.getBuiltinFormat(formatIndex); this.cellFormat.addFormat(cellAddress, formatIndex, formatString, cellType, cellStyleStr); } diff --git a/src/main/java/com/poiji/option/PoijiOptions.java b/src/main/java/com/poiji/option/PoijiOptions.java index 090c57d..73c6848 100644 --- a/src/main/java/com/poiji/option/PoijiOptions.java +++ b/src/main/java/com/poiji/option/PoijiOptions.java @@ -280,8 +280,10 @@ public Locale getLocale() { } private PoijiOptions setLocale(Locale locale) { - this.locale = locale; - LocaleUtil.setUserLocale(locale); + if (!this.rawData) { + this.locale = locale; + LocaleUtil.setUserLocale(locale); + } return this; } @@ -290,6 +292,9 @@ public boolean isRawData() { } private PoijiOptions setRawData(boolean rawData) { + if (rawData) { + setLocale(Locale.US); + } this.rawData = rawData; return this; } @@ -399,6 +404,7 @@ public PoijiOptionsBuilder preferNullOverDefault(boolean preferNullOverDefault) * Set the {@link Locale} used by Apache Poi and PoiJ. Default is {@link Locale#ENGLISH}. * This setting is only used by Apache Poi thread and PoiJ. See {@link org.apache.poi.util.LocaleUtil} * for more details. + * * @param locale Locale * @return this */ diff --git a/src/test/java/com/poiji/deserialize/RawValueLocaleTest.java b/src/test/java/com/poiji/deserialize/RawValueLocaleTest.java new file mode 100644 index 0000000..a515eb9 --- /dev/null +++ b/src/test/java/com/poiji/deserialize/RawValueLocaleTest.java @@ -0,0 +1,60 @@ +package com.poiji.deserialize; + +import com.poiji.bind.Poiji; +import com.poiji.deserialize.model.RowModelDouble; +import com.poiji.option.PoijiOptions; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.io.File; +import java.util.Arrays; +import java.util.List; +import java.util.Locale; + +import static org.junit.Assert.assertEquals; + +/** + * Created by beckerdennis on 07.12.2021 + */ +@RunWith(Parameterized.class) +public class RawValueLocaleTest { + + private final String path; + private final boolean isRawData; + + public RawValueLocaleTest(String path, boolean isRawData) { + this.path = path; + this.isRawData = isRawData; + } + + @Parameterized.Parameters(name = "{index}: ({0})={1}") + public static Iterable queries() { + return Arrays.asList(new Object[][]{ + {"src/test/resources/raw_value_locale.xls", true}, + {"src/test/resources/raw_value_locale.xlsx", true}, + {"src/test/resources/raw_value_locale.xls", false}, + {"src/test/resources/raw_value_locale.xlsx", false}, + }); + } + + @BeforeClass + public static void setLocaleGermany() { + Locale.setDefault(Locale.GERMANY); + } + + @Test + public void shouldMapRawValueWithDifferentLocale() { + PoijiOptions options = PoijiOptions.PoijiOptionsBuilder.settings().headerCount(0).setLocale(Locale.GERMANY).rawData(isRawData).build(); + List models = Poiji.fromExcel(new File(path), RowModelDouble.class, options); + + for (RowModelDouble model : models) { + if (isRawData) { + assertEquals(0.351D, model.getRowValue(), 0); + } else { + assertEquals(0.4D, model.getRowValue(), 0); + } + } + } +} diff --git a/src/test/java/com/poiji/deserialize/model/RowModelDouble.java b/src/test/java/com/poiji/deserialize/model/RowModelDouble.java new file mode 100644 index 0000000..8dd319a --- /dev/null +++ b/src/test/java/com/poiji/deserialize/model/RowModelDouble.java @@ -0,0 +1,17 @@ +package com.poiji.deserialize.model; + +import com.poiji.annotation.ExcelCell; + +/** + * Created by beckerdennis on 07.12.2021 + */ +public class RowModelDouble { + + @ExcelCell(0) + private double rowValue; + + public double getRowValue() { + return rowValue; + } + +} \ No newline at end of file diff --git a/src/test/resources/raw_value_locale.xls b/src/test/resources/raw_value_locale.xls new file mode 100644 index 0000000..2eb65e2 Binary files /dev/null and b/src/test/resources/raw_value_locale.xls differ diff --git a/src/test/resources/raw_value_locale.xlsx b/src/test/resources/raw_value_locale.xlsx new file mode 100644 index 0000000..f787665 Binary files /dev/null and b/src/test/resources/raw_value_locale.xlsx differ