]> git.basschouten.com Git - openhab-addons.git/blob
46908825224283ec57ca7f67d85a66cefab18ece
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.samsungtv.internal.service;
14
15 import java.io.IOException;
16 import java.io.StringReader;
17 import java.util.HashMap;
18
19 import javax.xml.parsers.DocumentBuilder;
20 import javax.xml.parsers.DocumentBuilderFactory;
21 import javax.xml.parsers.ParserConfigurationException;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.w3c.dom.Document;
26 import org.xml.sax.InputSource;
27 import org.xml.sax.SAXException;
28
29 /**
30  * The {@link SamsungTvUtils} provides some utilities for internal use.
31  *
32  * @author Pauli Anttila - Initial contribution
33  */
34 @NonNullByDefault
35 public class SamsungTvUtils {
36
37     /**
38      * Build {@link String} type {@link HashMap} from variable number of
39      * {@link String}s.
40      *
41      * @param data
42      *            Variable number of {@link String} parameters which will be
43      *            added to hash map.
44      */
45     public static HashMap<String, String> buildHashMap(String... data) {
46         HashMap<String, String> result = new HashMap<>();
47
48         if (data.length % 2 != 0) {
49             throw new IllegalArgumentException("Odd number of arguments");
50         }
51         String key = null;
52         Integer step = -1;
53
54         for (String value : data) {
55             step++;
56             switch (step % 2) {
57                 case 0:
58                     if (value == null) {
59                         throw new IllegalArgumentException("Null key value");
60                     }
61                     key = value;
62                     continue;
63                 case 1:
64                     if (key != null) {
65                         result.put(key, value);
66                     }
67                     break;
68             }
69         }
70
71         return result;
72     }
73
74     /**
75      * Build {@link Document} from {@link String} which contains XML content.
76      *
77      * @param xml
78      *            {@link String} which contains XML content.
79      * @return {@link Document} or null if convert has failed.
80      */
81     public static @Nullable Document loadXMLFromString(String xml) {
82         try {
83             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
84             // see https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
85             factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
86             factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
87             factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
88             factory.setXIncludeAware(false);
89             factory.setExpandEntityReferences(false);
90             DocumentBuilder builder = factory.newDocumentBuilder();
91             InputSource is = new InputSource(new StringReader(xml));
92             return builder.parse(is);
93
94         } catch (ParserConfigurationException | SAXException | IOException e) {
95             // Silently ignore exception and return null.
96         }
97
98         return null;
99     }
100 }