2 * Copyright (c) 2010-2023 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.bosesoundtouch.internal;
15 import java.util.HashMap;
17 import java.util.Objects;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.core.types.StateOption;
23 import com.google.gson.annotations.Expose;
26 * The {@link ContentItem} class manages a ContentItem
28 * @author Christian Niessner - Initial contribution
29 * @author Thomas Traunbauer - Initial contribution
32 public class ContentItem {
34 private String source = "";
35 private @Nullable String sourceAccount;
36 private @Nullable String location;
37 private boolean presetable = false;
38 private @Nullable String itemName;
39 private int presetID = 0;
40 private @Nullable String containerArt;
42 private final Map<String, String> additionalAttributes = new HashMap<>();
45 * Returns true if this ContentItem is defined as Preset
47 * @return true if this ContentItem is defined as Preset
49 public boolean isPreset() {
58 * Returns true if all necessary stats are set
60 * @return true if all necessary stats are set
62 public boolean isValid() {
63 if (getOperationMode() == OperationModeType.STANDBY) {
66 String localItemName = itemName;
67 if (localItemName != null) {
68 return !(localItemName.isEmpty() || source.isEmpty());
76 * Returns true if source, sourceAccount, location, itemName, and presetable are equal
78 * @return true if source, sourceAccount, location, itemName, and presetable are equal
81 public boolean equals(@Nullable Object obj) {
82 if (obj instanceof ContentItem) {
83 ContentItem other = (ContentItem) obj;
84 return Objects.equals(other.source, this.source) || Objects.equals(other.sourceAccount, this.sourceAccount)
85 || other.presetable == this.presetable || Objects.equals(other.location, this.location)
86 || Objects.equals(other.itemName, this.itemName);
88 return super.equals(obj);
92 * Returns the operation Mode, depending on the stats that are set
94 * @return the operation Mode, depending on the stats that are set
96 public OperationModeType getOperationMode() {
97 OperationModeType operationMode = OperationModeType.OTHER;
98 if ("".equals(source)) {
99 return OperationModeType.OTHER;
101 if (source.contains("PRODUCT")) {
102 String localSourceAccount = sourceAccount;
103 if (localSourceAccount != null) {
104 if (localSourceAccount.contains("TV")) {
105 operationMode = OperationModeType.TV;
107 if (localSourceAccount.contains("HDMI")) {
108 operationMode = OperationModeType.HDMI1;
111 return operationMode;
114 operationMode = OperationModeType.valueOf(source);
115 return operationMode;
116 } catch (IllegalArgumentException iae) {
117 return OperationModeType.OTHER;
121 public void setSource(String source) {
122 this.source = source;
125 public void setSourceAccount(String sourceAccount) {
126 this.sourceAccount = sourceAccount;
129 public void setLocation(String location) {
130 this.location = location;
133 public void setItemName(String itemName) {
134 this.itemName = itemName;
137 public void setAdditionalAttribute(String name, String value) {
138 this.additionalAttributes.put(name, value);
141 public void setPresetable(boolean presetable) {
142 this.presetable = presetable;
145 public void setPresetID(int presetID) {
146 this.presetID = presetID;
149 public void setContainerArt(String containerArt) {
150 this.containerArt = containerArt;
153 public String getSource() {
157 public @Nullable String getSourceAccount() {
158 return sourceAccount;
161 public @Nullable String getLocation() {
165 public @Nullable String getItemName() {
169 public boolean isPresetable() {
173 public int getPresetID() {
177 public @Nullable String getContainerArt() {
182 * Simple method to escape XML special characters in String.
183 * There are five XML Special characters which needs to be escaped :
190 private String escapeXml(String xml) {
191 xml = xml.replaceAll("&", "&");
192 xml = xml.replaceAll("<", "<");
193 xml = xml.replaceAll(">", ">");
194 xml = xml.replaceAll("\"", """);
195 xml = xml.replaceAll("'", "'");
200 * Returns the XML Code that is needed to switch to this ContentItem
202 * @return the XML Code that is needed to switch to this ContentItem
204 public String generateXML() {
206 switch (getOperationMode()) {
208 xml = "<ContentItem source=\"BLUETOOTH\"></ContentItem>";
214 xml = "<ContentItem source=\"AUX\" sourceAccount=\"" + sourceAccount + "\"></ContentItem>";
217 xml = "<ContentItem source=\"PRODUCT\" sourceAccount=\"TV\" isPresetable=\"false\" />";
220 xml = "<ContentItem source=\"PRODUCT\" sourceAccount=\"HDMI_1\" isPresetable=\"false\" />";
223 StringBuilder sbXml = new StringBuilder("<ContentItem");
225 sbXml.append(" source=\"").append(escapeXml(source)).append("\"");
227 String localLocation = location;
228 if (localLocation != null) {
229 sbXml.append(" location=\"").append(escapeXml(localLocation)).append("\"");
231 String localSourceAccount = sourceAccount;
232 if (localSourceAccount != null) {
233 sbXml.append(" sourceAccount=\"").append(escapeXml(localSourceAccount)).append("\"");
235 sbXml.append(" isPresetable=\"").append(presetable).append("\"");
236 for (Map.Entry<String, String> aae : additionalAttributes.entrySet()) {
237 sbXml.append(" ").append(aae.getKey()).append("=\"").append(escapeXml(aae.getValue())).append("\"");
240 if (itemName != null) {
241 sbXml.append("<itemName>").append(itemName).append("</itemName>");
243 if (containerArt != null) {
244 sbXml.append("<containerArt>").append(containerArt).append("</containerArt>");
246 sbXml.append("</ContentItem>");
247 xml = sbXml.toString();
253 public StateOption toStateOption() {
254 String stateOptionLabel = String.valueOf(presetID) + ": " + itemName;
255 return new StateOption(String.valueOf(presetID), stateOptionLabel);
259 public String toString() {
260 // if (presetID >= 1 && presetID <= 6) {
261 // StringBuilder buffer = new StringBuilder();
262 // buffer.append("PRESET_");
263 // buffer.append(presetID);
264 // return buffer.toString();
266 String localString = itemName;
267 return (localString != null) ? localString : "";