Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,21 @@ private CSVParser csvParser() throws IOException {
// As a fallback, build the CSV parser using the input stream.
return buildCsvParser(csvFormat, csvReadWorkbookHolder.getInputStream(), byteOrderMark);
}

/**
* Builds and returns a CSVParser instance based on the provided CSVFormat, InputStream, and ByteOrderMarkEnum.
*
* @param csvFormat The format configuration for parsing the CSV file.
* @param inputStream The input stream from which the CSV data will be read.
* @param byteOrderMark The enumeration representing the Byte Order Mark (BOM) of the file's character set.
* @return A CSVParser instance configured to parse the CSV data.
* @throws IOException If an I/O error occurs while creating the parser or reading from the input stream.
*
* <p>
* This method checks if the byteOrderMark is null. If it is null, it creates a CSVParser using the provided
* input stream and charset. Otherwise, it wraps the input stream with a BOMInputStream to handle files with a
* Byte Order Mark, ensuring proper decoding of the file content.
* </p>
*
* @param csvFormat The format configuration for parsing the CSV file.
* @param inputStream The input stream from which the CSV data will be read.
* @param byteOrderMark The enumeration representing the Byte Order Mark (BOM) of the file's character set.
* @return A CSVParser instance configured to parse the CSV data.
* @throws IOException If an I/O error occurs while creating the parser or reading from the input stream.
*/
private CSVParser buildCsvParser(CSVFormat csvFormat, InputStream inputStream, ByteOrderMarkEnum byteOrderMark)
throws IOException {
Expand All @@ -173,26 +176,28 @@ private CSVParser buildCsvParser(CSVFormat csvFormat, InputStream inputStream, B
/**
* Processes a single CSV record and maps its content to a structured format for further analysis.
*
* @param record The CSV record to be processed.
* @param record The CSV record to be processed.
* @param rowIndex The index of the current row being processed.
* This method performs the following steps:
* 1. Initializes a `LinkedHashMap` to store cell data, ensuring the order of columns is preserved.
* 2. Iterates through each cell in the CSV record using an iterator.
* 3. For each cell, creates a `ReadCellData` object and sets its metadata (row index, column index, type, and value).
* - If the cell is not blank, it is treated as a string and optionally trimmed based on the `autoTrim` configuration.
* - If the cell is blank, it is marked as empty.
* 4. Adds the processed cell data to the `cellMap`.
* 5. Determines the row type: if the `cellMap` is empty, the row is marked as `EMPTY`; otherwise, it is marked as `DATA`.
* 6. Creates a `ReadRowHolder` object with the row's metadata and cell map, and stores it in the context.
* 7. Updates the context's sheet holder with the cell map and row index.
* 8. Notifies the analysis event processor that the row processing has ended.
* This method performs the following steps:
* 1. Initializes a `LinkedHashMap` to store cell data, ensuring the order of columns is preserved.
* 2. Iterates through each cell in the CSV record using an iterator.
* 3. For each cell, creates a `ReadCellData` object and sets its metadata (row index, column index, type, and value).
* - If the cell is not blank, it is treated as a string and optionally trimmed based on the `autoTrim` configuration.
* - If the cell is blank, it is marked as empty.
* 4. Adds the processed cell data to the `cellMap`.
* 5. Determines the row type: if the `cellMap` is empty, the row is marked as `EMPTY`; otherwise, it is marked as `DATA`.
* 6. Creates a `ReadRowHolder` object with the row's metadata and cell map, and stores it in the context.
* 7. Updates the context's sheet holder with the cell map and row index.
* 8. Notifies the analysis event processor that the row processing has ended.
*/
private void dealRecord(CSVRecord record, int rowIndex) {
Map<Integer, Cell> cellMap = new LinkedHashMap<>();
Iterator<String> cellIterator = record.iterator();
int columnIndex = 0;
Boolean autoTrim =
csvReadContext.currentReadHolder().globalConfiguration().getAutoTrim();
csvReadContext.csvReadWorkbookHolder().globalConfiguration().getAutoTrim();
Boolean autoStrip =
csvReadContext.csvReadWorkbookHolder().globalConfiguration().getAutoStrip();
while (cellIterator.hasNext()) {
String cellString = cellIterator.next();
ReadCellData<String> readCellData = new ReadCellData<>();
Expand All @@ -202,7 +207,13 @@ private void dealRecord(CSVRecord record, int rowIndex) {
// csv is an empty string of whether <code>,,</code> is read or <code>,"",</code>
if (StringUtils.isNotBlank(cellString)) {
readCellData.setType(CellDataTypeEnum.STRING);
readCellData.setStringValue(autoTrim ? cellString.trim() : cellString);
if (autoStrip) {
readCellData.setStringValue(StringUtils.strip(cellString));
} else if (autoTrim) {
readCellData.setStringValue(cellString.trim());
} else {
readCellData.setStringValue(cellString);
}
} else {
readCellData.setType(CellDataTypeEnum.EMPTY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import cn.idev.excel.analysis.v03.IgnorableXlsRecordHandler;
import cn.idev.excel.context.xls.XlsReadContext;
import cn.idev.excel.enums.RowTypeEnum;
import cn.idev.excel.metadata.GlobalConfiguration;
import cn.idev.excel.metadata.data.ReadCellData;
import cn.idev.excel.util.StringUtils;
import org.apache.poi.hssf.record.LabelRecord;
import org.apache.poi.hssf.record.Record;

Expand All @@ -15,9 +17,14 @@ public class LabelRecordHandler extends AbstractXlsRecordHandler implements Igno
public void processRecord(XlsReadContext xlsReadContext, Record record) {
LabelRecord lrec = (LabelRecord) record;
String data = lrec.getValue();
if (data != null
&& xlsReadContext.currentReadHolder().globalConfiguration().getAutoTrim()) {
data = data.trim();
if (data != null) {
GlobalConfiguration globalConfiguration =
xlsReadContext.currentReadHolder().globalConfiguration();
if (globalConfiguration.getAutoStrip()) {
data = StringUtils.strip(data);
} else if (globalConfiguration.getAutoTrim()) {
data = data.trim();
}
}
xlsReadContext
.xlsReadSheetHolder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import cn.idev.excel.context.xls.XlsReadContext;
import cn.idev.excel.enums.RowTypeEnum;
import cn.idev.excel.metadata.Cell;
import cn.idev.excel.metadata.GlobalConfiguration;
import cn.idev.excel.metadata.data.ReadCellData;
import cn.idev.excel.util.StringUtils;
import java.util.Map;
import org.apache.poi.hssf.record.LabelSSTRecord;
import org.apache.poi.hssf.record.Record;
Expand All @@ -31,7 +33,12 @@ public void processRecord(XlsReadContext xlsReadContext, Record record) {
(int) lsrec.getColumn(), ReadCellData.newEmptyInstance(lsrec.getRow(), (int) lsrec.getColumn()));
return;
}
if (xlsReadContext.currentReadHolder().globalConfiguration().getAutoTrim()) {

GlobalConfiguration globalConfiguration =
xlsReadContext.currentReadHolder().globalConfiguration();
if (globalConfiguration.getAutoStrip()) {
data = StringUtils.strip(data);
} else if (globalConfiguration.getAutoTrim()) {
data = data.trim();
}
cellMap.put((int) lsrec.getColumn(), ReadCellData.newInstance(data, lsrec.getRow(), (int) lsrec.getColumn()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cn.idev.excel.constant.FastExcelConstants;
import cn.idev.excel.context.xlsx.XlsxReadContext;
import cn.idev.excel.enums.CellDataTypeEnum;
import cn.idev.excel.metadata.GlobalConfiguration;
import cn.idev.excel.metadata.data.ReadCellData;
import cn.idev.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder;
import cn.idev.excel.util.BooleanUtils;
Expand Down Expand Up @@ -95,9 +96,14 @@ public void endElement(XlsxReadContext xlsxReadContext, String name) {
throw new IllegalStateException("Cannot set values now");
}

if (tempCellData.getStringValue() != null
&& xlsxReadContext.currentReadHolder().globalConfiguration().getAutoTrim()) {
tempCellData.setStringValue(tempCellData.getStringValue().trim());
if (tempCellData.getStringValue() != null) {
GlobalConfiguration globalConfiguration =
xlsxReadContext.currentReadHolder().globalConfiguration();
if (globalConfiguration.getAutoStrip()) {
tempCellData.setStringValue(StringUtils.strip(tempCellData.getStringValue()));
} else if (globalConfiguration.getAutoTrim()) {
tempCellData.setStringValue(tempCellData.getStringValue().trim());
}
}

tempCellData.checkEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ public AbstractHolder(BasicParameter basicParameter, AbstractHolder prentAbstrac
globalConfiguration.setAutoTrim(basicParameter.getAutoTrim());
}

if (basicParameter.getAutoStrip() == null) {
if (prentAbstractHolder != null) {
globalConfiguration.setAutoStrip(
prentAbstractHolder.getGlobalConfiguration().getAutoStrip());
}
} else {
globalConfiguration.setAutoStrip(basicParameter.getAutoStrip());
}

if (basicParameter.getUse1904windowing() == null) {
if (prentAbstractHolder != null) {
globalConfiguration.setUse1904windowing(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public T registerConverter(Converter<?> converter) {

/**
* true if date uses 1904 windowing, or false if using 1900 date windowing.
*
* <p>
* default is false
*
* @param use1904windowing
Expand All @@ -83,7 +83,7 @@ public T locale(Locale locale) {

/**
* The cache used when parsing fields such as head.
*
* <p>
* default is THREAD_LOCAL.
*
* @since 3.3.0
Expand All @@ -104,6 +104,17 @@ public T autoTrim(Boolean autoTrim) {
return self();
}

/**
* Automatic strip includes sheet name and content
*
* @param autoStrip
* @return
*/
public T autoStrip(Boolean autoStrip) {
parameter().setAutoStrip(autoStrip);
return self();
}

@SuppressWarnings("unchecked")
protected T self() {
return (T) this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,8 @@ public class BasicParameter {
* default is THREAD_LOCAL.
*/
private CacheLocationEnum filedCacheLocation;
/**
* Automatic strip includes sheet name and content
*/
private Boolean autoStrip;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public class GlobalConfiguration {
* true if date uses 1904 windowing, or false if using 1900 date windowing.
*
* default is false
*
* @return
*/
private Boolean use1904windowing;
/**
Expand All @@ -47,8 +45,16 @@ public class GlobalConfiguration {
*/
private CacheLocationEnum filedCacheLocation;

/**
* Automatic strip includes sheet name and content
*
* default is false
*/
private Boolean autoStrip;

public GlobalConfiguration() {
this.autoTrim = Boolean.TRUE;
this.autoStrip = Boolean.FALSE;
this.use1904windowing = Boolean.FALSE;
this.locale = Locale.getDefault();
this.useScientificFormat = Boolean.FALSE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ public CsvReaderBuilder escape(Character escape) {
}

private ExcelReader buildExcelReader() {
if (this.readWorkbook.getAutoTrim() != null) {
this.csvFormatBuilder.setTrim(this.readWorkbook.getAutoTrim());
}
this.csvFormatBuilder.setTrim(this.readWorkbook.getAutoTrim() == null
|| this.readWorkbook.getAutoTrim()
|| Boolean.TRUE.equals(this.readWorkbook.getAutoStrip()));
if (this.readWorkbook.getIgnoreEmptyRow() != null) {
this.csvFormatBuilder.setIgnoreEmptyLines(this.readWorkbook.getIgnoreEmptyRow());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public void copyBasicParameter(ReadSheet other) {
this.setClazz(other.getClazz());
this.setCustomConverterList(other.getCustomConverterList());
this.setAutoTrim(other.getAutoTrim());
this.setAutoStrip(other.getAutoStrip());
this.setUse1904windowing(other.getUse1904windowing());
this.setNumRows(other.getNumRows());
this.setHidden(other.isHidden());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,15 @@ private void buildHead(AnalysisContext analysisContext, Map<Integer, ReadCellDat
if (StringUtils.isEmpty(headString)) {
continue;
}
if (analysisContext.currentReadHolder().globalConfiguration().getAutoTrim()) {
if (analysisContext.currentReadHolder().globalConfiguration().getAutoStrip()) {
headString = StringUtils.strip(headString);
} else if (analysisContext
.currentReadHolder()
.globalConfiguration()
.getAutoTrim()) {
headString = headString.trim();
}

if (headName.equals(headString)) {
headData.setColumnIndex(stringKey);
tmpHeadMap.put(stringKey, headData);
Expand Down
64 changes: 64 additions & 0 deletions fastexcel/src/main/java/cn/idev/excel/util/ParameterUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package cn.idev.excel.util;

import cn.idev.excel.context.AnalysisContext;
import cn.idev.excel.context.WriteContext;
import cn.idev.excel.read.metadata.ReadSheet;
import cn.idev.excel.write.metadata.WriteSheet;

/**
* Parameter Util
*/
public class ParameterUtil {

/**
* Get autoTrim flag for read
*
* @param readSheet actual read sheet
* @param context Analysis Context
* @return autoTrim flag
*/
public static boolean getAutoTrimFlag(ReadSheet readSheet, AnalysisContext context) {
return (readSheet.getAutoTrim() != null && readSheet.getAutoTrim())
|| (readSheet.getAutoTrim() == null
&& context.readWorkbookHolder().getGlobalConfiguration().getAutoTrim());
}

/**
* Get autoStrip flag for read
*
* @param readSheet actual read sheet
* @param context Analysis Context
* @return autoStrip flag
*/
public static boolean getAutoStripFlag(ReadSheet readSheet, AnalysisContext context) {
return (readSheet.getAutoStrip() != null && readSheet.getAutoStrip())
|| context.readWorkbookHolder().getGlobalConfiguration().getAutoStrip();
}

/**
* Get autoTrim flag for write
*
* @param writeSheet actual write sheet
* @param context Write Context
* @return autoTrim flag
*/
public static boolean getAutoTrimFlag(WriteSheet writeSheet, WriteContext context) {
return (writeSheet.getAutoTrim() != null && writeSheet.getAutoTrim())
|| (writeSheet.getAutoTrim() == null
&& context.writeWorkbookHolder()
.getGlobalConfiguration()
.getAutoTrim());
}

/**
* Get autoStrip flag for write
*
* @param writeSheet actual write sheet
* @param context Write Context
* @return autoStrip flag
*/
public static boolean getAutoStripFlag(WriteSheet writeSheet, WriteContext context) {
return (writeSheet.getAutoStrip() != null && writeSheet.getAutoStrip())
|| context.writeWorkbookHolder().getGlobalConfiguration().getAutoStrip();
}
}
22 changes: 12 additions & 10 deletions fastexcel/src/main/java/cn/idev/excel/util/SheetUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,20 @@ public static ReadSheet match(ReadSheet readSheet, AnalysisContext analysisConte
if (!match) {
String parameterSheetName = parameterReadSheet.getSheetName();
if (!StringUtils.isEmpty(parameterSheetName)) {
boolean autoTrim = (parameterReadSheet.getAutoTrim() != null && parameterReadSheet.getAutoTrim())
|| (parameterReadSheet.getAutoTrim() == null
&& analysisContext
.readWorkbookHolder()
.getGlobalConfiguration()
.getAutoTrim());
String sheetName = readSheet.getSheetName();
if (autoTrim) {
parameterSheetName = parameterSheetName.trim();
sheetName = sheetName.trim();
if (sheetName != null) {
boolean autoStrip = ParameterUtil.getAutoStripFlag(parameterReadSheet, analysisContext);
boolean autoTrim = ParameterUtil.getAutoTrimFlag(parameterReadSheet, analysisContext);

if (autoStrip) {
parameterSheetName = StringUtils.strip(parameterSheetName);
sheetName = StringUtils.strip(sheetName);
} else if (autoTrim) {
parameterSheetName = parameterSheetName.trim();
sheetName = sheetName.trim();
}
match = parameterSheetName.equals(sheetName);
}
match = parameterSheetName.equals(sheetName);
}
}
if (match) {
Expand Down
Loading