From 96f51fd0ef113e9eccb4acfe0eed224a9f0b5973 Mon Sep 17 00:00:00 2001 From: Jan Lolling Date: Wed, 1 Feb 2023 13:48:26 +0100 Subject: [PATCH] Cutting filter in parts implemented --- pom.xml | 6 +- .../de/jlo/talendcomp/sap/TextSplitter.java | 49 +++++++++++++++ .../talendcomp/sap/impl/TableInputImpl.java | 23 +++---- .../talendcomp/sap/impl/TestTextSplitter.java | 63 +++++++++++++++++++ 4 files changed, 125 insertions(+), 16 deletions(-) create mode 100644 src/main/java/de/jlo/talendcomp/sap/TextSplitter.java create mode 100644 src/test/java/de/jlo/talendcomp/sap/impl/TestTextSplitter.java diff --git a/pom.xml b/pom.xml index e5dab7f..9c85702 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 de.jlo.talendcomp jlo-talendcomp-saprfc - 1.4 + 1.5 UTF-8 @@ -21,10 +21,6 @@ com.sap.jco sapjco 3.0.21 - system - - - ${sapjco3Path} org.apache.commons diff --git a/src/main/java/de/jlo/talendcomp/sap/TextSplitter.java b/src/main/java/de/jlo/talendcomp/sap/TextSplitter.java new file mode 100644 index 0000000..f908f0d --- /dev/null +++ b/src/main/java/de/jlo/talendcomp/sap/TextSplitter.java @@ -0,0 +1,49 @@ +package de.jlo.talendcomp.sap; + +import java.util.ArrayList; +import java.util.List; + +public class TextSplitter { + + public static List split(String text, char delimiter) { + List result = new ArrayList<>(); + if (text != null) { + text = text.trim(); + boolean inLiteral = false; + int i = 0; + StringBuilder sb = new StringBuilder(); + for (; i < text.length(); i++) { + char c = text.charAt(i); + if (c == '\'') { + if (inLiteral == false) { + inLiteral = true; + } else { + inLiteral = false; + } + } + if (inLiteral) { + sb.append(c); + } else { + if (c == delimiter) { + // found delimiter to cut + // but ignore 2 following delimiter + String part = sb.toString(); + if (part.trim().isEmpty() == false) { + result.add(part); + } + sb.setLength(0); // reset string buffer + } else { + sb.append(c); + } + } + } + String lastPart = sb.toString(); + if (lastPart.trim().isEmpty() == false) { + result.add(lastPart); + } + } + return result; + } + + +} diff --git a/src/main/java/de/jlo/talendcomp/sap/impl/TableInputImpl.java b/src/main/java/de/jlo/talendcomp/sap/impl/TableInputImpl.java index b3906f6..d21aa50 100644 --- a/src/main/java/de/jlo/talendcomp/sap/impl/TableInputImpl.java +++ b/src/main/java/de/jlo/talendcomp/sap/impl/TableInputImpl.java @@ -2,7 +2,6 @@ import java.util.ArrayList; import java.util.List; -import java.util.StringTokenizer; import org.apache.commons.text.StringEscapeUtils; @@ -18,6 +17,7 @@ import com.sap.conn.jco.rt.DefaultTable; import de.jlo.talendcomp.sap.TableInput; +import de.jlo.talendcomp.sap.TextSplitter; /** * Copyright 2023 Jan Lolling jan.lolling@gmail.com @@ -49,7 +49,7 @@ public class TableInputImpl implements TableInput { private Integer rowCount = null; private Integer rowSkip = null; private String functionDescription = null; - private String filter72Separator = "\\"; + private char filterPartSeparator = ';'; /** * Create an instance if TableInput @@ -113,18 +113,19 @@ public void execute() throws Exception { } JCoParameterList tableParameterList = function.getTableParameterList(); // add where condition - if (filter != null) { - if (filter.length() > 72) { - throw new Exception("Filter expression: <" + filter + "> is longer than 72 chars. The function does not allows filter expressions longer than 72 chars."); - } else { - JCoTable tableInputOptions = tableParameterList.getTable("OPTIONS"); - tableInputOptions.appendRows(1); - tableInputOptions.firstRow(); - tableInputOptions.setValue("TEXT", filter); + List filterPartList = TextSplitter.split(filter, filterPartSeparator); + if (filterPartList.size() > 0) { + JCoTable tableInputOptions = tableParameterList.getTable("OPTIONS"); + tableInputOptions.appendRows(filterPartList.size()); + tableInputOptions.firstRow(); + for (String part : filterPartList) { + if (part.length() > 72) { + throw new Exception("The filter expression: <" + filter + "> contains a part which is larger than 72 chars. Affected part: <"+ part + ">. Please split the filter with: <" + filterPartSeparator + "> into parts smaller than 72 chars"); + } + tableInputOptions.setValue("TEXT", part); tableInputOptions.nextRow(); } } - // add fields if (listFields.isEmpty()) { throw new Exception("List of expected fields cannot not be empty"); diff --git a/src/test/java/de/jlo/talendcomp/sap/impl/TestTextSplitter.java b/src/test/java/de/jlo/talendcomp/sap/impl/TestTextSplitter.java new file mode 100644 index 0000000..0337d07 --- /dev/null +++ b/src/test/java/de/jlo/talendcomp/sap/impl/TestTextSplitter.java @@ -0,0 +1,63 @@ +package de.jlo.talendcomp.sap.impl; + +import static org.junit.Assert.assertTrue; + +import java.util.List; + +import org.junit.Test; + +import de.jlo.talendcomp.sap.TextSplitter; + +public class TestTextSplitter { + + @Test + public void test1() { + String input = "';';T1;T2; ;T3"; + List result = TextSplitter.split(input, ';'); + for (String s : result) { + System.out.println(">" + s); + } + assertTrue("Count parts incorrect: " + result.size(), result.size() == 4); + } + + @Test + public void test2() { + String input = "';'"; + List result = TextSplitter.split(input, ';'); + for (String s : result) { + System.out.println(">" + s); + } + assertTrue("Count parts incorrect: " + result.size(), result.size() == 1); + } + + @Test + public void test3() { + String input = "abcd efg"; + List result = TextSplitter.split(input, ';'); + for (String s : result) { + System.out.println(">" + s); + } + assertTrue("Count parts incorrect: " + result.size(), result.size() == 1); + } + + @Test + public void test4() { + String input = ""; + List result = TextSplitter.split(input, ';'); + for (String s : result) { + System.out.println(">" + s); + } + assertTrue("Count parts incorrect: " + result.size(), result.size() == 0); + } + + @Test + public void test5() { + String input = " ; "; + List result = TextSplitter.split(input, ';'); + for (String s : result) { + System.out.println(">" + s); + } + assertTrue("Count parts incorrect: " + result.size(), result.size() == 0); + } + +}