Skip to content

Commit

Permalink
1.5.0 add Unmarshaller.readSheetNames()
Browse files Browse the repository at this point in the history
  • Loading branch information
vaa25 committed Mar 17, 2024
1 parent 165d0c9 commit 3054ad5
Show file tree
Hide file tree
Showing 12 changed files with 310 additions and 186 deletions.
29 changes: 24 additions & 5 deletions src/main/java/com/poiji/bind/FromExcel.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.poiji.exception.PoijiExcelType;
import com.poiji.exception.PoijiException;
import com.poiji.option.PoijiOptions;
import org.apache.poi.ss.usermodel.Sheet;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
Expand All @@ -13,7 +15,6 @@
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;
import org.apache.poi.ss.usermodel.Sheet;

public class FromExcel<T> {

Expand Down Expand Up @@ -42,6 +43,12 @@ public List<T> toList() {
return result;
}

public List<String> readSheetNames() {
validateSource();
validateOptions();
return source.getDeserializer(options).readSheetNames();
}

public Stream<T> toStream() {
validate();
final Stream<T> stream = source.getDeserializer(options).stream(javaType);
Expand All @@ -53,17 +60,29 @@ public Stream<T> toStream() {
}

private void validate() {
if (source == null) {
throw new PoijiException("Source must be set");
}
validateSource();
validateJavaType();
validateOptions();
}

private void validateJavaType() {
if (javaType == null) {
throw new PoijiException("Class must be set");
}
}

private void validateOptions() {
if (options == null){
options = PoijiOptions.PoijiOptionsBuilder.settings().build();
}
}

private void validateSource() {
if (source == null) {
throw new PoijiException("Source must be set");
}
}

public FromExcel<T> withConsumer(final Consumer<? super T> consumer) {
this.consumer = consumer;
return this;
Expand Down Expand Up @@ -173,4 +192,4 @@ public Unmarshaller getDeserializer(final PoijiOptions options) {
}

}
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/poiji/bind/Unmarshaller.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.poiji.bind;

import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;

Expand All @@ -11,4 +12,6 @@ public interface Unmarshaller {
<T> void unmarshal(Class<T> type, Consumer<? super T> consumer);

<T> Stream<T> stream(Class<T> type);

List<String> readSheetNames();
}
26 changes: 21 additions & 5 deletions src/main/java/com/poiji/bind/mapping/CsvUnmarshallerStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import com.poiji.bind.Unmarshaller;
import com.poiji.exception.PoijiException;
import com.poiji.option.PoijiOptions;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import com.poiji.util.ReflectUtil;

import java.io.*;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
Expand Down Expand Up @@ -47,6 +49,20 @@ public <T> Stream<T> stream(final Class<T> type) {
}
}

/**
* Excel uses file name as sheet name for CSV format.
*/
@Override
public List<String> readSheetNames() {
final InputStream stream = poijiStream.stream();
if (stream instanceof FileInputStream) {
final String path = ReflectUtil.getFieldData("path", stream);
final String fileName = Paths.get(path).getFileName().toString();
return Collections.singletonList(fileName.substring(0, fileName.lastIndexOf(".")));
}
return options.preferNullOverDefault() ? null : Collections.emptyList();
}

private static class BomInputStream extends InputStream{

private final InputStream inner;
Expand All @@ -73,7 +89,7 @@ public Optional<String> getCharset(){
}

@Override
public int read() throws IOException {
public int read() {
return 0;
}

Expand Down
29 changes: 17 additions & 12 deletions src/main/java/com/poiji/bind/mapping/HSSFUnmarshaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,16 @@
import com.poiji.exception.PoijiException;
import com.poiji.option.PoijiOptions;
import com.poiji.save.TransposeUtil;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.BaseFormulaEvaluator;
import org.apache.poi.ss.usermodel.*;

import java.io.IOException;
import java.util.Iterator;
import java.util.Optional;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.BaseFormulaEvaluator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

/**
* This is the main class that converts the excel sheet fromExcel Java object
Expand Down Expand Up @@ -47,6 +41,17 @@ public <T> void unmarshal(Class<T> type, Consumer<? super T> consumer) {
}
}

@Override
public List<String> readSheetNames() {
try (final HSSFWorkbook workbook = (HSSFWorkbook) workbook()) {
final List<String> result = new ArrayList<>();
workbook.forEach(sheet-> result.add(sheet.getSheetName()));
return result;
} catch (final IOException e) {
throw new PoijiException("Problem occurred while closing HSSFWorkbook", e);
}
}

private <T> Sheet getSheet(final Class<T> type, final HSSFWorkbook workbook) {
if (options.getTransposed()){
TransposeUtil.transpose(workbook);
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/com/poiji/bind/mapping/SheetUnmarshaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

import com.poiji.exception.PoijiException;
import com.poiji.option.PoijiOptions;
import java.util.function.Consumer;
import java.util.stream.Stream;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;

/**
* Created by hakan on 11.10.2020
*/
Expand All @@ -29,6 +32,11 @@ public <T> void unmarshal(Class<T> type, Consumer<? super T> consumer) {
processRowsToObjects(sheet, type, consumer);
}

@Override
public List<String> readSheetNames() {
return Collections.singletonList(sheet.getSheetName());
}

private void setBaseFormulaEvaluator() {
Workbook workbook = workbook();
if (workbook instanceof HSSFWorkbook) {
Expand Down
Loading

0 comments on commit 3054ad5

Please sign in to comment.