From 6ff8f04c5ad18cd5730cb209546f127aef2c9d4b Mon Sep 17 00:00:00 2001 From: psainics Date: Tue, 16 Jan 2024 15:21:08 +0530 Subject: [PATCH] [s] Using parseInt to validate Sheet_Number --- .../xls/input/XlsInputFormatProvider.java | 36 +++++++++++++++---- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/format-xls/src/main/java/io/cdap/plugin/format/xls/input/XlsInputFormatProvider.java b/format-xls/src/main/java/io/cdap/plugin/format/xls/input/XlsInputFormatProvider.java index 4622edbc6..4b98cf74f 100644 --- a/format-xls/src/main/java/io/cdap/plugin/format/xls/input/XlsInputFormatProvider.java +++ b/format-xls/src/main/java/io/cdap/plugin/format/xls/input/XlsInputFormatProvider.java @@ -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; @@ -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.") @@ -95,7 +100,9 @@ public void validate(FormatContext context) { @Override protected void addFormatProperties(Map 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)); @@ -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()); }