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 other) {
83 return Objects.equals(other.source, this.source) || Objects.equals(other.sourceAccount, this.sourceAccount)
84 || other.presetable == this.presetable || Objects.equals(other.location, this.location)
85 || Objects.equals(other.itemName, this.itemName);
87 return super.equals(obj);
91 * Returns the operation Mode, depending on the stats that are set
93 * @return the operation Mode, depending on the stats that are set
95 public OperationModeType getOperationMode() {
96 OperationModeType operationMode = OperationModeType.OTHER;
97 if ("".equals(source)) {
98 return OperationModeType.OTHER;
100 if (source.contains("PRODUCT")) {
101 String localSourceAccount = sourceAccount;
102 if (localSourceAccount != null) {
103 if (localSourceAccount.contains("TV")) {
104 operationMode = OperationModeType.TV;
106 if (localSourceAccount.contains("HDMI")) {
107 operationMode = OperationModeType.HDMI1;
110 return operationMode;
113 operationMode = OperationModeType.valueOf(source);
114 return operationMode;
115 } catch (IllegalArgumentException iae) {
116 return OperationModeType.OTHER;
120 public void setSource(String source) {
121 this.source = source;
124 public void setSourceAccount(String sourceAccount) {
125 this.sourceAccount = sourceAccount;
128 public void setLocation(String location) {
129 this.location = location;
132 public void setItemName(String itemName) {
133 this.itemName = itemName;
136 public void setAdditionalAttribute(String name, String value) {
137 this.additionalAttributes.put(name, value);
140 public void setPresetable(boolean presetable) {
141 this.presetable = presetable;
144 public void setPresetID(int presetID) {
145 this.presetID = presetID;
148 public void setContainerArt(String containerArt) {
149 this.containerArt = containerArt;
152 public String getSource() {
156 public @Nullable String getSourceAccount() {
157 return sourceAccount;
160 public @Nullable String getLocation() {
164 public @Nullable String getItemName() {
168 public boolean isPresetable() {
172 public int getPresetID() {
176 public @Nullable String getContainerArt() {
181 * Simple method to escape XML special characters in String.
182 * There are five XML Special characters which needs to be escaped :
189 private String escapeXml(String xml) {
190 xml = xml.replace("&", "&");
191 xml = xml.replace("<", "<");
192 xml = xml.replace(">", ">");
193 xml = xml.replace("\"", """);
194 xml = xml.replace("'", "'");
199 * Returns the XML Code that is needed to switch to this ContentItem
201 * @return the XML Code that is needed to switch to this ContentItem
203 public String generateXML() {
205 switch (getOperationMode()) {
207 xml = "<ContentItem source=\"BLUETOOTH\"></ContentItem>";
213 xml = "<ContentItem source=\"AUX\" sourceAccount=\"" + sourceAccount + "\"></ContentItem>";
216 xml = "<ContentItem source=\"PRODUCT\" sourceAccount=\"TV\" isPresetable=\"false\" />";
219 xml = "<ContentItem source=\"PRODUCT\" sourceAccount=\"HDMI_1\" isPresetable=\"false\" />";
222 StringBuilder sbXml = new StringBuilder("<ContentItem");
224 sbXml.append(" source=\"").append(escapeXml(source)).append("\"");
226 String localLocation = location;
227 if (localLocation != null) {
228 sbXml.append(" location=\"").append(escapeXml(localLocation)).append("\"");
230 String localSourceAccount = sourceAccount;
231 if (localSourceAccount != null) {
232 sbXml.append(" sourceAccount=\"").append(escapeXml(localSourceAccount)).append("\"");
234 sbXml.append(" isPresetable=\"").append(presetable).append("\"");
235 for (Map.Entry<String, String> aae : additionalAttributes.entrySet()) {
236 sbXml.append(" ").append(aae.getKey()).append("=\"").append(escapeXml(aae.getValue())).append("\"");
239 if (itemName != null) {
240 sbXml.append("<itemName>").append(itemName).append("</itemName>");
242 if (containerArt != null) {
243 sbXml.append("<containerArt>").append(containerArt).append("</containerArt>");
245 sbXml.append("</ContentItem>");
246 xml = sbXml.toString();
252 public StateOption toStateOption() {
253 String stateOptionLabel = presetID + ": " + itemName;
254 return new StateOption(String.valueOf(presetID), stateOptionLabel);
258 public String toString() {
259 // if (presetID >= 1 && presetID <= 6) {
260 // StringBuilder buffer = new StringBuilder();
261 // buffer.append("PRESET_");
262 // buffer.append(presetID);
263 // return buffer.toString();
265 String localString = itemName;
266 return (localString != null) ? localString : "";