]> git.basschouten.com Git - openhab-addons.git/blob
285e25fb910aabe4d69072efb91d5c2a5ae419e4
[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.bosesoundtouch.internal.discovery;
14
15 import java.io.IOException;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.io.net.http.HttpUtil;
19
20 /**
21  * The {@link DiscoveryUtil} is a static helper class, to get infos from a XML
22  *
23  * @author Thomas Traunbauer - Initial contribution
24  */
25 @NonNullByDefault
26 public class DiscoveryUtil {
27
28     /**
29      * Finds the content in an element
30      *
31      * This is a quick and dirty method, it always delivers the first appearance of content in an element
32      */
33     public static String getContentOfFirstElement(String content, String element) {
34         String beginTag = "<" + element + ">";
35         String endTag = "</" + element + ">";
36
37         int startIndex = content.indexOf(beginTag) + beginTag.length();
38         int endIndex = content.indexOf(endTag);
39
40         if (startIndex != -1 && endIndex != -1) {
41             String result = content.substring(startIndex, endIndex);
42             return result != null ? result : "";
43         } else {
44             return "";
45         }
46     }
47
48     /**
49      * Executes an URL and returns to answer
50      */
51     public static String executeUrl(String url) throws IOException {
52         return HttpUtil.executeUrl("GET", url, 5000);
53     }
54 }