Skip to content

Commit

Permalink
Merge pull request #148 from ozlerhakan/2.6.0
Browse files Browse the repository at this point in the history
bump version 2.6.0
  • Loading branch information
ozlerhakan committed Apr 21, 2020
2 parents 12283d9 + cdf05a3 commit 7aa131c
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 145 deletions.
8 changes: 4 additions & 4 deletions README.adoc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
= Poiji
:version: v2.5.0
:version: v2.6.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-4.1.0-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[]
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-4.1.2-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[]

Poiji is a teeny 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 @@ -15,15 +15,15 @@ In your Maven/Gradle project, first add the corresponding dependency:
<dependency>
<groupId>com.github.ozlerhakan</groupId>
<artifactId>poiji</artifactId>
<version>2.5.0</version>
<version>2.6.0</version>
</dependency>
----

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

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</issueManagement>

<properties>
<apache.poi.version>4.1.0</apache.poi.version>
<apache.poi.version>4.1.2</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
82 changes: 36 additions & 46 deletions src/main/java/com/poiji/bind/Poiji.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,13 @@ private Poiji() {
/**
* converts excel rows into a list of objects
*
* @param file
* excel file ending with .xls or .xlsx.
* @param type
* type of the root object.
* @param <T>
* type of the root object.
* @return
* the newly created a list of objects
*
* @throws PoijiException
* if an internal exception occurs during the mapping process.
* @throws InvalidExcelFileExtension
* if the specified excel file extension is invalid.
* @throws IllegalCastException
* if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final.
*
* @param file excel file ending with .xls or .xlsx.
* @param type type of the root object.
* @param <T> type of the root object.
* @return the newly created a list of objects
* @throws PoijiException if an internal exception occurs during the mapping process.
* @throws InvalidExcelFileExtension if the specified excel file extension is invalid.
* @throws IllegalCastException if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final.
* @see Poiji#fromExcel(File, Class, PoijiOptions)
*/
public static synchronized <T> List<T> fromExcel(final File file, final Class<T> type) {
Expand All @@ -70,16 +61,15 @@ public static synchronized <T> List<T> fromExcel(final File file, final Class<T>
/**
* converts excel rows into a list of objects
*
* @param file excel file ending with .xls or .xlsx.
* @param type type of the root object.
* @param <T> type of the root object.
* @param file excel file ending with .xls or .xlsx.
* @param type type of the root object.
* @param <T> type of the root object.
* @param consumer output retrieves records
* @throws PoijiException if an internal exception occurs during the mapping
* process.
* @throws PoijiException if an internal exception occurs during the mapping
* process.
* @throws InvalidExcelFileExtension if the specified excel file extension
* is invalid.
* @throws IllegalCastException if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final.
*
* is invalid.
* @throws IllegalCastException if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final.
* @see Poiji#fromExcel(File, Class, PoijiOptions)
*/
public static synchronized <T> void fromExcel(final File file, final Class<T> type, final Consumer<? super T> consumer) {
Expand Down Expand Up @@ -112,13 +102,13 @@ public static synchronized <T> List<T> fromExcel(final InputStream inputStream,
* converts excel rows into a list of objects
*
* @param inputStream excel file stream
* @param excelType type of the excel file, xls or xlsx
* @param type type of the root object.
* @param <T> type of the root object.
* @param consumer represents an operation that accepts the type argument
* @throws PoijiException if an internal exception occurs during the mapping process.
* @param excelType type of the excel file, xls or xlsx
* @param type type of the root object.
* @param <T> type of the root object.
* @param consumer represents an operation that accepts the type argument
* @throws PoijiException if an internal exception occurs during the mapping process.
* @throws InvalidExcelFileExtension if the specified excel file extension is invalid.
* @throws IllegalCastException if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final.
* @throws IllegalCastException if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final.
* @see Poiji#fromExcel(File, Class, PoijiOptions)
*/
public static synchronized <T> void fromExcel(final InputStream inputStream,
Expand Down Expand Up @@ -146,21 +136,21 @@ public static synchronized <T> void fromExcel(final InputStream inputStream,
*/
public static synchronized <T> List<T> fromExcel(final File file, final Class<T> type, final PoijiOptions options) {
final ArrayList<T> list = new ArrayList<>();
fromExcel(file, type, options,list::add);
fromExcel(file, type, options, list::add);
return list;
}

/**
* converts excel rows into a list of objects
*
* @param file excel file ending with .xls or .xlsx.
* @param type type of the root object.
* @param <T> type of the root object.
* @param options specifies to change the default behaviour of the poiji.
* @param file excel file ending with .xls or .xlsx.
* @param type type of the root object.
* @param <T> type of the root object.
* @param options specifies to change the default behaviour of the poiji.
* @param consumer represents an operation that accepts the type argument
* @throws PoijiException if an internal exception occurs during the mapping process.
* @throws PoijiException if an internal exception occurs during the mapping process.
* @throws InvalidExcelFileExtension if the specified excel file extension is invalid.
* @throws IllegalCastException if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final.
* @throws IllegalCastException if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final.
* @see Poiji#fromExcel(File, Class)
*/
public static synchronized <T> void fromExcel(final File file, final Class<T> type, final PoijiOptions options, final Consumer<? super T> consumer) {
Expand Down Expand Up @@ -188,23 +178,23 @@ public static synchronized <T> List<T> fromExcel(final InputStream inputStream,
final PoijiOptions options) {
Objects.requireNonNull(excelType);
final ArrayList<T> list = new ArrayList<>();
fromExcel(inputStream, excelType,type, options, list::add);
fromExcel(inputStream, excelType, type, options, list::add);
return list;
}

/**
* converts excel rows into a list of objects
*
* @param inputStream excel file stream
* @param excelType type of the excel file, xls or xlsx
* @param type type of the root object.
* @param <T> type of the root object.
* @param options specifies to change the default behaviour of the poiji.
* @param consumer represents an operation that accepts the type argument
* @throws PoijiException if an internal exception occurs during the mapping process.
* @param excelType type of the excel file, xls or xlsx
* @param type type of the root object.
* @param <T> type of the root object.
* @param options specifies to change the default behaviour of the poiji.
* @param consumer represents an operation that accepts the type argument
* @throws PoijiException if an internal exception occurs during the mapping process.
* @throws InvalidExcelFileExtension if the specified excel file extension is invalid.
* @throws IllegalCastException if this Field object is enforcing Java
* language access control and the underlying field is either inaccessible or final.
* @throws IllegalCastException if this Field object is enforcing Java
* 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,
Expand Down
66 changes: 22 additions & 44 deletions src/main/java/com/poiji/bind/mapping/PoijiHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,13 @@
* Created by hakan on 22/10/2017
*/
final class PoijiHandler<T> implements SheetContentsHandler {

private T instance;
private Consumer<? super T> consumer;
private int internalRow;
private int internalCount;
private int limit;

private Class<T> type;
private PoijiOptions options;

private final Casting casting;
private Map<String, Integer> columnIndexPerTitle;
private Map<Integer, String> titlePerColumnIndex;
Expand All @@ -51,7 +48,6 @@ final class PoijiHandler<T> implements SheetContentsHandler {
this.options = options;
this.consumer = consumer;
this.limit = options.getLimit();

casting = options.getCasting();
columnIndexPerTitle = new HashMap<>();
titlePerColumnIndex = new HashMap<>();
Expand All @@ -61,16 +57,16 @@ final class PoijiHandler<T> implements SheetContentsHandler {

private void setFieldValue(String content, Class<? super T> subclass, int column) {
if (subclass != Object.class) {
if(setValue(content, subclass, column)) {
if (setValue(content, subclass, column)) {
return;
}

setFieldValue(content, subclass.getSuperclass(), column);
}
}

/**
* Using this to hold inner objects that will be mapped to the main object
* **/
* Using this to hold inner objects that will be mapped to the main object
**/
private Object getInstance(Field field) {
Object ins = null;
if (fieldInstances.containsKey(field.getName())) {
Expand All @@ -83,14 +79,12 @@ private Object getInstance(Field field) {
}

private boolean setValue(String content, Class<? super T> type, int column) {


Stream.of(type.getDeclaredFields())
.filter(field -> field.getAnnotation(ExcelUnknownCells.class) == null)
.forEach(field -> {
ExcelRow excelRow = field.getAnnotation(ExcelRow.class);
if (excelRow != null) {
Object o = casting.castValue(field.getType(), valueOf(internalRow), options);
Object o = casting.castValue(field.getType(), valueOf(internalRow), internalRow, column, options);
setFieldData(field, o, instance);
columnToField.put(-1, field);
}
Expand All @@ -103,17 +97,14 @@ private boolean setValue(String content, Class<? super T> type, int column) {
setFieldData(field, ins, instance);
columnToField.put(column, f);
columnToSuperClassField.put(column, field);
// return true;
}
}
} else {
if(setValue(field, column, content, instance)) {
if (setValue(field, column, content, instance)) {
columnToField.put(column, field);
// return true;
}
}
});

Stream.of(type.getDeclaredFields())
.filter(field -> field.getAnnotation(ExcelUnknownCells.class) != null)
.forEach(field -> {
Expand All @@ -127,33 +118,28 @@ private boolean setValue(String content, Class<? super T> type, int column) {
} else {
excelUnknownCellsMap = (Map) field.get(instance);
}

excelUnknownCellsMap.put(titlePerColumnIndex.get(column), content);
} catch (IllegalAccessException e) {
throw new IllegalCastException("Could not read content of field " + field.getName() + " on Object {" + instance + "}");
}
}
});

// For ExcelRow annotation
if(columnToField.containsKey(-1)) {
if (columnToField.containsKey(-1)) {
Field field = columnToField.get(-1);
Object o = casting.castValue(field.getType(), valueOf(internalRow), options);
Object o = casting.castValue(field.getType(), valueOf(internalRow), internalRow, column, options);
setFieldData(field, o, instance);
}
if(columnToField.containsKey(column)) {
if (columnToField.containsKey(column) && columnToSuperClassField.containsKey(column)) {
Field field = columnToField.get(column);
if (columnToSuperClassField.containsKey(column)) {
Object ins;
ins = getInstance(columnToSuperClassField.get(column));
if (setValue(field, column, content, ins)) {
setFieldData(columnToSuperClassField.get(column), ins, instance);
return true;
}
Object ins;
ins = getInstance(columnToSuperClassField.get(column));
if (setValue(field, column, content, ins)) {
setFieldData(columnToSuperClassField.get(column), ins, instance);
return true;
}
return setValue(field, column, content, instance);
}

return false;
}

Expand All @@ -162,7 +148,7 @@ private boolean setValue(Field field, int column, String content, Object ins) {
if (index != null) {
Class<?> fieldType = field.getType();
if (column == index.value()) {
Object o = casting.castValue(fieldType, content, options);
Object o = casting.castValue(fieldType, content, internalRow, column, options);
setFieldData(field, o, ins);
return true;
}
Expand All @@ -171,12 +157,12 @@ private boolean setValue(Field field, int column, String content, Object ins) {
if (excelCellName != null) {
Class<?> fieldType = field.getType();
final String titleName = options.getCaseInsensitive()
? excelCellName.value().toLowerCase()
: excelCellName.value();
? excelCellName.value().toLowerCase()
: excelCellName.value();
final Integer titleColumn = columnIndexPerTitle.get(titleName);
//Fix both columns mapped to name passing this condition below
if (titleColumn != null && titleColumn == column) {
Object o = casting.castValue(fieldType, content, options);
Object o = casting.castValue(fieldType, content, internalRow, column, options);
setFieldData(field, o, ins);
return true;
}
Expand Down Expand Up @@ -205,9 +191,8 @@ public void startRow(int rowNum) {

@Override
public void endRow(int rowNum) {

if (internalRow != rowNum)
return;
return;

if (rowNum + 1 > options.skip()) {
consumer.accept(instance);
Expand All @@ -218,29 +203,22 @@ public void endRow(int rowNum) {
public void cell(String cellReference, String formattedValue, XSSFComment comment) {
CellAddress cellAddress = new CellAddress(cellReference);
int row = cellAddress.getRow();

int headers = options.getHeaderStart();
int column = cellAddress.getColumn();

if (row <= headers) {
columnIndexPerTitle.put(
options.getCaseInsensitive() ? formattedValue.toLowerCase() : formattedValue,
column
options.getCaseInsensitive() ? formattedValue.toLowerCase() : formattedValue,
column
);

titlePerColumnIndex.put(column, getTitleNameForMap(formattedValue, column));
}

if (row + 1 <= options.skip()) {
return;
}

if (limit != 0 && internalCount > limit) {
return;
}

internalRow = row;

setFieldValue(formattedValue, type, column);
}

Expand All @@ -259,4 +237,4 @@ private String getTitleNameForMap(String cellContent, int columnIndex) {
public void headerFooter(String text, boolean isHeader, String tagName) {
//no-op
}
}
}

0 comments on commit 7aa131c

Please sign in to comment.