2 * Copyright (c) 2015. 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.Date;
21 import java.util.GregorianCalendar;
22 import java.util.List;
25 import javax.xml.bind.JAXBElement;
26 import javax.xml.datatype.DatatypeConfigurationException;
27 import javax.xml.datatype.DatatypeFactory;
28 import javax.xml.datatype.XMLGregorianCalendar;
29 import javax.xml.namespace.QName;
31 import org.hedgecode.xml.xspf.Attribution;
32 import org.hedgecode.xml.xspf.Extension;
33 import org.hedgecode.xml.xspf.Link;
34 import org.hedgecode.xml.xspf.Meta;
35 import org.hedgecode.xml.xspf.PlaylistFactory;
36 import org.hedgecode.xml.xspf.Track;
37 import org.hedgecode.xml.xspf.xsd.ExtensionType;
38 import org.hedgecode.xml.xspf.xsd.LinkType;
39 import org.hedgecode.xml.xspf.xsd.MetaType;
40 import org.hedgecode.xml.xspf.xsd.PlaylistType;
41 import org.hedgecode.xml.xspf.xsd.TrackType;
46 * @author Dmitry Samoshin aka gotty
48 public class XSDPlaylistBinder extends AbstractXSDBinder implements PlaylistBinder {
50 private static final String REF_LOCATION_NAME = "location";
51 private static final String REF_IDENTIFIER_NAME = "identifier";
53 private PlaylistType xsdPlaylist;
55 public XSDPlaylistBinder() {
56 this.xsdPlaylist = XSD_FACTORY.createPlaylistType();
59 public XSDPlaylistBinder(PlaylistType xsdPlaylist) {
60 this.xsdPlaylist = xsdPlaylist;
64 public String getPackageName() {
65 return xsdPlaylist.getClass().getPackage().getName();
69 public Class getPlaylistClass() {
70 return xsdPlaylist.getClass();
74 public Object getPlaylist() {
79 public void setPlaylist(Object playlist) {
80 if (playlist instanceof PlaylistType)
81 this.xsdPlaylist = (PlaylistType) playlist;
85 public JAXBElement wrapPlaylist() {
86 return XSD_FACTORY.createPlaylist(xsdPlaylist);
90 public String getTitle() {
91 return xsdPlaylist.getTitle();
95 public void setTitle(String title) {
96 xsdPlaylist.setTitle(title);
100 public String getCreator() {
101 return xsdPlaylist.getCreator();
105 public void setCreator(String creator) {
106 xsdPlaylist.setCreator(creator);
110 public String getAnnotation() {
111 return xsdPlaylist.getAnnotation();
115 public void setAnnotation(String annotation) {
116 xsdPlaylist.setAnnotation(annotation);
120 public String getInfo() {
121 return xsdPlaylist.getInfo();
125 public void setInfo(String info) {
126 xsdPlaylist.setInfo(info);
130 public String getLocation() {
131 return xsdPlaylist.getLocation();
135 public void setLocation(String location) {
136 xsdPlaylist.setLocation(location);
140 public String getIdentifier() {
141 return xsdPlaylist.getIdentifier();
145 public void setIdentifier(String identifier) {
146 xsdPlaylist.setIdentifier(identifier);
150 public String getImage() {
151 return xsdPlaylist.getImage();
155 public void setImage(String image) {
156 xsdPlaylist.setImage(image);
160 public Date getDate() {
161 XMLGregorianCalendar calendar = xsdPlaylist.getDate();
162 if (calendar != null)
163 return calendar.toGregorianCalendar().getTime();
168 public void setDate(Date date) {
169 XMLGregorianCalendar xmlCalendar = null;
171 GregorianCalendar gCalendar = new GregorianCalendar();
172 gCalendar.setTime(date);
174 xmlCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gCalendar);
175 } catch (DatatypeConfigurationException ignored) {}
177 xsdPlaylist.setDate(xmlCalendar);
181 public String getLicense() {
182 return xsdPlaylist.getLicense();
186 public void setLicense(String license) {
187 xsdPlaylist.setLicense(license);
191 public Attribution getAttribution() {
192 if (xsdPlaylist.getAttribution() != null) {
193 List<JAXBElement<String>> elements = xsdPlaylist.getAttribution().getIdentifierOrLocation();
194 Attribution attribution = PlaylistFactory.createAttribution();
195 String identifier = null, location = null;
196 for (JAXBElement<String> element : elements) {
198 if (REF_IDENTIFIER_NAME.equals(element.getName().getLocalPart()))
199 identifier = element.getValue();
200 else if (REF_LOCATION_NAME.equals(element.getName().getLocalPart()))
201 location = element.getValue();
203 if (identifier != null && location != null) {
204 attribution.add(identifier, location);
215 public void setAttribution(Attribution attribution) {
216 if (attribution != null) {
217 if (xsdPlaylist.getAttribution() == null) {
218 xsdPlaylist.setAttribution(
219 XSD_FACTORY.createAttributionType()
222 for (Map.Entry<String, String> element : attribution.get().entrySet()) {
223 xsdPlaylist.getAttribution().getIdentifierOrLocation().add(
224 XSD_FACTORY.createAttributionTypeIdentifier(element.getKey())
226 xsdPlaylist.getAttribution().getIdentifierOrLocation().add(
227 XSD_FACTORY.createAttributionTypeLocation(element.getValue())
234 public List<Link> getLinks() {
235 List<Link> links = new ArrayList<>();
236 for (LinkType link : xsdPlaylist.getLink()) {
238 PlaylistFactory.createLink(link.getRel(), link.getValue())
245 public void addLink(Link link) {
247 LinkType linkType = XSD_FACTORY.createLinkType();
248 linkType.setRel(link.getRel());
249 linkType.setValue(link.getContent());
250 xsdPlaylist.getLink().add(
257 public List<Meta> getMetas() {
258 List<Meta> metas = new ArrayList<>();
259 for (MetaType meta : xsdPlaylist.getMeta()) {
261 PlaylistFactory.createMeta(meta.getRel(), meta.getValue())
268 public void addMeta(Meta meta) {
270 MetaType metaType = XSD_FACTORY.createMetaType();
271 metaType.setRel(meta.getRel());
272 metaType.setValue(meta.getContent());
273 xsdPlaylist.getMeta().add(
280 public List<Extension> getExtensions() {
281 List<Extension> extensions = new ArrayList<>();
282 for (ExtensionType extension : xsdPlaylist.getExtension()) {
284 PlaylistFactory.createExtension(
285 extension.getApplication(), extension.getContent()
293 public void addExtension(Extension extension) {
294 if (extension != null) {
295 ExtensionType extensionType = XSD_FACTORY.createExtensionType();
296 extensionType.setApplication(extension.getApplication());
297 extensionType.getContent().addAll(extension.getContent());
298 xsdPlaylist.getExtension().add(
305 public List<Track> getTracks() {
306 List<Track> tracks = new ArrayList<>();
307 if (xsdPlaylist.getTrackList() != null) {
308 for (TrackType track : xsdPlaylist.getTrackList().getTrack()) {
310 PlaylistFactory.createTrack(
311 BinderFactory.createTrackBinder(track)
320 public void addTrack(Track track) {
322 if (xsdPlaylist.getTrackList() == null) {
323 xsdPlaylist.setTrackList(
324 XSD_FACTORY.createTrackListType()
327 TrackType trackType = XSD_FACTORY.createTrackType();
329 PlaylistFactory.bindTrack(
331 BinderFactory.createTrackBinder(trackType)
334 xsdPlaylist.getTrackList().getTrack().add(
341 public String getVersion() {
342 return xsdPlaylist.getVersion();
346 public void setVersion(String version) {
347 xsdPlaylist.setVersion(version);