Skip to content

Commit

Permalink
Merge dc5c707 into f33b4e3
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulrafique committed Mar 8, 2018
2 parents f33b4e3 + dc5c707 commit 74174cf
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 22 deletions.
12 changes: 12 additions & 0 deletions src/main/java/com/poiji/annotation/ExcelRow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.poiji.annotation;

import java.lang.annotation.*;

/**
* Created by ar on 8/03/2018.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Documented
public @interface ExcelRow {
}
35 changes: 21 additions & 14 deletions src/main/java/com/poiji/bind/mapping/HSSFUnmarshaller.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
package com.poiji.bind.mapping;

import com.poiji.annotation.ExcelCell;
import com.poiji.annotation.ExcelRow;
import com.poiji.bind.PoijiWorkbook;
import com.poiji.exception.IllegalCastException;
import com.poiji.exception.PoijiInstantiationException;
import com.poiji.bind.PoijiWorkbook;
import com.poiji.option.PoijiOptions;
import com.poiji.util.Casting;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.*;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import static java.lang.String.valueOf;

/**
* This is the main class that converts the excel sheet fromExcel Java object
* Created by hakan on 16/01/2017.
Expand Down Expand Up @@ -74,7 +72,12 @@ private <T> T deserialize0(Row currentRow, Class<T> type) {

private <T> T tailSetFieldValue(Row currentRow, Class<? super T> type, T instance) {
for (Field field : type.getDeclaredFields()) {

ExcelRow excelRow = field.getAnnotation(ExcelRow.class);
if (excelRow != null) {
Object o;
o = casting.castValue(field.getType(), valueOf(currentRow.getRowNum()), options);
setFieldData(instance, field, o);
}
ExcelCell index = field.getAnnotation(ExcelCell.class);
if (index != null) {
Class<?> fieldType = field.getType();
Expand All @@ -87,17 +90,21 @@ private <T> T tailSetFieldValue(Row currentRow, Class<? super T> type, T instanc
} else {
o = casting.castValue(fieldType, "", options);
}
try {
field.setAccessible(true);
field.set(instance, o);
} catch (IllegalAccessException e) {
throw new IllegalCastException("Unexpected cast type {" + o + "} of field" + field.getName());
}
setFieldData(instance, field, o);
}
}
return instance;
}

private <T> void setFieldData(T instance, Field field, Object o) {
try {
field.setAccessible(true);
field.set(instance, o);
} catch (IllegalAccessException e) {
throw new IllegalCastException("Unexpected cast type {" + o + "} of field" + field.getName());
}
}

private <T> T setFieldValue(Row currentRow, Class<? super T> subclass, T instance) {
return subclass == null
? instance
Expand Down
27 changes: 19 additions & 8 deletions src/main/java/com/poiji/bind/mapping/PoijiHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.poiji.bind.mapping;

import com.poiji.annotation.ExcelCell;
import com.poiji.annotation.ExcelRow;
import com.poiji.exception.IllegalCastException;
import com.poiji.option.PoijiOptions;
import com.poiji.util.Casting;
Expand All @@ -12,6 +13,7 @@
import java.util.ArrayList;
import java.util.List;

import static java.lang.String.valueOf;
import static org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler.SheetContentsHandler;

/**
Expand Down Expand Up @@ -47,7 +49,7 @@ private <T> T newInstanceOf(Class<T> type) {
T newInstance;
try {
newInstance = type.getDeclaredConstructor().newInstance();
} catch (NoSuchMethodException| InvocationTargetException | IllegalAccessException | InstantiationException e) {
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | InstantiationException e) {
throw new IllegalCastException("Cannot create a new instance of " + type.getName());
}

Expand All @@ -64,25 +66,34 @@ private void setFieldValue(String content, Class<? super T> subclass, int column

private void setValue(String content, Class<? super T> type, int column) {
for (Field field : type.getDeclaredFields()) {

ExcelRow excelRow = field.getAnnotation(ExcelRow.class);
if (excelRow != null) {
Object o;
o = casting.castValue(field.getType(), valueOf(internalCount), options);
setFieldData(field, o);
}
ExcelCell index = field.getAnnotation(ExcelCell.class);
if (index != null) {
Class<?> fieldType = field.getType();

if (column == index.value()) {
Object o = casting.castValue(fieldType, content, options);

try {
field.setAccessible(true);
field.set(instance, o);
} catch (IllegalAccessException e) {
throw new IllegalCastException("Unexpected cast type {" + o + "} of field" + field.getName());
}
setFieldData(field, o);
}
}
}
}

private void setFieldData(Field field, Object o) {
try {
field.setAccessible(true);
field.set(instance, o);
} catch (IllegalAccessException e) {
throw new IllegalCastException("Unexpected cast type {" + o + "} of field" + field.getName());
}
}

@Override
public void startRow(int rowNum) {
if (rowNum + 1 > options.skip()) {
Expand Down
89 changes: 89 additions & 0 deletions src/test/java/com/poiji/deserialize/RowNumberTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.poiji.deserialize;

import com.poiji.bind.Poiji;
import com.poiji.deserialize.model.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertEquals;

/**
* Created by ar on 9/03/2018.
*/
@RunWith(Parameterized.class)
public class RowNumberTest {

private String path;
private List<Person> expectedPersonList;
private Class<?> expectedException;

public RowNumberTest(String path, List<Person> expectedPersonList, Class<?> expectedException) {
this.path = path;
this.expectedPersonList = expectedPersonList;
this.expectedException = expectedException;
}

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

@Test
public void testRowNumberForXLSXFormatFile() {
List<Person> actualCars = Poiji.fromExcel(new File(path), Person.class);
assertEquals(expectedPersonList.get(0).getRow(), actualCars.get(0).getRow());
assertEquals(expectedPersonList.get(1).getRow(), actualCars.get(1).getRow());
assertEquals(expectedPersonList.get(2).getRow(), actualCars.get(2).getRow());
assertEquals(expectedPersonList.get(3).getRow(), actualCars.get(3).getRow());
assertEquals(expectedPersonList.get(4).getRow(), actualCars.get(4).getRow());
}

private static List<Person> unmarshalling() {
List<Person> persons = new ArrayList<>(5);
Person person1 = new Person();
person1.setRow(1);
person1.setName("Rafique");
person1.setAddress("Melbourne");
person1.setEmail("raf@abc.com");
person1.setMobile("91701");
persons.add(person1);
Person person2 = new Person();
person2.setRow(2);
person2.setName("Sam");
person2.setAddress("Melbourne");
person2.setEmail("sam@abc.com");
person2.setMobile("617019");
persons.add(person2);
Person person3 = new Person();
person3.setRow(3);
person3.setName("Tony");
person3.setAddress("Melbourne");
person3.setEmail("tony@abc.com");
person3.setMobile("617019");
persons.add(person3);
Person person4 = new Person();
person4.setRow(4);
person4.setName("Michael");
person4.setAddress("Melbourne");
person4.setEmail("mic@abc.com");
person4.setMobile("617019");
persons.add(person4);
Person person5 = new Person();
person5.setRow(5);
person5.setName("Terry");
person5.setAddress("Melbourne");
person5.setEmail("terry@abc.com");
person5.setMobile("617019");
persons.add(person5);
return persons;
}
}
65 changes: 65 additions & 0 deletions src/test/java/com/poiji/deserialize/model/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.poiji.deserialize.model;

import com.poiji.annotation.ExcelCell;
import com.poiji.annotation.ExcelRow;

/**
* Created by ar on 9/03/2018.
*/
public class Person {

@ExcelRow
protected int row;

@ExcelCell(1)
protected String name;

@ExcelCell(2)
protected String address;

@ExcelCell(3)
protected String mobile;

@ExcelCell(4)
protected String email;

public int getRow() {
return row;
}

public void setRow(int row) {
this.row = row;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getMobile() {
return mobile;
}

public void setMobile(String mobile) {
this.mobile = mobile;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}
}
Binary file added src/test/resources/person.xls
Binary file not shown.
Binary file added src/test/resources/person.xlsx
Binary file not shown.

0 comments on commit 74174cf

Please sign in to comment.