2 * Copyright (c) 2010-2020 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;
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19 import java.util.stream.Collectors;
21 import org.apache.commons.lang.StringEscapeUtils;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
27 * @author Mark Herwege - Initial contribution
28 * @author Karel Goderis - Based on UPnP logic in Sonos binding
31 public class UpnpEntry {
33 private static final String DIRECTORY_ROOT = "0";
35 private static final Pattern CONTAINER_PATTERN = Pattern.compile("object.container");
39 private String parentId;
40 private String upnpClass;
41 private String title = "";
42 private List<UpnpEntryRes> resList = new ArrayList<>();
43 private String album = "";
44 private String albumArtUri = "";
45 private String creator = "";
46 private String artist = "";
47 private String publisher = "";
48 private String genre = "";
49 private @Nullable Integer originalTrackNumber;
51 private boolean isContainer;
53 public UpnpEntry(String id, String refId, String parentId, String upnpClass) {
56 this.parentId = parentId;
57 this.upnpClass = upnpClass;
59 Matcher matcher = CONTAINER_PATTERN.matcher(upnpClass);
60 isContainer = matcher.find();
63 public UpnpEntry withTitle(String title) {
68 public UpnpEntry withAlbum(String album) {
73 public UpnpEntry withAlbumArtUri(String albumArtUri) {
74 this.albumArtUri = albumArtUri;
78 public UpnpEntry withCreator(String creator) {
79 this.creator = creator;
83 public UpnpEntry withArtist(String artist) {
88 public UpnpEntry withPublisher(String publisher) {
89 this.publisher = publisher;
93 public UpnpEntry withGenre(String genre) {
98 public UpnpEntry withResList(List<UpnpEntryRes> resList) {
99 this.resList = resList;
103 public UpnpEntry withTrackNumber(@Nullable Integer originalTrackNumber) {
104 this.originalTrackNumber = originalTrackNumber;
109 * @return the title of the entry.
112 public String toString() {
117 * @return the unique identifier of this entry.
119 public String getId() {
124 * @return the title of the entry.
126 public String getTitle() {
131 * @return the identifier of the entry this reference intry refers to.
133 public String getRefId() {
138 * @return the unique identifier of the parent of this entry.
140 public String getParentId() {
141 return parentId.isEmpty() ? DIRECTORY_ROOT : parentId;
145 * @return a URI for this entry. Thumbnail resources are not considered.
147 public String getRes() {
148 return resList.stream().filter(res -> !res.isThumbnailRes()).map(UpnpEntryRes::getRes).findAny().orElse("");
151 public List<String> getProtocolList() {
152 return resList.stream().map(UpnpEntryRes::getProtocolInfo).collect(Collectors.toList());
156 * @return the UPnP classname for this entry.
158 public String getUpnpClass() {
162 public boolean isContainer() {
167 * @return the name of the album.
169 public String getAlbum() {
174 * @return the URI for the album art.
176 public String getAlbumArtUri() {
177 return StringEscapeUtils.unescapeXml(albumArtUri);
181 * @return the name of the artist who created the entry.
183 public String getCreator() {
187 public String getArtist() {
191 public String getPublisher() {
195 public String getGenre() {
199 public @Nullable Integer getOriginalTrackNumber() {
200 return originalTrackNumber;