2 * Copyright (c) 2015-2019. Developed by Hedgecode.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.hedgecode.xml.xspf.bind;
19 import java.util.ArrayList;
20 import java.util.GregorianCalendar;
21 import java.util.List;
24 import javax.xml.bind.JAXBElement;
25 import javax.xml.datatype.DatatypeConfigurationException;
26 import javax.xml.datatype.DatatypeFactory;
27 import javax.xml.datatype.XMLGregorianCalendar;
28 import javax.xml.namespace.QName;
30 import org.hedgecode.xml.xspf.PlaylistFactory;
31 import org.hedgecode.xml.xspf.XSPFConstants;
32 import org.hedgecode.xml.xspf.rng.Attribution;
33 import org.hedgecode.xml.xspf.rng.Date;
34 import org.hedgecode.xml.xspf.rng.Identifier;
35 import org.hedgecode.xml.xspf.rng.License;
36 import org.hedgecode.xml.xspf.rng.Location;
37 import org.hedgecode.xml.xspf.rng.Playlist;
38 import org.hedgecode.xml.xspf.rng.Track;
39 import org.hedgecode.xml.xspf.rng.TrackList;
44 * @author Dmitry Samoshin aka gotty
46 public class RNGPlaylistBinder extends AbstractRNGBinder implements PlaylistBinder {
48 private Playlist rngPlaylist;
50 public RNGPlaylistBinder() {
51 this.rngPlaylist = RNG_FACTORY.createPlaylist();
54 public RNGPlaylistBinder(Playlist rngPlaylist) {
55 this.rngPlaylist = rngPlaylist;
59 protected Object getElement(Class elementClass) {
60 for (Object element : rngPlaylist.getTitleOrCreatorOrAnnotation()) {
61 if (element.getClass().equals(elementClass))
68 protected List<Object> getElements(Class elementClass) {
69 List<Object> elements = new ArrayList<>();
70 for (Object element : rngPlaylist.getTitleOrCreatorOrAnnotation()) {
71 if (element.getClass().equals(elementClass))
72 elements.add(element);
78 protected List<Object> getAllElements() {
79 return rngPlaylist.getTitleOrCreatorOrAnnotation();
83 public String getPackageName() {
84 return rngPlaylist.getClass().getPackage().getName();
88 public Class getPlaylistClass() {
89 return rngPlaylist.getClass();
93 public Object getPlaylist() {
98 public void setPlaylist(Object playlist) {
99 if (playlist instanceof Playlist)
100 this.rngPlaylist = (Playlist) playlist;
104 public JAXBElement wrapPlaylist() {
105 return new JAXBElement<>(
106 XSPFConstants.PLAYLIST_QNAME,
113 public String getLocation() {
114 Location locationElement = (Location) getElement(Location.class);
115 return locationElement != null ? locationElement.getValue() : null;
119 public void setLocation(String location) {
120 Location locationElement = (Location) getElement(Location.class);
121 if (locationElement != null)
122 getAllElements().remove(locationElement);
123 if (location != null) {
124 locationElement = RNG_FACTORY.createLocation();
125 locationElement.setValue(location);
126 getAllElements().add(locationElement);
131 public String getIdentifier() {
132 Identifier identifierElement = (Identifier) getElement(Identifier.class);
133 return identifierElement != null ? identifierElement.getValue() : null;
137 public void setIdentifier(String identifier) {
138 Identifier identifierElement = (Identifier) getElement(Identifier.class);
139 if (identifierElement != null)
140 getAllElements().remove(identifierElement);
141 if (identifier != null) {
142 identifierElement = RNG_FACTORY.createIdentifier();
143 identifierElement.setValue(identifier);
144 getAllElements().add(identifierElement);
149 public java.util.Date getDate() {
150 Date dateElement = (Date) getElement(Date.class);
151 return dateElement != null && dateElement.getValue() != null
152 ? dateElement.getValue().toGregorianCalendar().getTime()
157 public void setDate(java.util.Date date) {
158 Date dateElement = (Date) getElement(Date.class);
159 if (dateElement != null)
160 getAllElements().remove(dateElement);
162 dateElement = RNG_FACTORY.createDate();
163 GregorianCalendar gCalendar = new GregorianCalendar();
164 gCalendar.setTime(date);
165 XMLGregorianCalendar xmlCalendar = null;
167 xmlCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gCalendar);
168 } catch (DatatypeConfigurationException ignored) {}
169 dateElement.setValue(xmlCalendar);
170 getAllElements().add(dateElement);
175 public String getLicense() {
176 License licenseElement = (License) getElement(License.class);
177 return licenseElement != null ? licenseElement.getValue() : null;
181 public void setLicense(String license) {
182 License licenseElement = (License) getElement(License.class);
183 if (licenseElement != null)
184 getAllElements().remove(licenseElement);
185 if (license != null) {
186 licenseElement = RNG_FACTORY.createLicense();
187 licenseElement.setValue(license);
188 getAllElements().add(licenseElement);
193 public org.hedgecode.xml.xspf.Attribution getAttribution() {
194 Attribution attributionElement = (Attribution) getElement(Attribution.class);
195 if (attributionElement != null) {
196 org.hedgecode.xml.xspf.Attribution attribution = PlaylistFactory.createAttribution();
197 String identifier = null, location = null;
198 for (Object element : attributionElement.getIdentifierOrLocation()) {
200 if (element.getClass().equals(Identifier.class))
201 identifier = ((Identifier) element).getValue();
202 else if (element.getClass().equals(Location.class))
203 location = ((Location) element).getValue();
205 if (identifier != null && location != null) {
206 attribution.add(identifier, location);
217 public void setAttribution(org.hedgecode.xml.xspf.Attribution attribution) {
218 Attribution attributionElement = (Attribution) getElement(Attribution.class);
219 if (attributionElement != null)
220 getAllElements().remove(attributionElement);
221 if (attribution != null) {
222 attributionElement = RNG_FACTORY.createAttribution();
223 for (Map.Entry<String, String> element : attribution.get().entrySet()) {
224 Location location = RNG_FACTORY.createLocation();
225 location.setValue(element.getKey());
226 Identifier identifier = RNG_FACTORY.createIdentifier();
227 identifier.setValue(element.getValue());
228 attributionElement.getIdentifierOrLocation().add(location);
229 attributionElement.getIdentifierOrLocation().add(identifier);
231 getAllElements().add(attributionElement);
236 public List<org.hedgecode.xml.xspf.Track> getTracks() {
237 List<org.hedgecode.xml.xspf.Track> tracks = new ArrayList<>();
238 TrackList traclListElement = (TrackList) getElement(TrackList.class);
239 if (traclListElement != null) {
240 for (Track track : traclListElement.getTrack()) {
242 PlaylistFactory.createTrack(
243 BinderFactory.createTrackBinder(track)
252 public void addTrack(org.hedgecode.xml.xspf.Track track) {
254 TrackList trackListElement = (TrackList) getElement(TrackList.class);
255 if (trackListElement == null) {
256 trackListElement = RNG_FACTORY.createTrackList();
257 getAllElements().add(trackListElement);
260 Track trackElement = RNG_FACTORY.createTrack();
261 PlaylistFactory.bindTrack(
263 BinderFactory.createTrackBinder(trackElement)
266 trackListElement.getTrack().add(
273 public String getVersion() {
274 return rngPlaylist.getVersion();
278 public void setVersion(String version) {
279 rngPlaylist.setVersion(version);