2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.samsungtv.internal;
15 import java.io.IOException;
16 import java.io.StringReader;
17 import java.util.Base64;
18 import java.util.Optional;
20 import javax.xml.parsers.DocumentBuilderFactory;
21 import javax.xml.parsers.ParserConfigurationException;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.jupnp.model.meta.RemoteDevice;
26 import org.openhab.core.types.Command;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.w3c.dom.Document;
30 import org.xml.sax.InputSource;
31 import org.xml.sax.SAXException;
34 * The {@link Utils} is a collection of static utilities
36 * @author Nick Waterton - Initial contribution
40 private static final Logger LOGGER = LoggerFactory.getLogger(Utils.class);
41 public static DocumentBuilderFactory factory = getDocumentBuilder();
43 private static DocumentBuilderFactory getDocumentBuilder() {
44 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
46 // see https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
47 factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
48 factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
49 factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
50 factory.setXIncludeAware(false);
51 factory.setExpandEntityReferences(false);
52 } catch (ParserConfigurationException e) {
53 LOGGER.debug("XMLParser Configuration Error: {}", e.getMessage());
55 return Optional.ofNullable(factory).orElse(DocumentBuilderFactory.newInstance());
59 * Build {@link Document} from {@link String} which contains XML content.
62 * {@link String} which contains XML content.
63 * @return {@link Optional Document} or empty if convert has failed.
65 public static Optional<Document> loadXMLFromString(String xml, String host) {
67 return Optional.ofNullable(factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml))));
68 } catch (ParserConfigurationException | SAXException | IOException e) {
69 LOGGER.debug("{}: Error loading XML: {}", host, e.getMessage());
71 return Optional.empty();
74 public static boolean isSoundChannel(String name) {
75 return (name.contains("Volume") || name.contains("Mute"));
78 public static String b64encode(String str) {
79 return Base64.getUrlEncoder().encodeToString(str.getBytes());
82 public static String truncCmd(Command command) {
83 String cmd = command.toString();
84 return (cmd.length() <= 80) ? cmd : cmd.substring(0, 80) + "...";
87 public static String getModelName(@Nullable RemoteDevice device) {
88 return Optional.ofNullable(device).map(a -> a.getDetails()).map(a -> a.getModelDetails())
89 .map(a -> a.getModelName()).orElse("");
92 public static String getManufacturer(@Nullable RemoteDevice device) {
93 return Optional.ofNullable(device).map(a -> a.getDetails()).map(a -> a.getManufacturerDetails())
94 .map(a -> a.getManufacturer()).orElse("");
97 public static String getFriendlyName(@Nullable RemoteDevice device) {
98 return Optional.ofNullable(device).map(a -> a.getDetails()).map(a -> a.getFriendlyName()).orElse("");
101 public static String getUdn(@Nullable RemoteDevice device) {
102 return Optional.ofNullable(device).map(a -> a.getIdentity()).map(a -> a.getUdn())
103 .map(a -> a.getIdentifierString()).orElse("");
106 public static String getHost(@Nullable RemoteDevice device) {
107 return Optional.ofNullable(device).map(a -> a.getIdentity()).map(a -> a.getDescriptorURL())
108 .map(a -> a.getHost()).orElse("");
111 public static String getType(@Nullable RemoteDevice device) {
112 return Optional.ofNullable(device).map(a -> a.getType()).map(a -> a.getType()).orElse("");