Skip to content

Commit

Permalink
[s] Using parseInt to validate Sheet_Number
Browse files Browse the repository at this point in the history
  • Loading branch information
psainics committed Jan 16, 2024
1 parent dc3b008 commit 6ff8f04
Showing 1 changed file with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.cdap.plugin.format.xls.input;

import com.google.common.base.Strings;
import io.cdap.cdap.api.annotation.Description;
import io.cdap.cdap.api.annotation.Name;
import io.cdap.cdap.api.annotation.Plugin;
Expand Down Expand Up @@ -80,11 +81,15 @@ public void validate(FormatContext context) {
Schema schema = super.getSchema(context);
FailureCollector collector = context.getFailureCollector();
// When the sheet is specified by number, the sheet value must be a number
if (!conf.containsMacro(XlsInputFormatConfig.NAME_SHEET_VALUE)
&& conf.getSheet().equals(XlsInputFormatConfig.SHEET_NUMBER)
&& (conf.getSheetValue() == null || !conf.getSheetValue().matches("[0-9]+"))) {
collector.addFailure("Sheet number must be a number.", null)
.withConfigProperty(XlsInputFormatConfig.NAME_SHEET_VALUE);
if (!conf.containsMacro(XlsInputFormatConfig.NAME_SHEET_VALUE) &&
conf.getSheet().equals(XlsInputFormatConfig.SHEET_NUMBER) &&
!Strings.isNullOrEmpty(conf.getSheetValue())) {
try {
Integer.parseInt(conf.getSheetValue());
} catch (NumberFormatException e) {
collector.addFailure("Sheet number must be a number.", null)
.withConfigProperty(XlsInputFormatConfig.NAME_SHEET_VALUE);
}
}
if (!conf.containsMacro(PathTrackingConfig.NAME_SCHEMA) && schema == null && context.getInputSchema() == null) {
collector.addFailure("XLS format cannot be used without specifying a schema.", "Schema must be specified.")
Expand All @@ -95,7 +100,9 @@ public void validate(FormatContext context) {
@Override
protected void addFormatProperties(Map<String, String> properties) {
properties.put(XlsInputFormat.SHEET_NO, conf.getSheet());
properties.put(XlsInputFormat.SHEET_VALUE, conf.getSheetValue());
if (!Strings.isNullOrEmpty(conf.getSheetValue())) {
properties.put(XlsInputFormat.SHEET_VALUE, conf.getSheetValue());
}
properties.put(XlsInputFormat.NAME_SKIP_HEADER, String.valueOf(conf.getSkipHeader()));
properties.put(XlsInputFormat.TERMINATE_IF_EMPTY_ROW, String.valueOf(conf.getTerminateIfEmptyRow()));
properties.put(FileInputFormat.SPLIT_MINSIZE, Long.toString(Long.MAX_VALUE));
Expand All @@ -115,8 +122,23 @@ public Schema detectSchema(FormatContext context, InputFiles inputFiles) throws
Sheet workSheet;
// Check if user wants to access with name or number
if (conf.getSheet() != null && conf.getSheet().equals(XlsInputFormatConfig.SHEET_NUMBER)) {
workSheet = workbook.getSheetAt(Integer.parseInt(Objects.requireNonNull(conf.getSheetValue())));
int sheetValue = 0;
if (!Strings.isNullOrEmpty(conf.getSheetValue())) {
try {
sheetValue = Integer.parseInt(conf.getSheetValue());
} catch (NumberFormatException e) {
failureCollector.addFailure("Sheet number must be a number.", null)
.withConfigProperty(XlsInputFormatConfig.NAME_SHEET_VALUE);
return null;
}
}
workSheet = workbook.getSheetAt(sheetValue);
} else {
if (Strings.isNullOrEmpty(conf.getSheetValue())) {
failureCollector.addFailure("Sheet name must be specified.", null)
.withConfigProperty(XlsInputFormatConfig.NAME_SHEET_VALUE);
return null;
}
workSheet = workbook.getSheet(conf.getSheetValue());
}

Expand Down

0 comments on commit 6ff8f04

Please sign in to comment.