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.upnpcontrol.internal.queue;
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Objects;
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
20 import java.util.stream.Collectors;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.core.util.StringUtils;
28 * @author Mark Herwege - Initial contribution
29 * @author Karel Goderis - Based on UPnP logic in Sonos binding
32 public class UpnpEntry {
34 private static final String DIRECTORY_ROOT = "0";
36 private static final Pattern CONTAINER_PATTERN = Pattern.compile("object.container");
40 private String parentId;
41 private String upnpClass;
42 private String title = "";
43 private List<UpnpEntryRes> resList = new ArrayList<>();
44 private String album = "";
45 private String albumArtUri = "";
46 private String creator = "";
47 private String artist = "";
48 private String publisher = "";
49 private String genre = "";
50 private @Nullable Integer originalTrackNumber;
52 private boolean isContainer;
58 public UpnpEntry(String id, String refId, String parentId, String upnpClass) {
61 this.parentId = parentId;
62 this.upnpClass = upnpClass;
64 Matcher matcher = CONTAINER_PATTERN.matcher(upnpClass);
65 isContainer = matcher.find();
68 public UpnpEntry withTitle(String title) {
73 public UpnpEntry withAlbum(String album) {
78 public UpnpEntry withAlbumArtUri(String albumArtUri) {
79 this.albumArtUri = albumArtUri;
83 public UpnpEntry withCreator(String creator) {
84 this.creator = creator;
88 public UpnpEntry withArtist(String artist) {
93 public UpnpEntry withPublisher(String publisher) {
94 this.publisher = publisher;
98 public UpnpEntry withGenre(String genre) {
103 public UpnpEntry withResList(List<UpnpEntryRes> resList) {
104 this.resList = resList;
108 public UpnpEntry withTrackNumber(@Nullable Integer originalTrackNumber) {
109 this.originalTrackNumber = originalTrackNumber;
114 * @return the title of the entry.
117 public String toString() {
122 * @return the unique identifier of this entry.
124 public String getId() {
129 * @return the title of the entry.
131 public String getTitle() {
136 * @return the identifier of the entry this reference intry refers to.
138 public String getRefId() {
143 * @return the unique identifier of the parent of this entry.
145 public String getParentId() {
146 return parentId.isEmpty() ? DIRECTORY_ROOT : parentId;
150 * @return a URI for this entry. Thumbnail resources are not considered.
152 public String getRes() {
153 return resList.stream().filter(res -> !res.isThumbnailRes()).map(UpnpEntryRes::getRes).findAny().orElse("");
156 public List<String> getProtocolList() {
157 return resList.stream().map(UpnpEntryRes::getProtocolInfo).collect(Collectors.toList());
161 * @return the UPnP classname for this entry.
163 public String getUpnpClass() {
167 public boolean isContainer() {
172 * @return the name of the album.
174 public String getAlbum() {
179 * @return the URI for the album art.
181 public String getAlbumArtUri() {
182 return Objects.requireNonNull(StringUtils.unEscapeXml(albumArtUri));
186 * @return the name of the artist who created the entry.
188 public String getCreator() {
192 public String getArtist() {
196 public String getPublisher() {
200 public String getGenre() {
204 public @Nullable Integer getOriginalTrackNumber() {
205 return originalTrackNumber;