diff --git a/README.adoc b/README.adoc
index 4e544d1..a64912f 100644
--- a/README.adoc
+++ b/README.adoc
@@ -14,7 +14,7 @@ In your Maven/Gradle project, first add the corresponding dependency:
io.github.vaa25
poiji2
- 1.4.1
+ 1.5.0
org.apache.poi
@@ -29,7 +29,7 @@ In your Maven/Gradle project, first add the corresponding dependency:
[source,groovy]
----
dependencies {
- implementation 'io.github.vaa25:poiji2:1.4.1'
+ implementation 'io.github.vaa25:poiji2:1.5.0'
implementation 'org.apache.poi:poi-ooxml:5.2.5'
}
----
@@ -52,3 +52,4 @@ Also:
- Poiji2 can read and write huge xlsx files (see HugeTest.java)
- Poiji2 (since v1.4.0) can work with immutable java classes (see IgnoreTest.java). lombok @Value and java records applicable also.
- Poiji2 (since v1.4.1) can work with immutable java classes with many constructors (see ExcelConstructorTest.java). Apply @ExcelConstructor to choose one of.
+- Poiji2 (since v1.5.0) can read sheet names (see ReadSheetNamesTest.java).
diff --git a/src/main/java/com/poiji/bind/FromExcel.java b/src/main/java/com/poiji/bind/FromExcel.java
index d189b41..68240f6 100644
--- a/src/main/java/com/poiji/bind/FromExcel.java
+++ b/src/main/java/com/poiji/bind/FromExcel.java
@@ -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;
@@ -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 {
@@ -42,6 +43,12 @@ public List toList() {
return result;
}
+ public List readSheetNames() {
+ validateSource();
+ validateOptions();
+ return source.getDeserializer(options).readSheetNames();
+ }
+
public Stream toStream() {
validate();
final Stream stream = source.getDeserializer(options).stream(javaType);
@@ -53,17 +60,29 @@ public Stream 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 withConsumer(final Consumer super T> consumer) {
this.consumer = consumer;
return this;
@@ -173,4 +192,4 @@ public Unmarshaller getDeserializer(final PoijiOptions options) {
}
}
-}
\ No newline at end of file
+}
diff --git a/src/main/java/com/poiji/bind/Unmarshaller.java b/src/main/java/com/poiji/bind/Unmarshaller.java
index 5c71e73..5baff30 100644
--- a/src/main/java/com/poiji/bind/Unmarshaller.java
+++ b/src/main/java/com/poiji/bind/Unmarshaller.java
@@ -1,5 +1,6 @@
package com.poiji.bind;
+import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;
@@ -11,4 +12,6 @@ public interface Unmarshaller {
void unmarshal(Class type, Consumer super T> consumer);
Stream stream(Class type);
+
+ List readSheetNames();
}
diff --git a/src/main/java/com/poiji/bind/mapping/CsvUnmarshallerStream.java b/src/main/java/com/poiji/bind/mapping/CsvUnmarshallerStream.java
index 1e44bf9..e66dc18 100644
--- a/src/main/java/com/poiji/bind/mapping/CsvUnmarshallerStream.java
+++ b/src/main/java/com/poiji/bind/mapping/CsvUnmarshallerStream.java
@@ -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;
@@ -47,6 +49,20 @@ public Stream stream(final Class type) {
}
}
+ /**
+ * Excel uses file name as sheet name for CSV format.
+ */
+ @Override
+ public List 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;
@@ -73,7 +89,7 @@ public Optional getCharset(){
}
@Override
- public int read() throws IOException {
+ public int read() {
return 0;
}
diff --git a/src/main/java/com/poiji/bind/mapping/HSSFUnmarshaller.java b/src/main/java/com/poiji/bind/mapping/HSSFUnmarshaller.java
index ee1c13e..40e46f1 100644
--- a/src/main/java/com/poiji/bind/mapping/HSSFUnmarshaller.java
+++ b/src/main/java/com/poiji/bind/mapping/HSSFUnmarshaller.java
@@ -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
@@ -47,6 +41,17 @@ public void unmarshal(Class type, Consumer super T> consumer) {
}
}
+ @Override
+ public List readSheetNames() {
+ try (final HSSFWorkbook workbook = (HSSFWorkbook) workbook()) {
+ final List 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 Sheet getSheet(final Class type, final HSSFWorkbook workbook) {
if (options.getTransposed()){
TransposeUtil.transpose(workbook);
diff --git a/src/main/java/com/poiji/bind/mapping/ReadMappedFields.java b/src/main/java/com/poiji/bind/mapping/ReadMappedFields.java
index b782d3d..1e6ac09 100644
--- a/src/main/java/com/poiji/bind/mapping/ReadMappedFields.java
+++ b/src/main/java/com/poiji/bind/mapping/ReadMappedFields.java
@@ -1,26 +1,15 @@
package com.poiji.bind.mapping;
-import com.poiji.annotation.ExcelCell;
-import com.poiji.annotation.ExcelCellName;
-import com.poiji.annotation.ExcelCellRange;
-import com.poiji.annotation.ExcelList;
-import com.poiji.annotation.ExcelParseExceptions;
-import com.poiji.annotation.ExcelRow;
-import com.poiji.annotation.ExcelUnknownCells;
-import com.poiji.annotation.ExcelWriteOnly;
+import com.poiji.annotation.*;
import com.poiji.config.Casting;
import com.poiji.exception.ExcelParseException;
import com.poiji.option.PoijiOptions;
import com.poiji.util.AnnotationUtil;
import com.poiji.util.ReflectUtil;
+
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
import static com.poiji.annotation.ExcelCellName.ABSENT_ORDER;
import static java.util.Arrays.asList;
@@ -127,7 +116,7 @@ public void setCellInData(final int row, final int column, final String content,
for (final Field unknownField : unknownFields) {
final Object unknownData = data.get(unknownField);
if (unknownData == null) {
- final Map map = new HashMap<>();
+ final Map map = ReflectUtil.newMap(unknownField);
data.put(unknownField, map);
map.put(unknownColumns.get(column), content);
} else {
@@ -235,7 +224,7 @@ private List getPossibleFieldNames(final ExcelCellName annotation) {
private List parseUnknownCells(final List fields) {
final List rest = new ArrayList<>(fields.size());
for (final Field field : fields) {
- if (field.getAnnotation(ExcelUnknownCells.class) != null && field.getType().isAssignableFrom(Map.class)) {
+ if (field.isAnnotationPresent(ExcelUnknownCells.class) && Map.class.isAssignableFrom(field.getType())) {
unknownFields.add(field);
ReflectUtil.setAccessible(field);
} else {
diff --git a/src/main/java/com/poiji/bind/mapping/SheetUnmarshaller.java b/src/main/java/com/poiji/bind/mapping/SheetUnmarshaller.java
index eb6c049..3f60d6e 100644
--- a/src/main/java/com/poiji/bind/mapping/SheetUnmarshaller.java
+++ b/src/main/java/com/poiji/bind/mapping/SheetUnmarshaller.java
@@ -2,8 +2,6 @@
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;
@@ -11,6 +9,11 @@
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
*/
@@ -29,6 +32,11 @@ public void unmarshal(Class type, Consumer super T> consumer) {
processRowsToObjects(sheet, type, consumer);
}
+ @Override
+ public List readSheetNames() {
+ return Collections.singletonList(sheet.getSheetName());
+ }
+
private void setBaseFormulaEvaluator() {
Workbook workbook = workbook();
if (workbook instanceof HSSFWorkbook) {
diff --git a/src/main/java/com/poiji/bind/mapping/XSSFUnmarshaller.java b/src/main/java/com/poiji/bind/mapping/XSSFUnmarshaller.java
index a24a732..4dd0071 100644
--- a/src/main/java/com/poiji/bind/mapping/XSSFUnmarshaller.java
+++ b/src/main/java/com/poiji/bind/mapping/XSSFUnmarshaller.java
@@ -4,18 +4,6 @@
import com.poiji.exception.PoijiException;
import com.poiji.option.PoijiOptions;
import com.poiji.save.TransposeUtil;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.util.List;
-import java.util.Locale;
-import java.util.Optional;
-import java.util.Spliterator;
-import java.util.Spliterators;
-import java.util.function.Consumer;
-import java.util.stream.Stream;
-import java.util.stream.StreamSupport;
-import javax.xml.parsers.ParserConfigurationException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.DocumentFactoryHelper;
@@ -34,6 +22,15 @@
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
+import javax.xml.parsers.ParserConfigurationException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.*;
+import java.util.function.Consumer;
+import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
+
import static org.apache.poi.openxml4j.opc.PackageAccess.READ_WRITE;
/**
@@ -47,78 +44,84 @@ abstract class XSSFUnmarshaller implements Unmarshaller {
this.options = options;
}
- protected void unmarshal0(Class type, Consumer super T> consumer, OPCPackage open)
- throws ParserConfigurationException, IOException, SAXException, OpenXML4JException
- {
- if (options.getTransposed()) {
- if (open.getPackageAccess() == READ_WRITE) {
- final XSSFWorkbook workbook = new XSSFWorkbook(open);
- TransposeUtil.transpose(workbook);
- workbook.write(new OutputStream() {
- @Override
- public void write(final int b) {
- }
- });
- } else {
- throw new UnsupportedOperationException("Can't apply transposition for streamed XLSX source");
- }
- }
-
- ReadOnlySharedStringsTable readOnlySharedStringsTable = new ReadOnlySharedStringsTable(open);
- XSSFReader workbookReader = new XSSFReader(open);
- StylesTable styles = workbookReader.getStylesTable();
- XMLReader reader = XMLHelper.newXMLReader();
-
- InputSource is = new InputSource(workbookReader.getWorkbookData());
+ @Override
+ public void unmarshal(Class type, Consumer super T> consumer) {
+ openFileAndExecute(opcPackage -> unmarshal0(type, consumer, opcPackage));
+ }
- reader.setContentHandler(new WorkBookContentHandler(options));
- reader.parse(is);
+ protected void unmarshal0(Class type, Consumer super T> consumer, OPCPackage open) {
+ try {
+ if (options.getTransposed()) {
+ if (open.getPackageAccess() == READ_WRITE) {
+ final XSSFWorkbook workbook = new XSSFWorkbook(open);
+ TransposeUtil.transpose(workbook);
+ workbook.write(new OutputStream() {
+ @Override
+ public void write(final int b) {
+ }
+ });
+ } else {
+ throw new UnsupportedOperationException("Can't apply transposition for streamed XLSX source");
+ }
+ }
+ ReadOnlySharedStringsTable readOnlySharedStringsTable = new ReadOnlySharedStringsTable(open);
+ XSSFReader workbookReader = new XSSFReader(open);
+ StylesTable styles = workbookReader.getStylesTable();
+ XMLReader reader = XMLHelper.newXMLReader();
- WorkBookContentHandler wbch = (WorkBookContentHandler) reader.getContentHandler();
- List sheets = wbch.getSheets();
- if (sheets.isEmpty()) {
- throw new PoijiException("no excel sheets found");
- }
- PoijiNumberFormat poijiNumberFormat = options.getPoijiNumberFormat();
- if (poijiNumberFormat != null) {
- poijiNumberFormat.overrideExcelNumberFormats(styles);
- }
+ InputSource is = new InputSource(workbookReader.getWorkbookData());
- SheetIterator iter = (SheetIterator) workbookReader.getSheetsData();
- int sheetCounter = 0;
+ reader.setContentHandler(new WorkBookContentHandler(options));
+ reader.parse(is);
- Optional maybeSheetName = SheetNameExtractor.getSheetName(type, options);
+ WorkBookContentHandler wbch = (WorkBookContentHandler) reader.getContentHandler();
+ List sheets = wbch.getSheets();
+ if (sheets.isEmpty()) {
+ throw new PoijiException("no excel sheets found");
+ }
+ PoijiNumberFormat poijiNumberFormat = options.getPoijiNumberFormat();
+ if (poijiNumberFormat != null) {
+ poijiNumberFormat.overrideExcelNumberFormats(styles);
+ }
- if (!maybeSheetName.isPresent()) {
- int requestedIndex = options.sheetIndex();
- int nonHiddenSheetIndex = 0;
- while (iter.hasNext()) {
- try (InputStream stream = iter.next()) {
- WorkBookSheet wbs = sheets.get(sheetCounter);
- if (wbs.getState().equals("visible")) {
- if (nonHiddenSheetIndex == requestedIndex) {
- processSheet(styles, reader, readOnlySharedStringsTable, type, stream, consumer);
- return;
+ SheetIterator iter = (SheetIterator) workbookReader.getSheetsData();
+ int sheetCounter = 0;
+
+ Optional maybeSheetName = SheetNameExtractor.getSheetName(type, options);
+
+ if (!maybeSheetName.isPresent()) {
+ int requestedIndex = options.sheetIndex();
+ int nonHiddenSheetIndex = 0;
+ while (iter.hasNext()) {
+ try (InputStream stream = iter.next()) {
+ WorkBookSheet wbs = sheets.get(sheetCounter);
+ if (wbs.getState().equals("visible")) {
+ if (nonHiddenSheetIndex == requestedIndex) {
+ processSheet(styles, reader, readOnlySharedStringsTable, type, stream, consumer);
+ return;
+ }
+ nonHiddenSheetIndex++;
}
- nonHiddenSheetIndex++;
}
+ sheetCounter++;
}
- sheetCounter++;
- }
- } else {
- String sheetName = maybeSheetName.get();
- while (iter.hasNext()) {
- try (InputStream stream = iter.next()) {
- WorkBookSheet wbs = sheets.get(sheetCounter);
- if (wbs.getState().equals("visible")) {
- if (iter.getSheetName().equalsIgnoreCase(sheetName)) {
- processSheet(styles, reader, readOnlySharedStringsTable, type, stream, consumer);
- return;
+ } else {
+ String sheetName = maybeSheetName.get();
+ while (iter.hasNext()) {
+ try (InputStream stream = iter.next()) {
+ WorkBookSheet wbs = sheets.get(sheetCounter);
+ if (wbs.getState().equals("visible")) {
+ if (iter.getSheetName().equalsIgnoreCase(sheetName)) {
+ processSheet(styles, reader, readOnlySharedStringsTable, type, stream, consumer);
+ return;
+ }
}
}
+ sheetCounter++;
}
- sheetCounter++;
}
+ } catch (IOException | OpenXML4JException | ParserConfigurationException | SAXException e) {
+ throw new PoijiException("Problem occurred while reading data: " + e.getMessage(), e);
}
}
@@ -268,15 +271,13 @@ private Stream streamSheet(
}
- protected final void listOfEncryptedItems(Class type, Consumer super T> consumer, POIFSFileSystem fs) throws IOException {
- InputStream stream = DocumentFactoryHelper.getDecryptedStream(fs, options.getPassword());
-
- try (OPCPackage open = OPCPackage.open(stream)) {
- unmarshal0(type, consumer, open);
-
- } catch (ParserConfigurationException | SAXException | IOException | OpenXML4JException e) {
- IOUtils.closeQuietly(fs);
- throw new PoijiException("Problem occurred while reading data", e);
+ protected final void applyInEncryptedOpcPackage(Consumer process, POIFSFileSystem fileSystem) {
+ try (InputStream stream = DocumentFactoryHelper.getDecryptedStream(fileSystem, options.getPassword());
+ OPCPackage open = OPCPackage.open(stream)) {
+ process.accept(open);
+ } catch (IOException | OpenXML4JException e) {
+ IOUtils.closeQuietly(fileSystem);
+ throw new PoijiException("Problem occurred while reading data: " + e.getMessage(), e);
}
}
@@ -292,4 +293,22 @@ protected final Stream streamOfEncryptedItems(Class type, POIFSFileSys
}
}
+ protected abstract void openFileAndExecute(Consumer process);
+
+ @Override
+ public List readSheetNames() {
+ final List result = new ArrayList<>();
+ openFileAndExecute(opcPackage -> readSheetNames(opcPackage, result));
+ return result;
+ }
+
+ private void readSheetNames(OPCPackage opcPackage, List result) {
+ try {
+ final XSSFWorkbook workbook = new XSSFWorkbook(opcPackage);
+ workbook.forEach(sheet -> result.add(sheet.getSheetName()));
+ } catch (IOException e) {
+ throw new PoijiException("Problem occurred while reading data: " + e.getMessage(), e);
+ }
+ }
+
}
diff --git a/src/main/java/com/poiji/bind/mapping/XSSFUnmarshallerFile.java b/src/main/java/com/poiji/bind/mapping/XSSFUnmarshallerFile.java
index 8a1b67c..3ccd364 100644
--- a/src/main/java/com/poiji/bind/mapping/XSSFUnmarshallerFile.java
+++ b/src/main/java/com/poiji/bind/mapping/XSSFUnmarshallerFile.java
@@ -3,16 +3,17 @@
import com.poiji.bind.PoijiFile;
import com.poiji.exception.PoijiException;
import com.poiji.option.PoijiOptions;
-import java.io.IOException;
-import java.util.function.Consumer;
-import java.util.stream.Stream;
-import javax.xml.parsers.ParserConfigurationException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.xml.sax.SAXException;
+import javax.xml.parsers.ParserConfigurationException;
+import java.io.IOException;
+import java.util.function.Consumer;
+import java.util.stream.Stream;
+
import static org.apache.poi.openxml4j.opc.PackageAccess.READ;
import static org.apache.poi.openxml4j.opc.PackageAccess.READ_WRITE;
@@ -28,13 +29,13 @@ final class XSSFUnmarshallerFile extends XSSFUnmarshaller {
this.poijiFile = poijiFile;
}
- @Override
- public void unmarshal(Class type, Consumer super T> consumer) {
+
+ protected void openFileAndExecute(Consumer process) {
if (options.getPassword() != null) {
- returnFromEncryptedFile(type, consumer);
+ applyInFileSystem(fileSystem-> applyInEncryptedOpcPackage(process, fileSystem));
} else {
- returnFromExcelFile(type,consumer);
+ applyInOpcPackage(process);
}
}
@@ -52,27 +53,20 @@ public Stream stream(final Class type) {
} catch (ParserConfigurationException | SAXException | IOException | OpenXML4JException e) {
throw new PoijiException("Problem occurred while reading data", e);
}
-
}
- public void returnFromExcelFile(Class type, Consumer super T> consumer) {
+ private void applyInOpcPackage(Consumer process) {
final PackageAccess packageAccess = options.getTransposed() ? READ_WRITE : READ;
-
try (OPCPackage open = OPCPackage.open(poijiFile.file(), packageAccess)) {
-
- unmarshal0(type, consumer, open);
-
- } catch (ParserConfigurationException | SAXException | IOException | OpenXML4JException e) {
- throw new PoijiException("Problem occurred while reading data", e);
+ process.accept(open);
+ } catch (IOException | OpenXML4JException e) {
+ throw new PoijiException("Problem occurred while reading data: " + e.getMessage(), e);
}
}
- private void returnFromEncryptedFile(Class type, Consumer super T> consumer) {
-
+ private void applyInFileSystem(Consumer process) {
try (POIFSFileSystem fs = new POIFSFileSystem(poijiFile.file(), true)) {
-
- listOfEncryptedItems(type, consumer, fs);
-
+ process.accept(fs);
} catch (IOException e) {
throw new PoijiException("Problem occurred while reading data", e);
}
diff --git a/src/main/java/com/poiji/bind/mapping/XSSFUnmarshallerStream.java b/src/main/java/com/poiji/bind/mapping/XSSFUnmarshallerStream.java
index 6290be5..2a2597b 100644
--- a/src/main/java/com/poiji/bind/mapping/XSSFUnmarshallerStream.java
+++ b/src/main/java/com/poiji/bind/mapping/XSSFUnmarshallerStream.java
@@ -3,15 +3,16 @@
import com.poiji.bind.PoijiInputStream;
import com.poiji.exception.PoijiException;
import com.poiji.option.PoijiOptions;
-import java.io.IOException;
-import java.util.function.Consumer;
-import java.util.stream.Stream;
-import javax.xml.parsers.ParserConfigurationException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.xml.sax.SAXException;
+import javax.xml.parsers.ParserConfigurationException;
+import java.io.IOException;
+import java.util.function.Consumer;
+import java.util.stream.Stream;
+
/**
* Created by hakan on 22/10/2017
*/
@@ -24,16 +25,6 @@ final class XSSFUnmarshallerStream extends XSSFUnmarshaller {
this.poijiInputStream = poijiInputStream;
}
- @Override
- public void unmarshal(Class type, Consumer super T> consumer) {
-
- if (options.getPassword() != null) {
- returnFromEncryptedFile(type, consumer);
- } else {
- returnFromExcelFile(type, consumer);
- }
- }
-
@Override
public Stream stream(final Class type) {
try {
@@ -49,20 +40,28 @@ public Stream stream(final Class type) {
}
}
- private void returnFromExcelFile(Class type, Consumer super T> consumer) {
+ private void applyInOpcPackage(Consumer process) {
try (OPCPackage open = OPCPackage.open(poijiInputStream.stream())) {
- unmarshal0(type, consumer, open);
- } catch (ParserConfigurationException | SAXException | IOException | OpenXML4JException e) {
- throw new PoijiException("Problem occurred while reading data", e);
+ process.accept(open);
+ } catch (IOException | OpenXML4JException e) {
+ throw new PoijiException("Problem occurred while reading data: " + e.getMessage(), e);
}
}
- private void returnFromEncryptedFile(Class type, Consumer super T> consumer) {
- try (POIFSFileSystem fs = new POIFSFileSystem(poijiInputStream.stream())) {
- listOfEncryptedItems(type, consumer, fs);
+ private void applyInFileSystem(Consumer process) {
+ try (POIFSFileSystem fileSystem = new POIFSFileSystem(poijiInputStream.stream())) {
+ applyInEncryptedOpcPackage(process, fileSystem);
} catch (IOException e) {
throw new PoijiException("Problem occurred while reading data", e);
}
}
+ @Override
+ protected void openFileAndExecute(Consumer process) {
+ if (options.getPassword() != null) {
+ applyInFileSystem(process);
+ } else {
+ applyInOpcPackage(process);
+ }
+ }
}
diff --git a/src/main/java/com/poiji/util/ReflectUtil.java b/src/main/java/com/poiji/util/ReflectUtil.java
index 4d703c8..e28433f 100644
--- a/src/main/java/com/poiji/util/ReflectUtil.java
+++ b/src/main/java/com/poiji/util/ReflectUtil.java
@@ -5,14 +5,12 @@
import com.poiji.exception.IllegalCastException;
import com.poiji.exception.PoijiException;
import com.poiji.exception.PoijiInstantiationException;
+
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
public class ReflectUtil {
public static T newInstanceOf(Class type) {
@@ -170,4 +168,24 @@ public static void setAccessible(Field field) {
field.setAccessible(true);
}
}
+
+ public static T getFieldData(String fieldName, Object instance) {
+ try {
+ final Field field = instance.getClass().getDeclaredField(fieldName);
+ setAccessible(field);
+ return (T) field.get(instance);
+ } catch (IllegalAccessException e) {
+ throw new IllegalCastException("Unexpected cast type {");
+ } catch (NoSuchFieldException e) {
+ throw new PoijiException(e.getMessage(), e);
+ }
+ }
+
+ public static Map newMap(Field field) {
+ final Class> type = field.getType();
+ if (type == Map.class) {
+ return new LinkedHashMap<>();
+ }
+ return (Map) newInstanceOf(type);
+ }
}
diff --git a/src/test/java/com/poiji/deserialize/CaseInsensitiveTest.java b/src/test/java/com/poiji/deserialize/CaseInsensitiveTest.java
index f690d80..822ff22 100644
--- a/src/test/java/com/poiji/deserialize/CaseInsensitiveTest.java
+++ b/src/test/java/com/poiji/deserialize/CaseInsensitiveTest.java
@@ -3,12 +3,13 @@
import com.poiji.bind.Poiji;
import com.poiji.deserialize.model.byname.OrgWithUnknownCellsByName;
import com.poiji.option.PoijiOptions;
-import java.io.File;
-import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
+import java.io.File;
+import java.util.List;
+
import static java.util.Arrays.asList;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
@@ -46,16 +47,16 @@ public void caseInsensitiveColumnNames() {
.filter(org -> org.getId().equals("CrEaTe"))
.findFirst()
.get();
- assertThat(firstRow.getUnknownCells().size(), is(1));
- assertThat(firstRow.getUnknownCells().get("region"), is("EMEA"));
+ assertThat(firstRow.getSortedUnknownCells().size(), is(1));
+ assertThat(firstRow.getSortedUnknownCells().get("region"), is("EMEA"));
OrgWithUnknownCellsByName secondRow = organisations.stream()
.filter(org -> org.getId().equals("8d9e6430-8626-4556-8004-079085d2df2d"))
.findFirst()
.get();
- assertThat(secondRow.getUnknownCells().size(), is(1));
- assertThat(secondRow.getUnknownCells().get("region"), is("NA"));
+ assertThat(secondRow.getSortedUnknownCells().size(), is(1));
+ assertThat(secondRow.getSortedUnknownCells().get("region"), is("NA"));
}
}
diff --git a/src/test/java/com/poiji/deserialize/IgnoreWhitespacesTest.java b/src/test/java/com/poiji/deserialize/IgnoreWhitespacesTest.java
index 0450f0f..22176ab 100644
--- a/src/test/java/com/poiji/deserialize/IgnoreWhitespacesTest.java
+++ b/src/test/java/com/poiji/deserialize/IgnoreWhitespacesTest.java
@@ -3,12 +3,13 @@
import com.poiji.bind.Poiji;
import com.poiji.deserialize.model.byname.OrgWithUnknownCellsByName;
import com.poiji.option.PoijiOptions;
-import java.io.File;
-import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
+import java.io.File;
+import java.util.List;
+
import static java.util.Arrays.asList;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
@@ -51,16 +52,16 @@ public void ignoreWhitespaceColumnNames() {
.filter(org -> org.getId().equals("CrEaTe"))
.findFirst()
.get();
- assertThat(firstRow.getUnknownCells().size(), is(1));
- assertThat(firstRow.getUnknownCells().get("region"), is("EMEA"));
+ assertThat(firstRow.getSortedUnknownCells().size(), is(1));
+ assertThat(firstRow.getSortedUnknownCells().get("region"), is("EMEA"));
OrgWithUnknownCellsByName secondRow = organisations.stream()
.filter(org -> org.getId().equals("8d9e6430-8626-4556-8004-079085d2df2d"))
.findFirst()
.get();
- assertThat(secondRow.getUnknownCells().size(), is(1));
- assertThat(secondRow.getUnknownCells().get("region"), is("NA"));
+ assertThat(secondRow.getSortedUnknownCells().size(), is(1));
+ assertThat(secondRow.getSortedUnknownCells().get("region"), is("NA"));
}
}
diff --git a/src/test/java/com/poiji/deserialize/ReadExcelBySheetNameTest.java b/src/test/java/com/poiji/deserialize/ReadExcelBySheetNameTest.java
index 9ddf384..2ebfbb7 100644
--- a/src/test/java/com/poiji/deserialize/ReadExcelBySheetNameTest.java
+++ b/src/test/java/com/poiji/deserialize/ReadExcelBySheetNameTest.java
@@ -1,56 +1,47 @@
package com.poiji.deserialize;
-import static org.hamcrest.core.Is.is;
-import static org.junit.Assert.assertThat;
-
-import java.io.File;
-import java.time.LocalDate;
-import java.time.format.DateTimeFormatter;
-import java.time.format.DateTimeFormatterBuilder;
-import java.time.temporal.ChronoField;
-import java.util.Arrays;
-import java.util.List;
-
+import com.poiji.bind.Poiji;
+import com.poiji.deserialize.model.byid.Calculation;
+import com.poiji.option.PoijiOptions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
-import com.poiji.bind.Poiji;
-import com.poiji.deserialize.model.byid.Calculation;
-import com.poiji.option.PoijiOptions;
+import java.io.File;
+import java.util.List;
+
+import static java.util.Arrays.asList;
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
@RunWith(Parameterized.class)
public class ReadExcelBySheetNameTest {
-
- private String path;
-
- public ReadExcelBySheetNameTest(String path) {
- this.path = path;
- }
-
- @Parameterized.Parameters(name = "{index}: ({0})={1}")
- public static Iterable