From b88d3948c472b1fa7b48723f0c07a1c722c21bea Mon Sep 17 00:00:00 2001 From: Paul Vogel Date: Tue, 2 Feb 2021 13:00:15 +0100 Subject: [PATCH] Remove dependency org.apache.commons.* from CcuGateway Remove dependencies org.apache.commons.lang.StringUtils and org.apache.commons.lang.ObjectUtils from CcuGateway Signed-off-by: Paul Vogel --- .../internal/communicator/CcuGateway.java | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/bundles/org.openhab.binding.homematic/src/main/java/org/openhab/binding/homematic/internal/communicator/CcuGateway.java b/bundles/org.openhab.binding.homematic/src/main/java/org/openhab/binding/homematic/internal/communicator/CcuGateway.java index 7e3835611cab8..de8a6829679f6 100644 --- a/bundles/org.openhab.binding.homematic/src/main/java/org/openhab/binding/homematic/internal/communicator/CcuGateway.java +++ b/bundles/org.openhab.binding.homematic/src/main/java/org/openhab/binding/homematic/internal/communicator/CcuGateway.java @@ -20,8 +20,6 @@ import java.util.Map; import java.util.concurrent.TimeUnit; -import org.apache.commons.lang.ObjectUtils; -import org.apache.commons.lang.StringUtils; import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.api.ContentResponse; import org.eclipse.jetty.client.util.StringContentProvider; @@ -123,7 +121,7 @@ protected void setChannelDatapointValues(HmChannel channel, HmParamsetType param HmDevice device = channel.getDevice(); String channelName = String.format("%s.%s:%s.", device.getHmInterface().getName(), device.getAddress(), channel.getNumber()); - String datapointNames = StringUtils.join(dpNames.toArray(), "\\t"); + String datapointNames = String.join("\\t", dpNames); TclScriptDataList resultList = sendScriptByName("getAllChannelValues", TclScriptDataList.class, new String[] { "channel_name", "datapoint_names" }, new String[] { channelName, datapointNames }); @@ -151,7 +149,10 @@ protected void addChannelDatapoints(HmChannel channel, HmParamsetType paramsetTy @Override protected void setVariable(HmDatapoint dp, Object value) throws IOException { - String strValue = StringUtils.replace(ObjectUtils.toString(value), "\"", "\\\""); + String strValue = ""; + if (value != null) { + strValue = value.toString().replace("\"", "\\\""); + } if (dp.isStringType()) { strValue = "\"" + strValue + "\""; } @@ -185,7 +186,9 @@ private T sendScriptByName(String scriptName, Class clazz, String[] varia throws IOException { String script = tclregaScripts.get(scriptName); for (int i = 0; i < variableNames.length; i++) { - script = StringUtils.replace(script, "{" + variableNames[i] + "}", values[i]); + if (script != null) { + script = script.replace("{" + variableNames[i] + "}", values[i]); + } } return sendScript(script, clazz); } @@ -196,8 +199,8 @@ private T sendScriptByName(String scriptName, Class clazz, String[] varia @SuppressWarnings("unchecked") private synchronized T sendScript(String script, Class clazz) throws IOException { try { - script = StringUtils.trim(script); - if (StringUtils.isEmpty(script)) { + script = script == null ? null : script.trim(); + if (script == null || script.length() == 0) { throw new RuntimeException("Homematic TclRegaScript is empty!"); } if (logger.isTraceEnabled()) { @@ -210,7 +213,14 @@ private synchronized T sendScript(String script, Class clazz) throws IOEx .header(HttpHeader.CONTENT_TYPE, "text/plain;charset=" + config.getEncoding()).send(); String result = new String(response.getContent(), config.getEncoding()); - result = StringUtils.substringBeforeLast(result, ""); + if (result.length() != 0) { + String separator = ""; + // Gets the substring before the last occurrence of the separator. + int pos = result.lastIndexOf(separator); + if (pos != -1) { + result = result.substring(0, pos); + } + } if (logger.isTraceEnabled()) { logger.trace("Result TclRegaScript: {}", result); } @@ -231,7 +241,10 @@ private Map loadTclRegaScripts() throws IOException { Map result = new HashMap<>(); if (scriptList.getScripts() != null) { for (TclScript script : scriptList.getScripts()) { - result.put(script.name, StringUtils.trimToNull(script.data)); + // trim data to null (if it's empty) + String data = script.data == null ? null : script.data.trim(); + data = (data == null || data.length() == 0) ? null : data; + result.put(script.name, data); } } return result;