Skip to content

Commit

Permalink
Merge 6fe7308 into 12283d9
Browse files Browse the repository at this point in the history
  • Loading branch information
vaa25 committed Feb 8, 2020
2 parents 12283d9 + 6fe7308 commit 02fd371
Show file tree
Hide file tree
Showing 21 changed files with 1,321 additions and 36 deletions.
9 changes: 9 additions & 0 deletions src/main/java/com/poiji/annotation/ExcelCellName.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,19 @@
@Documented
public @interface ExcelCellName {

int ABSENT_ORDER = -1;

/**
* Specifies the column name where the corresponding value is mapped from the excel data
*
* @return column name
*/
String value();

/**
* Specifies the column order in saved file
*
* @return column order
*/
int order() default ABSENT_ORDER;
}
20 changes: 14 additions & 6 deletions src/main/java/com/poiji/bind/Poiji.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import com.poiji.exception.PoijiException;
import com.poiji.option.PoijiOptions;
import com.poiji.option.PoijiOptions.PoijiOptionsBuilder;
import com.poiji.save.FileSaverFactory;
import com.poiji.util.Files;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
Expand Down Expand Up @@ -207,17 +207,25 @@ public static synchronized <T> List<T> fromExcel(final InputStream inputStream,
* language access control and the underlying field is either inaccessible or final.
* @see Poiji#fromExcel(File, Class)
*/
public static synchronized <T> void fromExcel(final InputStream inputStream,
final PoijiExcelType excelType,
final Class<T> type,
final PoijiOptions options,
final Consumer<? super T> consumer) {
public static synchronized <T> void fromExcel(final InputStream inputStream, final PoijiExcelType excelType,
final Class<T> type, final PoijiOptions options, final Consumer<? super T> consumer
) {
Objects.requireNonNull(excelType);

final Unmarshaller unmarshaller = deserializer(inputStream, excelType, options);
unmarshaller.unmarshal(type, consumer);
}

public static <T> void toExcel(final File file, final Class<T> clazz, final List<T> data) {
toExcel(file, clazz, data, PoijiOptionsBuilder.settings().build());
}

public static <T> void toExcel(
final File file, final Class<T> clazz, final List<T> data, final PoijiOptions options
) {
new FileSaverFactory<>(clazz, options).toFile(file).save(data);
}

private static Unmarshaller deserializer(final File file, final PoijiOptions options) {
final PoijiFile<?> poijiFile = new PoijiFile<>(file);

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/poiji/bind/mapping/SheetNameExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

import com.poiji.annotation.ExcelSheet;
import com.poiji.option.PoijiOptions;

import java.util.Optional;

/**
* Utility class to extract the sheet name.
*/
class SheetNameExtractor {
public class SheetNameExtractor {
/**
* Extracts the sheet name from either the annotated value {@link ExcelSheet} from the model class or from the sheet name set
* in the Poiji Options. Poiji first looks at {@link ExcelSheet} then {@link PoijiOptions}.
* @param type The class instance of the object model.
*
* @param type The class instance of the object model.
* @param options The Poiji options.
* @param <T> The type of the object model.
* @param <T> The type of the object model.
* @return an Optional sheet name
*/
public static <T> Optional<String> getSheetName(Class<T> type, PoijiOptions options) {
Expand Down
22 changes: 18 additions & 4 deletions src/main/java/com/poiji/config/DefaultCasting.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.poiji.config;

import com.poiji.option.PoijiOptions;
import com.poiji.parser.Parsers;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand All @@ -13,9 +15,6 @@
import java.util.Date;
import java.util.List;

import com.poiji.option.PoijiOptions;
import com.poiji.parser.Parsers;

/**
* Created by hakan on 22/01/2017.
*/
Expand Down Expand Up @@ -222,9 +221,24 @@ public Object castValue(Class<?> fieldType, String rawValue, int row, int col, P
} else if (fieldType == Float.class) {
o = floatValue(value, sheetName, row, col, options);

} else if (fieldType == boolean.class || fieldType == Boolean.class) {
} else if (fieldType == boolean.class) {
o = Boolean.valueOf(value);

} else if (fieldType == Boolean.class) {
o = value.isEmpty() ? options.preferNullOverDefault() ? null : false : Boolean.valueOf(value);

} else if (fieldType == byte.class) {
o = Byte.valueOf(value);

} else if (fieldType == Byte.class) {
o = value.isEmpty() ? options.preferNullOverDefault() ? null : (byte) 0 : Byte.valueOf(value);

} else if (fieldType == short.class) {
o = Short.valueOf(value);

} else if (fieldType == Short.class) {
o = value.isEmpty() ? options.preferNullOverDefault() ? null : (short) 0 : Short.valueOf(value);

} else if (fieldType == Date.class) {
o = dateValue(value, sheetName, row, col, options);

Expand Down
114 changes: 94 additions & 20 deletions src/main/java/com/poiji/option/PoijiOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import com.poiji.config.Casting;
import com.poiji.config.DefaultCasting;
import com.poiji.exception.PoijiException;

import com.poiji.save.CellCasting;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

import static com.poiji.util.PoijiConstants.DEFAULT_DATE_FORMATTER;
import static com.poiji.util.PoijiConstants.DEFAULT_DATE_PATTERN;
import static com.poiji.util.PoijiConstants.DEFAULT_DATE_TIME_FORMATTER;
import static com.poiji.util.PoijiConstants.DEFAULT_DATE_TIME_PATTERN;

/**
* Created by hakan on 17/01/2017.
Expand All @@ -24,13 +25,16 @@ public final class PoijiOptions {
private String dateRegex;
private String dateTimeRegex;
private String datePattern;
private String localDatePattern;
private String localDateTimePattern;
private boolean dateLenient;
private boolean trimCellValue;
private boolean ignoreHiddenSheets;
private boolean preferNullOverDefault;
private DateTimeFormatter dateFormatter;
private DateTimeFormatter dateTimeFormatter;
private Casting casting;
private CellCasting cellCasting;
private int headerStart;
private String sheetName;
private boolean caseInsensitive;
Expand Down Expand Up @@ -58,6 +62,15 @@ private PoijiOptions setDatePattern(String datePattern) {
return this;
}

public String getLocalDatePattern() {
return localDatePattern;
}

private PoijiOptions setLocalDatePattern(final String localDatePattern) {
this.localDatePattern = localDatePattern;
return this;
}

private PoijiOptions setDateFormatter(DateTimeFormatter dateFormatter) {
this.dateFormatter = dateFormatter;
return this;
Expand Down Expand Up @@ -86,6 +99,15 @@ public String datePattern() {
return datePattern;
}

public String getLocalDateTimePattern() {
return localDateTimePattern;
}

private PoijiOptions setLocalDateTimePattern(final String localDateTimePattern) {
this.localDateTimePattern = localDateTimePattern;
return this;
}

public DateTimeFormatter dateFormatter() {
return dateFormatter;
}
Expand Down Expand Up @@ -134,6 +156,15 @@ public PoijiOptions setCasting(Casting casting) {
return this;
}

public CellCasting getCellCasting() {
return cellCasting;
}

private PoijiOptions setCellCasting(final CellCasting cellCasting) {
this.cellCasting = cellCasting;
return this;
}

private PoijiOptions setSheetIndex(int sheetIndex) {
this.sheetIndex = sheetIndex;
return this;
Expand Down Expand Up @@ -192,7 +223,7 @@ public boolean getCaseInsensitive() {
return caseInsensitive;
}

public PoijiOptions setCaseInsensitive(final boolean caseInsensitive) {
private PoijiOptions setCaseInsensitive(final boolean caseInsensitive) {
this.caseInsensitive = caseInsensitive;
return this;
}
Expand All @@ -209,8 +240,11 @@ public static class PoijiOptionsBuilder {
private boolean preferNullOverDefault;
private String datePattern = DEFAULT_DATE_PATTERN;
private DateTimeFormatter dateFormatter = DEFAULT_DATE_FORMATTER;
private String localDatePattern = DEFAULT_DATE_PATTERN;
private String localDateTimePattern = DEFAULT_DATE_TIME_PATTERN;
private DateTimeFormatter dateTimeFormatter = DEFAULT_DATE_TIME_FORMATTER;
private Casting casting = new DefaultCasting();
private CellCasting cellCasting = new CellCasting();
private int headerStart = 0;
private int skip = 0;
private int limit = 0;
Expand Down Expand Up @@ -269,14 +303,38 @@ public PoijiOptionsBuilder dateTimeFormatter(DateTimeFormatter dateTimeFormatter
* set date pattern, default date format is "dd/M/yyyy" for
* java.util.Date
*
* @param datePattern date time formatter
* @param datePattern date pattern
* @return this
*/
public PoijiOptionsBuilder datePattern(String datePattern) {
this.datePattern = datePattern;
return this;
}

/**
* set date time pattern, default date time format is "dd/M/yyyy HH:mm:ss" for
* writing java.time.LocalDate into excel
*
* @param localDatePattern date time pattern
* @return this
*/
public PoijiOptionsBuilder localDatePattern(String localDatePattern) {
this.localDatePattern = localDatePattern;
return this;
}

/**
* set date time pattern, default date time format is "dd/M/yyyy HH:mm:ss" for
* writing java.time.LocalDateTime into excel
*
* @param localDateTimePattern date time pattern
* @return this
*/
public PoijiOptionsBuilder localDateTimePattern(String localDateTimePattern) {
this.localDateTimePattern = localDateTimePattern;
return this;
}

/**
* set whether or not to use null instead of default values for Integer,
* Double, Float, Long, String and java.util.Date types.
Expand All @@ -291,23 +349,26 @@ public PoijiOptionsBuilder preferNullOverDefault(boolean preferNullOverDefault)

public PoijiOptions build() {
return new PoijiOptions()
.setSkip(skip + headerStart + 1)
.setPassword(password)
.setPreferNullOverDefault(preferNullOverDefault)
.setDatePattern(datePattern)
.setDateFormatter(dateFormatter)
.setDateTimeFormatter(dateTimeFormatter)
.setSheetIndex(sheetIndex)
.setSheetName(sheetName)
.setIgnoreHiddenSheets(ignoreHiddenSheets)
.setTrimCellValue(trimCellValue)
.setDateRegex(dateRegex)
.setDateTimeRegex(dateTimeRegex)
.setDateLenient(dateLenient)
.setHeaderStart(headerStart)
.setCasting(casting)
.setLimit(limit)
.setCaseInsensitive(caseInsensitive);
.setSkip(skip + headerStart + 1)
.setPassword(password)
.setPreferNullOverDefault(preferNullOverDefault)
.setDatePattern(datePattern)
.setLocalDatePattern(localDatePattern)
.setLocalDateTimePattern(localDateTimePattern)
.setDateFormatter(dateFormatter)
.setDateTimeFormatter(dateTimeFormatter)
.setSheetIndex(sheetIndex)
.setSheetName(sheetName)
.setIgnoreHiddenSheets(ignoreHiddenSheets)
.setTrimCellValue(trimCellValue)
.setDateRegex(dateRegex)
.setDateTimeRegex(dateTimeRegex)
.setDateLenient(dateLenient)
.setHeaderStart(headerStart)
.setCasting(casting)
.setCellCasting(cellCasting)
.setLimit(limit)
.setCaseInsensitive(caseInsensitive);
}

/**
Expand Down Expand Up @@ -448,6 +509,19 @@ public PoijiOptionsBuilder withCasting(Casting casting) {
return this;
}

/**
* Use a modified cell casting implementation
*
* @param cellCasting modified cell casting implementation
* @return this
*/
public PoijiOptionsBuilder withCellCasting(CellCasting cellCasting) {
Objects.requireNonNull(cellCasting);

this.cellCasting = cellCasting;
return this;
}

/**
* This is to set the row which the unmarshall will
* use to start reading header titles, incase the
Expand Down
Loading

0 comments on commit 02fd371

Please sign in to comment.