Skip to content

Commit

Permalink
add ExcelList.java
Browse files Browse the repository at this point in the history
  • Loading branch information
vaa25 committed Nov 18, 2021
1 parent 0e9bd5e commit 1d13f76
Show file tree
Hide file tree
Showing 15 changed files with 400 additions and 94 deletions.
7 changes: 4 additions & 3 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ In your Maven/Gradle project, first add the corresponding dependency:
<dependency>
<groupId>io.github.vaa25</groupId>
<artifactId>poiji2</artifactId>
<version>1.2.2</version>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
Expand All @@ -29,14 +29,14 @@ In your Maven/Gradle project, first add the corresponding dependency:
[source,groovy]
----
dependencies {
implementation 'io.github.vaa25:poiji2:1.2.2'
implementation 'io.github.vaa25:poiji2:1.3.0'
implementation 'org.apache.poi:poi-ooxml:4.1.2'
}
----

You can find feature descriptions in https://github.com/ozlerhakan/poiji README

Supported features: 1 - 17.
Supported all features of original Poiji.

Also:

Expand All @@ -50,3 +50,4 @@ Also:
- Poiji2 can transform excel to Stream (use `Poiji.fromExcelToStream(...)`). WARNING! XLSX takes double time more than transform to List.
- Poiji2 has usable builders for any case (ex. use `Poiji.<JavaClass>fromExcel().withSource(new File(...)).withJavaType(JavaClass.class).toStream()`)
- Poiji2 (since v1.2.1) can be used with https://github.com/vaa25/spring-boot-starter-web-excel[spring-boot-starter-web-excel]
- Poiji2 can read lists in row (use `@ExcelList` on `List`)
28 changes: 28 additions & 0 deletions src/main/java/com/poiji/annotation/ExcelList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.poiji.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Documented
public @interface ExcelList {

/**
* Count of neighbour cells in row that belongs to one list element
*/
int elementSize();

/**
* First column number that belongs to list
*/
int listStart();

/**
* Last column number that belongs to list
*/
int listEnd() default Integer.MAX_VALUE;
}
40 changes: 22 additions & 18 deletions src/main/java/com/poiji/bind/mapping/CsvLineReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,37 @@ public CsvLineReader(final Class<T> entity, final PoijiOptions options) {
}

public T readLine(final String line) {
final int row = this.row++;
if (isHeaderRow(row)){
return parseColumnNames(line);
} else if (isContentRow(row)){
return parseContentRow(line);
final T result;
if (isHeaderRow()){
result = parseColumnNames(line);
} else if (isContentRow()){
result = parseContentRow(line);
} else {
return null;
result = null;
}
row++;
return result;
}

private boolean isContentRow(final int rowNum) {
return rowNum > options.skip() + options.getHeaderStart() + options.getHeaderCount() - 1 && (options.getLimit() == 0 || internalCount <= options.getLimit());
private boolean isContentRow() {
return row > options.skip() + options.getHeaderStart() + options.getHeaderCount() - 1 && (options.getLimit() == 0 || internalCount <= options.getLimit());
}

private boolean isHeaderRow(final int row) {
private boolean isHeaderRow() {
int headerStart = options.getHeaderStart();
int headerCount = options.getHeaderCount();
this.internalCount = headerStart + headerCount;
return row >= headerStart && row < headerStart + headerCount;
}

private T parseContentRow(final String line) {
final String[] values = parseLine(line);
if (areValuesHaveData(values)){
final int lastValuedColumn = lastValuedColumn(values);
if (lastValuedColumn >= 0) {
final T instance = ReflectUtil.newInstanceOf(entity);
for (int column = 0; column < values.length; column++) {
if (usedColumns.contains(column) || readMappedFields.orderedFields.containsKey(column)){
readMappedFields.setCellInInstance(internalCount, column, unwrap(values[column]), instance);
for (int column = 0; column <= lastValuedColumn; column++) {
if (usedColumns.contains(column) || readMappedFields.orderedFields.containsKey(column)) {
readMappedFields.setCellInInstance(row, column, unwrap(values[column]), instance);
}
}
internalCount++;
Expand Down Expand Up @@ -107,13 +111,13 @@ private enum State {
BEGIN, MIDDLE, QUOTE
}

private boolean areValuesHaveData(final String[] values) {
for (final String value : values) {
if (!value.isEmpty()){
return true;
private int lastValuedColumn(final String[] values) {
for (int i = values.length - 1; i >= 0; i--) {
if (!values[i].isEmpty()) {
return i;
}
}
return false;
return -1;
}

private String unwrap(final String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ public <T> Stream<T> stream(final Class<T> type) {
final String charsetName = bomInputStream.getCharset().orElse(options.getCharset());
try {
final BufferedReader reader = new BufferedReader(new InputStreamReader(bomInputStream, charsetName));
return reader.lines().map(csvLineReader::readLine).filter(Objects::nonNull);
Stream<String> stream = reader.lines();
if (options.getLimit() > 0) {
stream =
stream.limit(
options.getLimit() + options.getHeaderStart() + options.getHeaderCount() + options.skip());
}
return stream.map(csvLineReader::readLine).filter(Objects::nonNull);
} catch (IOException e) {
throw new PoijiException("Problem occurred while reading CSV data", e);
}
Expand Down
Loading

0 comments on commit 1d13f76

Please sign in to comment.