]> git.basschouten.com Git - openhab-addons.git/blob
a33102b1f893dbdcc9c6888268c14c00556bfc74
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.upnpcontrol.internal;
14
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;
20
21 import org.apache.commons.lang.StringEscapeUtils;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24
25 /**
26  *
27  * @author Mark Herwege - Initial contribution
28  * @author Karel Goderis - Based on UPnP logic in Sonos binding
29  */
30 @NonNullByDefault
31 public class UpnpEntry {
32
33     private static final String DIRECTORY_ROOT = "0";
34
35     private static final Pattern CONTAINER_PATTERN = Pattern.compile("object.container");
36
37     private String id;
38     private String refId;
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;
50
51     private boolean isContainer;
52
53     public UpnpEntry(String id, String refId, String parentId, String upnpClass) {
54         this.id = id;
55         this.refId = refId;
56         this.parentId = parentId;
57         this.upnpClass = upnpClass;
58
59         Matcher matcher = CONTAINER_PATTERN.matcher(upnpClass);
60         isContainer = matcher.find();
61     }
62
63     public UpnpEntry withTitle(String title) {
64         this.title = title;
65         return this;
66     }
67
68     public UpnpEntry withAlbum(String album) {
69         this.album = album;
70         return this;
71     }
72
73     public UpnpEntry withAlbumArtUri(String albumArtUri) {
74         this.albumArtUri = albumArtUri;
75         return this;
76     }
77
78     public UpnpEntry withCreator(String creator) {
79         this.creator = creator;
80         return this;
81     }
82
83     public UpnpEntry withArtist(String artist) {
84         this.artist = artist;
85         return this;
86     }
87
88     public UpnpEntry withPublisher(String publisher) {
89         this.publisher = publisher;
90         return this;
91     }
92
93     public UpnpEntry withGenre(String genre) {
94         this.genre = genre;
95         return this;
96     }
97
98     public UpnpEntry withResList(List<UpnpEntryRes> resList) {
99         this.resList = resList;
100         return this;
101     }
102
103     public UpnpEntry withTrackNumber(@Nullable Integer originalTrackNumber) {
104         this.originalTrackNumber = originalTrackNumber;
105         return this;
106     }
107
108     /**
109      * @return the title of the entry.
110      */
111     @Override
112     public String toString() {
113         return title;
114     }
115
116     /**
117      * @return the unique identifier of this entry.
118      */
119     public String getId() {
120         return id;
121     }
122
123     /**
124      * @return the title of the entry.
125      */
126     public String getTitle() {
127         return title;
128     }
129
130     /**
131      * @return the identifier of the entry this reference intry refers to.
132      */
133     public String getRefId() {
134         return refId;
135     }
136
137     /**
138      * @return the unique identifier of the parent of this entry.
139      */
140     public String getParentId() {
141         return parentId.isEmpty() ? DIRECTORY_ROOT : parentId;
142     }
143
144     /**
145      * @return a URI for this entry. Thumbnail resources are not considered.
146      */
147     public String getRes() {
148         return resList.stream().filter(res -> !res.isThumbnailRes()).map(UpnpEntryRes::getRes).findAny().orElse("");
149     }
150
151     public List<String> getProtocolList() {
152         return resList.stream().map(UpnpEntryRes::getProtocolInfo).collect(Collectors.toList());
153     }
154
155     /**
156      * @return the UPnP classname for this entry.
157      */
158     public String getUpnpClass() {
159         return upnpClass;
160     }
161
162     public boolean isContainer() {
163         return isContainer;
164     }
165
166     /**
167      * @return the name of the album.
168      */
169     public String getAlbum() {
170         return album;
171     }
172
173     /**
174      * @return the URI for the album art.
175      */
176     public String getAlbumArtUri() {
177         return StringEscapeUtils.unescapeXml(albumArtUri);
178     }
179
180     /**
181      * @return the name of the artist who created the entry.
182      */
183     public String getCreator() {
184         return creator;
185     }
186
187     public String getArtist() {
188         return artist;
189     }
190
191     public String getPublisher() {
192         return publisher;
193     }
194
195     public String getGenre() {
196         return genre;
197     }
198
199     public @Nullable Integer getOriginalTrackNumber() {
200         return originalTrackNumber;
201     }
202 }