Skip to content

Commit edddc2f

Browse files
committed
update pr #57
1 parent a5dcba6 commit edddc2f

File tree

6 files changed

+36
-94
lines changed

6 files changed

+36
-94
lines changed

README.adoc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
= Poiji
2-
:version: v1.17.0
2+
:version: v1.18.0
33

44
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[]
55

@@ -15,15 +15,15 @@ In your Maven/Gradle project, first add the corresponding dependency:
1515
<dependency>
1616
<groupId>com.github.ozlerhakan</groupId>
1717
<artifactId>poiji</artifactId>
18-
<version>1.17.0</version>
18+
<version>1.18.0</version>
1919
</dependency>
2020
----
2121

2222
.gradle
2323
[source,groovy]
2424
----
2525
dependencies {
26-
compile 'com.github.ozlerhakan:poiji:1.17.0'
26+
compile 'com.github.ozlerhakan:poiji:1.18.0'
2727
}
2828
----
2929

@@ -44,8 +44,9 @@ com.poiji.bind.Poiji#fromExcel(java.io.InputStream, com.poiji.exception.PoijiExc
4444
.`PoijiOptions.PoijiOptionsBuilder` Structure
4545
----
4646
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#settings()
47-
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#settings(int)
4847
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#build()
48+
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#dateLenient(boolean)
49+
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#dateRegex(String)
4950
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#datePattern(String)
5051
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#dateTimeFormatter(java.time.format.DateTimeFormatter)
5152
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#ignoreHiddenSheets(boolean)

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.github.ozlerhakan</groupId>
88
<artifactId>poiji</artifactId>
9-
<version>1.17.0</version>
9+
<version>1.18.0</version>
1010
<packaging>jar</packaging>
1111

1212
<name>poiji</name>

src/main/java/com/poiji/option/PoijiOptions.java

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,13 @@ public final class PoijiOptions {
1515
private int skip;
1616
private int sheetIndex;
1717
private String password;
18+
private String dateRegex;
1819
private String datePattern;
20+
private boolean dateLenient;
21+
private boolean trimCellValue;
22+
private boolean ignoreHiddenSheets;
1923
private boolean preferNullOverDefault;
2024
private DateTimeFormatter dateTimeFormatter;
21-
private boolean ignoreHiddenSheets;
22-
private boolean trimCellValue;
23-
//ISSUE #57
24-
//specify regex pattern for date converstion, if set and does not match date set to null
25-
private String dateRegex;
26-
//to set simple date format to Lenient if wanted
27-
private boolean dateLenient;
2825

2926
private PoijiOptions() {
3027
super();
@@ -107,22 +104,20 @@ public int sheetIndex() {
107104
return sheetIndex;
108105
}
109106

110-
//ISSUE #57
111107
public String getDateRegex() {
112108
return dateRegex;
113109
}
114110

115-
public PoijiOptions setDateRegex(String dateRegex) {
111+
private PoijiOptions setDateRegex(String dateRegex) {
116112
this.dateRegex = dateRegex;
117113
return this;
118114
}
119115

120-
//ISSUE #57
121116
public boolean getDateLenient() {
122117
return dateLenient;
123118
}
124119

125-
public PoijiOptions setDateLenient(boolean dateLenient) {
120+
private PoijiOptions setDateLenient(boolean dateLenient) {
126121
this.dateLenient = dateLenient;
127122
return this;
128123
}
@@ -132,15 +127,13 @@ public static class PoijiOptionsBuilder {
132127
private int skip = 1;
133128
private int sheetIndex;
134129
private String password;
135-
private boolean preferNullOverDefault = false;
130+
private String dateRegex;
131+
private boolean dateLenient;
132+
private boolean trimCellValue;
133+
private boolean ignoreHiddenSheets;
134+
private boolean preferNullOverDefault;
136135
private String datePattern = DEFAULT_DATE_PATTERN;
137136
private DateTimeFormatter dateTimeFormatter = DEFAULT_DATE_TIME_FORMATTER;
138-
//ISSUE #55
139-
private boolean ignoreHiddenSheets = false;
140-
private boolean trimCellValue = false;
141-
//ISSUE #57
142-
private String dateRegex = null;
143-
private boolean dateLenient = true;
144137

145138
private PoijiOptionsBuilder() {
146139
}
@@ -272,7 +265,6 @@ public PoijiOptionsBuilder trimCellValue(boolean trimCellValue) {
272265
return this;
273266
}
274267

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

288-
//ISSUE #57
289280
/**
290-
* If would like to set the simple date format is lenient option, use to
291-
* set how strict the date formating must be, defaults to lenient true
292-
* as that is the default for simple date format.
281+
* If the simple date format is lenient, use to
282+
* set how strict the date formatting must be, defaults to lenient false.
283+
* It works only for java.util.Date.
293284
*
294-
* @param dateLenient
285+
* @param dateLenient true or false
295286
* @return this
296287
*/
297288
public PoijiOptionsBuilder dateLenient(boolean dateLenient) {

src/main/java/com/poiji/util/Casting.java

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -100,35 +100,30 @@ private Float floatValue(String value, PoijiOptions options) {
100100
}
101101
}
102102

103-
//ISSUE #57
104-
//a default date method basied on option settings
105103
private Date defaultDate(PoijiOptions options) {
106104
if (Boolean.TRUE.equals(options.preferNullOverDefault())) {
107105
return null;
108-
} else {
109-
Calendar calendar = Calendar.getInstance();
110-
return calendar.getTime();
111106
}
107+
Calendar calendar = Calendar.getInstance();
108+
return calendar.getTime();
112109
}
113110

114-
//ISSUE #57
115-
//a default date method basied on option settings
116111
private LocalDate defaultLocalDate(PoijiOptions options) {
117112
if (Boolean.TRUE.equals(options.preferNullOverDefault())) {
118113
return null;
119-
} else {
120-
return LocalDate.now();
121114
}
115+
return LocalDate.now();
122116
}
123117

124118
private Date dateValue(String value, PoijiOptions options) {
125119

126120
//ISSUE #57
127-
//if a date regex has been speficied then it wont be null
121+
//if a date regex has been specified then it wont be null
128122
//so then make sure the string matches the pattern
129-
//if it doenst, fall back to default
123+
//if it doesn't, fall back to default
130124
//else continue to turn string into java date
131-
//the reason for this is sometime java will manage to parse a string to a date object
125+
126+
//the reason for this is sometime Java will manage to parse a string to a date object
132127
//without any exceptions but since the string was not an exact match you get a very strange date
133128
if (options.getDateRegex() != null && !value.matches(options.getDateRegex())) {
134129
return defaultDate(options);
@@ -139,37 +134,27 @@ private Date dateValue(String value, PoijiOptions options) {
139134
return sdf.parse(value);
140135
} catch (ParseException e) {
141136
return defaultDate(options);
142-
// if (Boolean.TRUE.equals(options.preferNullOverDefault())) {
143-
// return null;
144-
// } else {
145-
// Calendar calendar = Calendar.getInstance();
146-
// return calendar.getTime();
147-
// }
148137
}
149138
}
150139
}
151140

152141
private LocalDate localDateValue(String value, PoijiOptions options) {
153142

154143
//ISSUE #57
155-
//if a date regex has been speficied then it wont be null
144+
//if a date regex has been specified then it wont be null
156145
//so then make sure the string matches the pattern
157-
//if it doenst, fall back to default
146+
//if it doesn't, fall back to default
158147
//else continue to turn string into java date
148+
159149
//the reason for this is sometime java will manage to parse a string to a date object
160150
//without any exceptions but since the string was not an exact match you get a very strange date
161151
if (options.getDateRegex() != null && !value.matches(options.getDateRegex())) {
162152
return defaultLocalDate(options);
163153
} else {
164-
165154
try {
166155
return LocalDate.parse(value, options.dateTimeFormatter());
167156
} catch (DateTimeParseException e) {
168-
//ISSUE #57
169-
//originally returned null, and did not take into consideration 'preferNullOverDefault' setting
170-
//updated to honour options setting
171157
return defaultLocalDate(options);
172-
// return null;
173158
}
174159
}
175160
}
@@ -185,8 +170,6 @@ private Object enumValue(String value, Class type) {
185170
public Object castValue(Class<?> fieldType, String value, PoijiOptions options) {
186171
Object o;
187172

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

230213
} else {
231-
if (value.length() == 0) {
214+
if (value.isEmpty()) {
232215
if (Boolean.TRUE.equals(options.preferNullOverDefault())) {
233216
o = null;
234217
} else {

src/test/java/com/poiji/deserialize/DeserializersDateLenientTest.java

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public DeserializersDateLenientTest(String path, List<EmployeeExtended> expected
3838
public static Iterable<Object[]> queries() throws Exception {
3939
return Arrays.asList(new Object[][]{
4040
{"src/test/resources/date_lenient.xlsx", unmarshalling(), null},
41-
{"src/test/resources/date_lenient.xls", unmarshalling(), null},});
41+
{"src/test/resources/date_lenient.xls", unmarshalling(), null}});
4242
}
4343

4444
@Test
@@ -107,39 +107,6 @@ public void lenientFalse() {
107107
}
108108
}
109109

110-
@Test
111-
public void lenientNotSet() {
112-
113-
try {
114-
PoijiOptions options = PoijiOptions.PoijiOptionsBuilder.settings().datePattern("yyyy-MM-dd").build();
115-
116-
List<EmployeeExtended> actualEmployees = Poiji.fromExcel(new File(path), EmployeeExtended.class, options);
117-
118-
assertThat(actualEmployees, notNullValue());
119-
assertThat(actualEmployees.size(), not(0));
120-
assertThat(actualEmployees.size(), is(expectedEmployess.size()));
121-
122-
EmployeeExtended actualEmployee1 = actualEmployees.get(0);
123-
EmployeeExtended actualEmployee2 = actualEmployees.get(1);
124-
EmployeeExtended actualEmployee3 = actualEmployees.get(2);
125-
126-
EmployeeExtended expectedEmployee1 = expectedEmployess.get(0);
127-
EmployeeExtended expectedEmployee2 = expectedEmployess.get(1);
128-
EmployeeExtended expectedEmployee3 = expectedEmployess.get(2);
129-
130-
assertThat(actualEmployee1, is(expectedEmployee1));
131-
assertThat(actualEmployee2, is(expectedEmployee2));
132-
assertThat(actualEmployee3, is(expectedEmployee3));
133-
134-
} catch (Exception e) {
135-
if (expectedException == null) {
136-
fail(e.getMessage());
137-
} else {
138-
assertThat(e, instanceOf(expectedException));
139-
}
140-
}
141-
}
142-
143110
private static List<EmployeeExtended> unmarshalling() throws ParseException {
144111
List<EmployeeExtended> employees = new ArrayList<>(3);
145112

src/test/java/com/poiji/deserialize/DeserializersDateRegexTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ public class DeserializersDateRegexTest {
2828
private final List<EmployeeExtended> expectedEmployess;
2929
private final Class<?> expectedException;
3030

31-
public DeserializersDateRegexTest(String path, List<EmployeeExtended> expectedEmployess, Class<?> expectedException) {
31+
public DeserializersDateRegexTest(String path, List<EmployeeExtended> expectedEmployees, Class<?> expectedException) {
3232
this.path = path;
33-
this.expectedEmployess = expectedEmployess;
33+
this.expectedEmployess = expectedEmployees;
3434
this.expectedException = expectedException;
3535
}
3636

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

4444
@Test

0 commit comments

Comments
 (0)