Skip to content

Commit

Permalink
Merge pull request #224 from ozlerhakan/3.1.5
Browse files Browse the repository at this point in the history
3.1.5
  • Loading branch information
ozlerhakan committed Dec 12, 2021
2 parents 65b78ab + 540586c commit 6b4f61e
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/maven.yml
@@ -1,6 +1,6 @@
name: Poiji Build

on: [push]
on: [push, pull_request]

jobs:
build:
Expand Down
10 changes: 5 additions & 5 deletions README.adoc
Expand Up @@ -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.

Expand All @@ -24,15 +24,15 @@ In your Maven/Gradle project, first add the corresponding dependency:
<dependency>
<groupId>com.github.ozlerhakan</groupId>
<artifactId>poiji</artifactId>
<version>3.1.4</version>
<version>3.1.5</version>
</dependency>
----

.gradle
[source,groovy]
----
dependencies {
compile 'com.github.ozlerhakan:poiji:3.1.4'
compile 'com.github.ozlerhakan:poiji:3.1.5'
}
----

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.ozlerhakan</groupId>
<artifactId>poiji</artifactId>
<version>3.1.4</version>
<version>3.1.5</version>
<packaging>jar</packaging>

<name>poiji</name>
Expand Down Expand Up @@ -45,7 +45,7 @@
</issueManagement>

<properties>
<apache.poi.version>5.0.0</apache.poi.version>
<apache.poi.version>5.1.0</apache.poi.version>
<junit.version>4.12</junit.version>
<project.build.resourceEncoding>UTF-8</project.build.resourceEncoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/com/poiji/bind/mapping/HSSFUnmarshaller.java
Expand Up @@ -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;
Expand Down Expand Up @@ -69,9 +68,6 @@ abstract class HSSFUnmarshaller extends PoijiWorkBook implements Unmarshaller {
@Override
public <T> void unmarshal(Class<T> type, Consumer<? super T> consumer) {
HSSFWorkbook workbook = (HSSFWorkbook) workbook();
if (workbook == null) {
throw new PoijiException("The workbook is null.");
}
Optional<String> maybeSheetName = this.getSheetName(type, options);

baseFormulaEvaluator = HSSFFormulaEvaluator.create(workbook, null, null);
Expand Down
14 changes: 0 additions & 14 deletions 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;
Expand All @@ -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
*/
Expand Down Expand Up @@ -72,20 +69,13 @@ 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.
String cellRef = attributes.getValue("r");
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;
Expand All @@ -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);
}
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/poiji/option/PoijiOptions.java
Expand Up @@ -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;
}

Expand All @@ -290,6 +292,9 @@ public boolean isRawData() {
}

private PoijiOptions setRawData(boolean rawData) {
if (rawData) {
setLocale(Locale.US);
}
this.rawData = rawData;
return this;
}
Expand Down Expand Up @@ -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
*/
Expand Down
60 changes: 60 additions & 0 deletions 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<Object[]> 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<RowModelDouble> 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);
}
}
}
}
17 changes: 17 additions & 0 deletions 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;
}

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

0 comments on commit 6b4f61e

Please sign in to comment.