Skip to content

Commit

Permalink
update pr #57
Browse files Browse the repository at this point in the history
  • Loading branch information
ozlerhakan committed Sep 17, 2018
1 parent a5dcba6 commit edddc2f
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 94 deletions.
9 changes: 5 additions & 4 deletions README.adoc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
= Poiji
:version: v1.17.0
:version: v1.18.0

image:https://travis-ci.org/ozlerhakan/poiji.svg?branch=master["Build Status", link="https://travis-ci.org/ozlerhakan/poiji"] image:https://api.codacy.com/project/badge/Grade/6587e90886184da29a1b7c5634695c9d["Codacy code quality", link="https://www.codacy.com/app/ozlerhakan/poiji?utm_source=github.com&utm_medium=referral&utm_content=ozlerhakan/poiji&utm_campaign=Badge_Grade"] image:https://coveralls.io/repos/github/ozlerhakan/poiji/badge.svg?branch=master["Coverage Status", link="https://coveralls.io/github/ozlerhakan/poiji?branch=master"] image:https://img.shields.io/badge/apache.poi-3.17-brightgreen.svg[] image:https://img.shields.io/badge/gitter-join%20chat-blue.svg["Gitter", link="https://gitter.im/poiji/Lobby"] image:https://img.shields.io/badge/license-MIT-blue.svg[]

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

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

Expand All @@ -44,8 +44,9 @@ com.poiji.bind.Poiji#fromExcel(java.io.InputStream, com.poiji.exception.PoijiExc
.`PoijiOptions.PoijiOptionsBuilder` Structure
----
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#settings()
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#settings(int)
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#build()
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#dateLenient(boolean)
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#dateRegex(String)
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#datePattern(String)
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#dateTimeFormatter(java.time.format.DateTimeFormatter)
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#ignoreHiddenSheets(boolean)
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.ozlerhakan</groupId>
<artifactId>poiji</artifactId>
<version>1.17.0</version>
<version>1.18.0</version>
<packaging>jar</packaging>

<name>poiji</name>
Expand Down
39 changes: 15 additions & 24 deletions src/main/java/com/poiji/option/PoijiOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@ public final class PoijiOptions {
private int skip;
private int sheetIndex;
private String password;
private String dateRegex;
private String datePattern;
private boolean dateLenient;
private boolean trimCellValue;
private boolean ignoreHiddenSheets;
private boolean preferNullOverDefault;
private DateTimeFormatter dateTimeFormatter;
private boolean ignoreHiddenSheets;
private boolean trimCellValue;
//ISSUE #57
//specify regex pattern for date converstion, if set and does not match date set to null
private String dateRegex;
//to set simple date format to Lenient if wanted
private boolean dateLenient;

private PoijiOptions() {
super();
Expand Down Expand Up @@ -107,22 +104,20 @@ public int sheetIndex() {
return sheetIndex;
}

//ISSUE #57
public String getDateRegex() {
return dateRegex;
}

public PoijiOptions setDateRegex(String dateRegex) {
private PoijiOptions setDateRegex(String dateRegex) {
this.dateRegex = dateRegex;
return this;
}

//ISSUE #57
public boolean getDateLenient() {
return dateLenient;
}

public PoijiOptions setDateLenient(boolean dateLenient) {
private PoijiOptions setDateLenient(boolean dateLenient) {
this.dateLenient = dateLenient;
return this;
}
Expand All @@ -132,15 +127,13 @@ public static class PoijiOptionsBuilder {
private int skip = 1;
private int sheetIndex;
private String password;
private boolean preferNullOverDefault = false;
private String dateRegex;
private boolean dateLenient;
private boolean trimCellValue;
private boolean ignoreHiddenSheets;
private boolean preferNullOverDefault;
private String datePattern = DEFAULT_DATE_PATTERN;
private DateTimeFormatter dateTimeFormatter = DEFAULT_DATE_TIME_FORMATTER;
//ISSUE #55
private boolean ignoreHiddenSheets = false;
private boolean trimCellValue = false;
//ISSUE #57
private String dateRegex = null;
private boolean dateLenient = true;

private PoijiOptionsBuilder() {
}
Expand Down Expand Up @@ -272,7 +265,6 @@ public PoijiOptionsBuilder trimCellValue(boolean trimCellValue) {
return this;
}

//ISSUE #57
/**
* Date regex, if would like to specify a regex patter the date must be
* in, e.g.\\d{2}/\\d{1}/\\d{4}.
Expand All @@ -285,13 +277,12 @@ public PoijiOptionsBuilder dateRegex(String dateRegex) {
return this;
}

//ISSUE #57
/**
* If would like to set the simple date format is lenient option, use to
* set how strict the date formating must be, defaults to lenient true
* as that is the default for simple date format.
* If the simple date format is lenient, use to
* set how strict the date formatting must be, defaults to lenient false.
* It works only for java.util.Date.
*
* @param dateLenient
* @param dateLenient true or false
* @return this
*/
public PoijiOptionsBuilder dateLenient(boolean dateLenient) {
Expand Down
39 changes: 11 additions & 28 deletions src/main/java/com/poiji/util/Casting.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,35 +100,30 @@ private Float floatValue(String value, PoijiOptions options) {
}
}

//ISSUE #57
//a default date method basied on option settings
private Date defaultDate(PoijiOptions options) {
if (Boolean.TRUE.equals(options.preferNullOverDefault())) {
return null;
} else {
Calendar calendar = Calendar.getInstance();
return calendar.getTime();
}
Calendar calendar = Calendar.getInstance();
return calendar.getTime();
}

//ISSUE #57
//a default date method basied on option settings
private LocalDate defaultLocalDate(PoijiOptions options) {
if (Boolean.TRUE.equals(options.preferNullOverDefault())) {
return null;
} else {
return LocalDate.now();
}
return LocalDate.now();
}

private Date dateValue(String value, PoijiOptions options) {

//ISSUE #57
//if a date regex has been speficied then it wont be null
//if a date regex has been specified then it wont be null
//so then make sure the string matches the pattern
//if it doenst, fall back to default
//if it doesn't, fall back to default
//else continue to turn string into java date
//the reason for this is sometime java will manage to parse a string to a date object

//the reason for this is sometime Java will manage to parse a string to a date object
//without any exceptions but since the string was not an exact match you get a very strange date
if (options.getDateRegex() != null && !value.matches(options.getDateRegex())) {
return defaultDate(options);
Expand All @@ -139,37 +134,27 @@ private Date dateValue(String value, PoijiOptions options) {
return sdf.parse(value);
} catch (ParseException e) {
return defaultDate(options);
// if (Boolean.TRUE.equals(options.preferNullOverDefault())) {
// return null;
// } else {
// Calendar calendar = Calendar.getInstance();
// return calendar.getTime();
// }
}
}
}

private LocalDate localDateValue(String value, PoijiOptions options) {

//ISSUE #57
//if a date regex has been speficied then it wont be null
//if a date regex has been specified then it wont be null
//so then make sure the string matches the pattern
//if it doenst, fall back to default
//if it doesn't, fall back to default
//else continue to turn string into java date

//the reason for this is sometime java will manage to parse a string to a date object
//without any exceptions but since the string was not an exact match you get a very strange date
if (options.getDateRegex() != null && !value.matches(options.getDateRegex())) {
return defaultLocalDate(options);
} else {

try {
return LocalDate.parse(value, options.dateTimeFormatter());
} catch (DateTimeParseException e) {
//ISSUE #57
//originally returned null, and did not take into consideration 'preferNullOverDefault' setting
//updated to honour options setting
return defaultLocalDate(options);
// return null;
}
}
}
Expand All @@ -185,8 +170,6 @@ private Object enumValue(String value, Class type) {
public Object castValue(Class<?> fieldType, String value, PoijiOptions options) {
Object o;

//ISSUE #55 : additioanl functionality
//if set in options, will trim any leading and trailing white spaces.
if (options.trimCellValue()) {
value = value.trim();
}
Expand Down Expand Up @@ -228,7 +211,7 @@ public Object castValue(Class<?> fieldType, String value, PoijiOptions options)
o = enumValue(value, fieldType);

} else {
if (value.length() == 0) {
if (value.isEmpty()) {
if (Boolean.TRUE.equals(options.preferNullOverDefault())) {
o = null;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public DeserializersDateLenientTest(String path, List<EmployeeExtended> expected
public static Iterable<Object[]> queries() throws Exception {
return Arrays.asList(new Object[][]{
{"src/test/resources/date_lenient.xlsx", unmarshalling(), null},
{"src/test/resources/date_lenient.xls", unmarshalling(), null},});
{"src/test/resources/date_lenient.xls", unmarshalling(), null}});
}

@Test
Expand Down Expand Up @@ -107,39 +107,6 @@ public void lenientFalse() {
}
}

@Test
public void lenientNotSet() {

try {
PoijiOptions options = PoijiOptions.PoijiOptionsBuilder.settings().datePattern("yyyy-MM-dd").build();

List<EmployeeExtended> actualEmployees = Poiji.fromExcel(new File(path), EmployeeExtended.class, options);

assertThat(actualEmployees, notNullValue());
assertThat(actualEmployees.size(), not(0));
assertThat(actualEmployees.size(), is(expectedEmployess.size()));

EmployeeExtended actualEmployee1 = actualEmployees.get(0);
EmployeeExtended actualEmployee2 = actualEmployees.get(1);
EmployeeExtended actualEmployee3 = actualEmployees.get(2);

EmployeeExtended expectedEmployee1 = expectedEmployess.get(0);
EmployeeExtended expectedEmployee2 = expectedEmployess.get(1);
EmployeeExtended expectedEmployee3 = expectedEmployess.get(2);

assertThat(actualEmployee1, is(expectedEmployee1));
assertThat(actualEmployee2, is(expectedEmployee2));
assertThat(actualEmployee3, is(expectedEmployee3));

} catch (Exception e) {
if (expectedException == null) {
fail(e.getMessage());
} else {
assertThat(e, instanceOf(expectedException));
}
}
}

private static List<EmployeeExtended> unmarshalling() throws ParseException {
List<EmployeeExtended> employees = new ArrayList<>(3);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ public class DeserializersDateRegexTest {
private final List<EmployeeExtended> expectedEmployess;
private final Class<?> expectedException;

public DeserializersDateRegexTest(String path, List<EmployeeExtended> expectedEmployess, Class<?> expectedException) {
public DeserializersDateRegexTest(String path, List<EmployeeExtended> expectedEmployees, Class<?> expectedException) {
this.path = path;
this.expectedEmployess = expectedEmployess;
this.expectedEmployess = expectedEmployees;
this.expectedException = expectedException;
}

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

@Test
Expand Down

0 comments on commit edddc2f

Please sign in to comment.