]> git.basschouten.com Git - openhab-addons.git/blob
ebc4f987173fc2a3860db6eca9a1d7a489d3e032
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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 org.apache.commons.lang.StringEscapeUtils;
16 import org.eclipse.jdt.annotation.NonNullByDefault;
17
18 /**
19  * The {@link KaleidescapeFormatter} is a utility class with formatting methods for Kaleidescape strings
20  *
21  * @author Michael Lobstein - Initial contribution
22  */
23 @NonNullByDefault
24 public class KaleidescapeFormatter {
25     public static String formatString(String input) {
26         if (!input.equals("")) {
27             // convert || back to :
28             input = input.replace("||", ":");
29
30             // if input does not have any escaped characters, bypass all the replace()'s
31             if (input.contains("\\")) {
32                 // fix escaped :
33                 input = input.replace("\\:", ":");
34
35                 // fix escaped /
36                 input = input.replace("\\/", "/");
37
38                 // convert \r into comma space
39                 input = input.replace("\\r", ", ");
40
41                 // convert \d146 from review text into apostrophe
42                 input = input.replace("\\d146", "'");
43                 // convert \d147 & \d148 from review text into double quote
44                 input = input.replace("\\d147", "\"");
45                 input = input.replace("\\d148", "\"");
46
47                 // fix the encoding for k mangled extended ascii characters (chars coming in as \dnnn)
48                 // I.e. characters with accent, umlaut, etc., they need to be restored to the correct character
49                 // example: Noel (with umlaut 'o') comes in as N\d246el
50                 input = input.replaceAll("(?i)\\\\d([0-9]{3})", "\\&#$1;"); // first convert to html escaped codes
51                 // then convert with unescapeHtml, not sure how to do this without the Apache libraries :(
52                 return StringEscapeUtils.unescapeHtml(input);
53             }
54         }
55         return input;
56     }
57 }