From 53f35d7cb759d14c26c8a8f27e050098ff8b9bc7 Mon Sep 17 00:00:00 2001 From: lsiepel Date: Sat, 18 Feb 2023 22:28:45 +0100 Subject: [PATCH 1/6] Remove org.apache.common Signed-off-by: lsiepel --- .../org/openhab/binding/exec/internal/handler/ExecHandler.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/handler/ExecHandler.java b/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/handler/ExecHandler.java index cf9402d0de23a..698d8d356056b 100644 --- a/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/handler/ExecHandler.java +++ b/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/handler/ExecHandler.java @@ -29,7 +29,6 @@ import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; -import org.apache.commons.lang3.StringUtils; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.exec.internal.ExecWhitelistWatchService; @@ -289,7 +288,7 @@ public void execute() { outputBuilder.append(errorBuilder.toString()); - String transformedResponse = StringUtils.chomp(outputBuilder.toString()); + String transformedResponse = outputBuilder.toString().replaceAll("\r", "").replaceAll("\n", ""); String transformation = (String) getConfig().get(TRANSFORM); if (transformation != null && transformation.length() > 0) { From 4aef7139238aa25398378b913ef692ca18a66549 Mon Sep 17 00:00:00 2001 From: lsiepel Date: Wed, 26 Jul 2023 15:12:16 +0200 Subject: [PATCH 2/6] Fix chomp implementation including tests Signed-off-by: lsiepel --- .../exec/internal/handler/ExecHandler.java | 5 +- .../binding/exec/internal/util/Util.java | 61 +++++++++++++++++++ .../binding/exec/internal/util/UtilTests.java | 41 +++++++++++++ 3 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/util/Util.java create mode 100644 bundles/org.openhab.binding.exec/src/test/java/org/openhab/binding/exec/internal/util/UtilTests.java diff --git a/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/handler/ExecHandler.java b/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/handler/ExecHandler.java index 698d8d356056b..58b7b3c63b64a 100644 --- a/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/handler/ExecHandler.java +++ b/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/handler/ExecHandler.java @@ -32,6 +32,7 @@ import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.exec.internal.ExecWhitelistWatchService; +import org.openhab.binding.exec.internal.util.Util; import org.openhab.core.library.types.DateTimeType; import org.openhab.core.library.types.DecimalType; import org.openhab.core.library.types.OnOffType; @@ -288,10 +289,10 @@ public void execute() { outputBuilder.append(errorBuilder.toString()); - String transformedResponse = outputBuilder.toString().replaceAll("\r", "").replaceAll("\n", ""); + String transformedResponse = Util.chomp(outputBuilder.toString()); String transformation = (String) getConfig().get(TRANSFORM); - if (transformation != null && transformation.length() > 0) { + if (transformation != null && transformation.length() > 0 && transformedResponse != null) { transformedResponse = transformResponse(transformedResponse, transformation); } diff --git a/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/util/Util.java b/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/util/Util.java new file mode 100644 index 0000000000000..f0e4763ec18e6 --- /dev/null +++ b/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/util/Util.java @@ -0,0 +1,61 @@ +/** + * Copyright (c) 2010-2023 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.exec.internal.util; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; + +/** + * Class for utility methods + * + * @author Leo Siepel - Initial contribution + */ + +@NonNullByDefault +public class Util { + + /** + *

+ * If a newline char exists at the end of the line it is removed + *

+ * + *
+     * Util.chomp(null)          = null
+     * Util.chomp("")            = ""
+     * Util.chomp("abc \r")      = "abc "
+     * Util.chomp("abc\n")       = "abc"
+     * Util.chomp("abc\r\n")     = "abc"
+     * Util.chomp("abc\r\n\r\n") = "abc\r\n"
+     * Util.chomp("abc\n\r")     = "abc\n"
+     * Util.chomp("abc\n\rabc")  = "abc\n\rabc"
+     * Util.chomp("\r")          = ""
+     * Util.chomp("\n")          = ""
+     * Util.chomp("\r\n")        = ""
+     * 
+ * + * @param str nullable string + * @return chomped string (nullable) + */ + public static @Nullable String chomp(final @Nullable String str) { + if (str == null || str.isEmpty()) { + return str; + } + if (str.endsWith("\r\n")) { + return str.substring(0, str.length() - 2); + } else if (str.endsWith("\r") || str.endsWith("\n")) { + return str.substring(0, str.length() - 1); + } else { + return str; + } + } +} diff --git a/bundles/org.openhab.binding.exec/src/test/java/org/openhab/binding/exec/internal/util/UtilTests.java b/bundles/org.openhab.binding.exec/src/test/java/org/openhab/binding/exec/internal/util/UtilTests.java new file mode 100644 index 0000000000000..a1357f688edb2 --- /dev/null +++ b/bundles/org.openhab.binding.exec/src/test/java/org/openhab/binding/exec/internal/util/UtilTests.java @@ -0,0 +1,41 @@ +/** + * Copyright (c) 2010-2023 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.exec.internal.util; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +/** + * Test class for utility methods. + * + * @author Leo Siepel - Initial contribution + */ + +public class UtilTests { + + @Test + public void chompTest() { + assertEquals("", Util.chomp("")); + assertEquals(null, Util.chomp(null)); + assertEquals("abc ", Util.chomp("abc \r")); + assertEquals("abc", Util.chomp("abc\n")); + assertEquals("abc", Util.chomp("abc\r\n")); + assertEquals("abc\r\n", Util.chomp("abc\r\n\r\n")); + assertEquals("abc\n", Util.chomp("abc\n\r")); + assertEquals("abc\n\rabc", Util.chomp("abc\n\rabc")); + assertEquals("", Util.chomp("\r")); + assertEquals("", Util.chomp("\n")); + assertEquals("", Util.chomp("\r\n")); + } +} From 79ca1f582490dab9943818baf5533687dc65f881 Mon Sep 17 00:00:00 2001 From: lsiepel Date: Fri, 28 Jul 2023 19:58:40 +0200 Subject: [PATCH 3/6] Improve class name Signed-off-by: lsiepel --- .../exec/internal/handler/ExecHandler.java | 4 ++-- .../util/{Util.java => StringUtils.java} | 2 +- .../{UtilTests.java => StringUtilsTests.java} | 24 +++++++++---------- 3 files changed, 15 insertions(+), 15 deletions(-) rename bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/util/{Util.java => StringUtils.java} (98%) rename bundles/org.openhab.binding.exec/src/test/java/org/openhab/binding/exec/internal/util/{UtilTests.java => StringUtilsTests.java} (50%) diff --git a/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/handler/ExecHandler.java b/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/handler/ExecHandler.java index 58b7b3c63b64a..d6680d12abd82 100644 --- a/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/handler/ExecHandler.java +++ b/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/handler/ExecHandler.java @@ -32,7 +32,7 @@ import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.exec.internal.ExecWhitelistWatchService; -import org.openhab.binding.exec.internal.util.Util; +import org.openhab.binding.exec.internal.util.StringUtils; import org.openhab.core.library.types.DateTimeType; import org.openhab.core.library.types.DecimalType; import org.openhab.core.library.types.OnOffType; @@ -289,7 +289,7 @@ public void execute() { outputBuilder.append(errorBuilder.toString()); - String transformedResponse = Util.chomp(outputBuilder.toString()); + String transformedResponse = StringUtils.chomp(outputBuilder.toString()); String transformation = (String) getConfig().get(TRANSFORM); if (transformation != null && transformation.length() > 0 && transformedResponse != null) { diff --git a/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/util/Util.java b/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/util/StringUtils.java similarity index 98% rename from bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/util/Util.java rename to bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/util/StringUtils.java index f0e4763ec18e6..66ebb4a27ff65 100644 --- a/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/util/Util.java +++ b/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/util/StringUtils.java @@ -22,7 +22,7 @@ */ @NonNullByDefault -public class Util { +public class StringUtils { /** *

diff --git a/bundles/org.openhab.binding.exec/src/test/java/org/openhab/binding/exec/internal/util/UtilTests.java b/bundles/org.openhab.binding.exec/src/test/java/org/openhab/binding/exec/internal/util/StringUtilsTests.java similarity index 50% rename from bundles/org.openhab.binding.exec/src/test/java/org/openhab/binding/exec/internal/util/UtilTests.java rename to bundles/org.openhab.binding.exec/src/test/java/org/openhab/binding/exec/internal/util/StringUtilsTests.java index a1357f688edb2..039c5d9daacb9 100644 --- a/bundles/org.openhab.binding.exec/src/test/java/org/openhab/binding/exec/internal/util/UtilTests.java +++ b/bundles/org.openhab.binding.exec/src/test/java/org/openhab/binding/exec/internal/util/StringUtilsTests.java @@ -22,20 +22,20 @@ * @author Leo Siepel - Initial contribution */ -public class UtilTests { +public class StringUtilsTests { @Test public void chompTest() { - assertEquals("", Util.chomp("")); - assertEquals(null, Util.chomp(null)); - assertEquals("abc ", Util.chomp("abc \r")); - assertEquals("abc", Util.chomp("abc\n")); - assertEquals("abc", Util.chomp("abc\r\n")); - assertEquals("abc\r\n", Util.chomp("abc\r\n\r\n")); - assertEquals("abc\n", Util.chomp("abc\n\r")); - assertEquals("abc\n\rabc", Util.chomp("abc\n\rabc")); - assertEquals("", Util.chomp("\r")); - assertEquals("", Util.chomp("\n")); - assertEquals("", Util.chomp("\r\n")); + assertEquals("", StringUtils.chomp("")); + assertEquals(null, StringUtils.chomp(null)); + assertEquals("abc ", StringUtils.chomp("abc \r")); + assertEquals("abc", StringUtils.chomp("abc\n")); + assertEquals("abc", StringUtils.chomp("abc\r\n")); + assertEquals("abc\r\n", StringUtils.chomp("abc\r\n\r\n")); + assertEquals("abc\n", StringUtils.chomp("abc\n\r")); + assertEquals("abc\n\rabc", StringUtils.chomp("abc\n\rabc")); + assertEquals("", StringUtils.chomp("\r")); + assertEquals("", StringUtils.chomp("\n")); + assertEquals("", StringUtils.chomp("\r\n")); } } From ce8cd80ac51bcca57773f8c64aaf5561489ee5e6 Mon Sep 17 00:00:00 2001 From: Leo Siepel Date: Sun, 15 Oct 2023 21:08:00 +0200 Subject: [PATCH 4/6] Adapt to core StringUtils Signed-off-by: Leo Siepel --- .../exec/internal/handler/ExecHandler.java | 2 +- .../exec/internal/util/StringUtils.java | 61 ------------------- .../exec/internal/util/StringUtilsTests.java | 41 ------------- 3 files changed, 1 insertion(+), 103 deletions(-) delete mode 100644 bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/util/StringUtils.java delete mode 100644 bundles/org.openhab.binding.exec/src/test/java/org/openhab/binding/exec/internal/util/StringUtilsTests.java diff --git a/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/handler/ExecHandler.java b/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/handler/ExecHandler.java index d6680d12abd82..873050546d639 100644 --- a/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/handler/ExecHandler.java +++ b/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/handler/ExecHandler.java @@ -32,7 +32,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.exec.internal.ExecWhitelistWatchService; -import org.openhab.binding.exec.internal.util.StringUtils; import org.openhab.core.library.types.DateTimeType; import org.openhab.core.library.types.DecimalType; import org.openhab.core.library.types.OnOffType; @@ -46,6 +45,7 @@ import org.openhab.core.transform.TransformationService; import org.openhab.core.types.Command; import org.openhab.core.types.RefreshType; +import org.openhab.core.util; import org.osgi.framework.BundleContext; import org.osgi.framework.FrameworkUtil; import org.slf4j.Logger; diff --git a/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/util/StringUtils.java b/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/util/StringUtils.java deleted file mode 100644 index 66ebb4a27ff65..0000000000000 --- a/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/util/StringUtils.java +++ /dev/null @@ -1,61 +0,0 @@ -/** - * Copyright (c) 2010-2023 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.exec.internal.util; - -import org.eclipse.jdt.annotation.NonNullByDefault; -import org.eclipse.jdt.annotation.Nullable; - -/** - * Class for utility methods - * - * @author Leo Siepel - Initial contribution - */ - -@NonNullByDefault -public class StringUtils { - - /** - *

- * If a newline char exists at the end of the line it is removed - *

- * - *
-     * Util.chomp(null)          = null
-     * Util.chomp("")            = ""
-     * Util.chomp("abc \r")      = "abc "
-     * Util.chomp("abc\n")       = "abc"
-     * Util.chomp("abc\r\n")     = "abc"
-     * Util.chomp("abc\r\n\r\n") = "abc\r\n"
-     * Util.chomp("abc\n\r")     = "abc\n"
-     * Util.chomp("abc\n\rabc")  = "abc\n\rabc"
-     * Util.chomp("\r")          = ""
-     * Util.chomp("\n")          = ""
-     * Util.chomp("\r\n")        = ""
-     * 
- * - * @param str nullable string - * @return chomped string (nullable) - */ - public static @Nullable String chomp(final @Nullable String str) { - if (str == null || str.isEmpty()) { - return str; - } - if (str.endsWith("\r\n")) { - return str.substring(0, str.length() - 2); - } else if (str.endsWith("\r") || str.endsWith("\n")) { - return str.substring(0, str.length() - 1); - } else { - return str; - } - } -} diff --git a/bundles/org.openhab.binding.exec/src/test/java/org/openhab/binding/exec/internal/util/StringUtilsTests.java b/bundles/org.openhab.binding.exec/src/test/java/org/openhab/binding/exec/internal/util/StringUtilsTests.java deleted file mode 100644 index 039c5d9daacb9..0000000000000 --- a/bundles/org.openhab.binding.exec/src/test/java/org/openhab/binding/exec/internal/util/StringUtilsTests.java +++ /dev/null @@ -1,41 +0,0 @@ -/** - * Copyright (c) 2010-2023 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.exec.internal.util; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.Test; - -/** - * Test class for utility methods. - * - * @author Leo Siepel - Initial contribution - */ - -public class StringUtilsTests { - - @Test - public void chompTest() { - assertEquals("", StringUtils.chomp("")); - assertEquals(null, StringUtils.chomp(null)); - assertEquals("abc ", StringUtils.chomp("abc \r")); - assertEquals("abc", StringUtils.chomp("abc\n")); - assertEquals("abc", StringUtils.chomp("abc\r\n")); - assertEquals("abc\r\n", StringUtils.chomp("abc\r\n\r\n")); - assertEquals("abc\n", StringUtils.chomp("abc\n\r")); - assertEquals("abc\n\rabc", StringUtils.chomp("abc\n\rabc")); - assertEquals("", StringUtils.chomp("\r")); - assertEquals("", StringUtils.chomp("\n")); - assertEquals("", StringUtils.chomp("\r\n")); - } -} From 2787fc125a54a2e47619fab01b84234955aeabe1 Mon Sep 17 00:00:00 2001 From: Leo Siepel Date: Mon, 16 Oct 2023 22:41:01 +0200 Subject: [PATCH 5/6] Fix import Signed-off-by: Leo Siepel --- .../org/openhab/binding/exec/internal/handler/ExecHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/handler/ExecHandler.java b/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/handler/ExecHandler.java index 873050546d639..a2e1ac5a10f38 100644 --- a/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/handler/ExecHandler.java +++ b/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/handler/ExecHandler.java @@ -45,7 +45,7 @@ import org.openhab.core.transform.TransformationService; import org.openhab.core.types.Command; import org.openhab.core.types.RefreshType; -import org.openhab.core.util; +import org.openhab.core.util.StringUtils; import org.osgi.framework.BundleContext; import org.osgi.framework.FrameworkUtil; import org.slf4j.Logger; From 98bd5555135b5dd756d369b63b71f11f41a04e19 Mon Sep 17 00:00:00 2001 From: Leo Siepel Date: Mon, 16 Oct 2023 22:44:06 +0200 Subject: [PATCH 6/6] Improve null check Signed-off-by: Leo Siepel --- .../openhab/binding/exec/internal/handler/ExecHandler.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/handler/ExecHandler.java b/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/handler/ExecHandler.java index a2e1ac5a10f38..d978ba99cac30 100644 --- a/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/handler/ExecHandler.java +++ b/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/handler/ExecHandler.java @@ -23,6 +23,7 @@ import java.util.Calendar; import java.util.Date; import java.util.IllegalFormatException; +import java.util.Objects; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; @@ -289,10 +290,10 @@ public void execute() { outputBuilder.append(errorBuilder.toString()); - String transformedResponse = StringUtils.chomp(outputBuilder.toString()); + String transformedResponse = Objects.requireNonNull(StringUtils.chomp(outputBuilder.toString())); String transformation = (String) getConfig().get(TRANSFORM); - if (transformation != null && transformation.length() > 0 && transformedResponse != null) { + if (transformation != null && transformation.length() > 0) { transformedResponse = transformResponse(transformedResponse, transformation); }