]> git.basschouten.com Git - openhab-addons.git/blob
62b466b43b9e7412185a589a7ea456d0bb1d5685
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.kaleidescape.internal.communication;
14
15 import static org.openhab.binding.kaleidescape.internal.KaleidescapeBindingConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18
19 /**
20  * The {@link KaleidescapeFormatter} is a utility class with formatting methods for Kaleidescape strings
21  *
22  * @author Michael Lobstein - Initial contribution
23  */
24 @NonNullByDefault
25 public class KaleidescapeFormatter {
26     private static final String WITH_DELIMITER = "((?<=\\\\d[0-9]{3})|(?=\\\\d[0-9]{3}))";
27
28     public static String formatString(String input) {
29         if (!EMPTY.equals(input)) {
30             // convert || back to :
31             input = input.replace("||", ":");
32
33             // if input does not have any escaped characters, bypass all the replace()'s
34             if (input.contains("\\")) {
35                 // fix escaped :
36                 input = input.replace("\\:", ":");
37
38                 // fix escaped /
39                 input = input.replace("\\/", "/");
40
41                 // convert \r into comma space
42                 input = input.replace("\\r", ", ");
43
44                 // convert \d146 from review text into apostrophe
45                 input = input.replace("\\d146", "'");
46                 // convert \d147 & \d148 from review text into double quote
47                 input = input.replace("\\d147", "\"");
48                 input = input.replace("\\d148", "\"");
49             }
50
51             // fix the encoding for k mangled extended ascii characters (chars coming in as \dnnn)
52             // I.e. characters with accent, umlaut, etc., they need to be restored to the correct character
53             // example: Noel (with umlaut 'o') comes in as N\d246el
54             if (input.contains("\\d")) {
55                 StringBuilder fixedOutput = new StringBuilder();
56                 String[] arr = input.split(WITH_DELIMITER);
57
58                 for (String s : arr) {
59                     if (s.startsWith("\\d") && s.length() == 5) {
60                         try {
61                             fixedOutput.append((char) Integer.parseInt(s.substring(2, 5)));
62                         } catch (NumberFormatException e) {
63                             fixedOutput.append(s);
64                         }
65                     } else {
66                         fixedOutput.append(s);
67                     }
68                 }
69                 return fixedOutput.toString();
70             }
71         }
72         return input;
73     }
74 }