2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.kaleidescape.internal.communication;
15 import static org.openhab.binding.kaleidescape.internal.KaleidescapeBindingConstants.*;
17 import org.apache.commons.lang3.StringEscapeUtils;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
21 * The {@link KaleidescapeFormatter} is a utility class with formatting methods for Kaleidescape strings
23 * @author Michael Lobstein - Initial contribution
26 public class KaleidescapeFormatter {
27 public static String formatString(String input) {
28 if (!EMPTY.equals(input)) {
29 // convert || back to :
30 input = input.replace("||", ":");
32 // if input does not have any escaped characters, bypass all the replace()'s
33 if (input.contains("\\")) {
35 input = input.replace("\\:", ":");
38 input = input.replace("\\/", "/");
40 // convert \r into comma space
41 input = input.replace("\\r", ", ");
43 // convert \d146 from review text into apostrophe
44 input = input.replace("\\d146", "'");
45 // convert \d147 & \d148 from review text into double quote
46 input = input.replace("\\d147", "\"");
47 input = input.replace("\\d148", "\"");
49 // fix the encoding for k mangled extended ascii characters (chars coming in as \dnnn)
50 // I.e. characters with accent, umlaut, etc., they need to be restored to the correct character
51 // example: Noel (with umlaut 'o') comes in as N\d246el
52 input = input.replaceAll("(?i)\\\\d([0-9]{3})", "\\&#$1;"); // first convert to html escaped codes
53 // then convert with unescapeHtml4, not sure how to do this without the Apache libraries :(
54 return StringEscapeUtils.unescapeHtml4(input);