]> git.basschouten.com Git - openhab-addons.git/blob
26eaa4d2a49bd1073ef95e4d622e26d085546e4c
[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.sonos.internal.util;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16
17 /**
18  * The {@link StringUtils} class defines some static string utility methods
19  *
20  * @author Leo Siepel - Initial contribution
21  */
22 @NonNullByDefault
23 public class StringUtils {
24
25     /**
26      * Simple method to escape XML special characters in String.
27      * There are five XML Special characters which needs to be escaped:
28      *
29      * <pre>
30      * {@code
31      * & - &amp;
32      * < - &lt;
33      * > - &gt;
34      * " - &quot;
35      * ' - &apos;
36      * }
37      * </pre>
38      */
39     public static String escapeXml(String xml) {
40         xml = xml.replace("&", "&amp;");
41         xml = xml.replace("<", "&lt;");
42         xml = xml.replace(">", "&gt;");
43         xml = xml.replace("\"", "&quot;");
44         xml = xml.replace("'", "&apos;");
45         return xml;
46     }
47
48     /**
49      * Simple method to un escape XML special characters in String.
50      * There are five XML Special characters which needs to be escaped :
51      *
52      * <pre>
53      * {@code
54      * & - &amp;
55      * < - &lt;
56      * > - &gt;
57      * " - &quot;
58      * ' - &apos;
59      * }
60      * </pre>
61      */
62     public static String unEscapeXml(String xml) {
63         xml = xml.replace("&amp;", "&");
64         xml = xml.replace("&lt;", "<");
65         xml = xml.replace("&gt;", ">");
66         xml = xml.replace("&quot;", "\"");
67         xml = xml.replace("&apos;", "'");
68         return xml;
69     }
70 }