From 29b077ec0db40bd68238b953961c26bf2e2b81f3 Mon Sep 17 00:00:00 2001 From: sramazzina Date: Fri, 24 Jan 2025 15:04:48 +0100 Subject: [PATCH] fix #4818 --- .../transforms/concatfields/ConcatFields.java | 32 +++++++++++++++++-- .../concatfields/ConcatFieldsData.java | 3 ++ .../concatfields/ConcatFieldsMeta.java | 4 +++ 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/plugins/transforms/concatfields/src/main/java/org/apache/hop/pipeline/transforms/concatfields/ConcatFields.java b/plugins/transforms/concatfields/src/main/java/org/apache/hop/pipeline/transforms/concatfields/ConcatFields.java index aac55e7ef65..dd20777b7c5 100644 --- a/plugins/transforms/concatfields/src/main/java/org/apache/hop/pipeline/transforms/concatfields/ConcatFields.java +++ b/plugins/transforms/concatfields/src/main/java/org/apache/hop/pipeline/transforms/concatfields/ConcatFields.java @@ -157,7 +157,8 @@ private Object[] concatFields(Object[] inputRowData) throws HopException { nullString = Const.NVL(field.getNullString(), ""); } - concatField(targetString, valueMeta, valueData, nullString); + String trimType = data.trimType[i]; + concatField(targetString, valueMeta, valueData, nullString, trimType); } outputRowData[data.outputRowMeta.size() - 1] = targetString.toString(); @@ -166,13 +167,32 @@ private Object[] concatFields(Object[] inputRowData) throws HopException { } private void concatField( - StringBuilder targetField, IValueMeta valueMeta, Object valueData, String nullString) + StringBuilder targetField, + IValueMeta valueMeta, + Object valueData, + String nullString, + String trimType) throws HopValueException { if (valueMeta.isNull(valueData)) { targetField.append(nullString); } else { - targetField.append(valueMeta.getString(valueData)); + if (trimType != null) { + switch (trimType) { + case ConcatFieldsMeta.TRIM_TYPE_LEFT: + targetField.append(valueMeta.getString(valueData).replaceAll("^\\s+", "")); + break; + case ConcatFieldsMeta.TRIM_TYPE_RIGHT: + targetField.append(valueMeta.getString(valueData).replaceAll("\\s+$", "")); + break; + case ConcatFieldsMeta.TRIM_TYPE_BOTH: + targetField.append(valueMeta.getString(valueData).trim()); + break; + default: + targetField.append(valueMeta.getString(valueData)); + break; + } + } } } @@ -220,12 +240,18 @@ private void initStringDataFields() { } data.stringNullValue = new String[meta.getOutputFields().size()]; + data.trimType = new String[meta.getOutputFields().size()]; + for (int i = 0; i < meta.getOutputFields().size(); i++) { data.stringNullValue[i] = ""; String nullString = meta.getOutputFields().get(i).getNullString(); if (!StringUtil.isEmpty(nullString)) { data.stringNullValue[i] = nullString; } + String trimType = meta.getOutputFields().get(i).getTrimType(); + if (!StringUtil.isEmpty(trimType)) { + data.trimType[i] = trimType; + } } } diff --git a/plugins/transforms/concatfields/src/main/java/org/apache/hop/pipeline/transforms/concatfields/ConcatFieldsData.java b/plugins/transforms/concatfields/src/main/java/org/apache/hop/pipeline/transforms/concatfields/ConcatFieldsData.java index db0ae9ef7c6..be89feba108 100644 --- a/plugins/transforms/concatfields/src/main/java/org/apache/hop/pipeline/transforms/concatfields/ConcatFieldsData.java +++ b/plugins/transforms/concatfields/src/main/java/org/apache/hop/pipeline/transforms/concatfields/ConcatFieldsData.java @@ -37,6 +37,9 @@ public class ConcatFieldsData extends BaseTransformData implements ITransformDat public String stringSeparator; public String stringEnclosure; public String[] stringNullValue; + + public String[] trimType; + public int targetFieldLength; // for fast data dump (StringBuilder size) public ConcatFieldsData() { diff --git a/plugins/transforms/concatfields/src/main/java/org/apache/hop/pipeline/transforms/concatfields/ConcatFieldsMeta.java b/plugins/transforms/concatfields/src/main/java/org/apache/hop/pipeline/transforms/concatfields/ConcatFieldsMeta.java index dc7057ad5e8..5668b8dfb38 100644 --- a/plugins/transforms/concatfields/src/main/java/org/apache/hop/pipeline/transforms/concatfields/ConcatFieldsMeta.java +++ b/plugins/transforms/concatfields/src/main/java/org/apache/hop/pipeline/transforms/concatfields/ConcatFieldsMeta.java @@ -51,6 +51,10 @@ public class ConcatFieldsMeta extends BaseTransformMeta { private static final Class PKG = ConcatFieldsMeta.class; + public static final String TRIM_TYPE_LEFT = "left"; + public static final String TRIM_TYPE_RIGHT = "right"; + public static final String TRIM_TYPE_BOTH = "both"; + /** The separator to choose for the CSV file */ @HopMetadataProperty(key = "separator", injectionKey = "SEPARATOR") private String separator;