[LIB-4] Extend copyright for 2019 year
[hespiff.git] / src / main / java / org / hedgecode / xml / xspf / bind / XSDPlaylistBinder.java
1 /*
2  * Copyright (c) 2015-2019. Developed by Hedgecode.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.hedgecode.xml.xspf.bind;
18
19 import java.util.ArrayList;
20 import java.util.Date;
21 import java.util.GregorianCalendar;
22 import java.util.List;
23 import java.util.Map;
24
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;
30
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;
42
43 /**
44  *
45  *
46  * @author Dmitry Samoshin aka gotty
47  */
48 public class XSDPlaylistBinder extends AbstractXSDBinder implements PlaylistBinder {
49
50     private static final String REF_LOCATION_NAME = "location";
51     private static final String REF_IDENTIFIER_NAME = "identifier";
52
53     private PlaylistType xsdPlaylist;
54
55     public XSDPlaylistBinder() {
56         this.xsdPlaylist = XSD_FACTORY.createPlaylistType();
57     }
58
59     public XSDPlaylistBinder(PlaylistType xsdPlaylist) {
60         this.xsdPlaylist = xsdPlaylist;
61     }
62
63     @Override
64     public String getPackageName() {
65         return xsdPlaylist.getClass().getPackage().getName();
66     }
67
68     @Override
69     public Class getPlaylistClass() {
70         return xsdPlaylist.getClass();
71     }
72
73     @Override
74     public Object getPlaylist() {
75         return xsdPlaylist;
76     }
77
78     @Override
79     public void setPlaylist(Object playlist) {
80         if (playlist instanceof PlaylistType)
81             this.xsdPlaylist = (PlaylistType) playlist;
82     }
83
84     @Override
85     public JAXBElement wrapPlaylist() {
86         return XSD_FACTORY.createPlaylist(xsdPlaylist);
87     }
88
89     @Override
90     public String getTitle() {
91         return xsdPlaylist.getTitle();
92     }
93
94     @Override
95     public void setTitle(String title) {
96         xsdPlaylist.setTitle(title);
97     }
98
99     @Override
100     public String getCreator() {
101         return xsdPlaylist.getCreator();
102     }
103
104     @Override
105     public void setCreator(String creator) {
106         xsdPlaylist.setCreator(creator);
107     }
108
109     @Override
110     public String getAnnotation() {
111         return xsdPlaylist.getAnnotation();
112     }
113
114     @Override
115     public void setAnnotation(String annotation) {
116         xsdPlaylist.setAnnotation(annotation);
117     }
118
119     @Override
120     public String getInfo() {
121         return xsdPlaylist.getInfo();
122     }
123
124     @Override
125     public void setInfo(String info) {
126         xsdPlaylist.setInfo(info);
127     }
128
129     @Override
130     public String getLocation() {
131         return xsdPlaylist.getLocation();
132     }
133
134     @Override
135     public void setLocation(String location) {
136         xsdPlaylist.setLocation(location);
137     }
138
139     @Override
140     public String getIdentifier() {
141         return xsdPlaylist.getIdentifier();
142     }
143
144     @Override
145     public void setIdentifier(String identifier) {
146         xsdPlaylist.setIdentifier(identifier);
147     }
148
149     @Override
150     public String getImage() {
151         return xsdPlaylist.getImage();
152     }
153
154     @Override
155     public void setImage(String image) {
156         xsdPlaylist.setImage(image);
157     }
158
159     @Override
160     public Date getDate() {
161         XMLGregorianCalendar calendar = xsdPlaylist.getDate();
162         if (calendar != null)
163             return calendar.toGregorianCalendar().getTime();
164         return null;
165     }
166
167     @Override
168     public void setDate(Date date) {
169         XMLGregorianCalendar xmlCalendar = null;
170         if (date != null) {
171             GregorianCalendar gCalendar = new GregorianCalendar();
172             gCalendar.setTime(date);
173             try {
174                 xmlCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gCalendar);
175             } catch (DatatypeConfigurationException ignored) {}
176         }
177         xsdPlaylist.setDate(xmlCalendar);
178     }
179
180     @Override
181     public String getLicense() {
182         return xsdPlaylist.getLicense();
183     }
184
185     @Override
186     public void setLicense(String license) {
187         xsdPlaylist.setLicense(license);
188     }
189
190     @Override
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) {
197
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();
202
203                 if (identifier != null && location != null) {
204                     attribution.add(identifier, location);
205                     identifier = null;
206                     location = null;
207                 }
208             }
209             return attribution;
210         }
211         return null;
212     }
213
214     @Override
215     public void setAttribution(Attribution attribution) {
216         if (attribution != null) {
217             if (xsdPlaylist.getAttribution() == null) {
218                 xsdPlaylist.setAttribution(
219                         XSD_FACTORY.createAttributionType()
220                 );
221             }
222             for (Map.Entry<String, String> element : attribution.get().entrySet()) {
223                 xsdPlaylist.getAttribution().getIdentifierOrLocation().add(
224                         XSD_FACTORY.createAttributionTypeIdentifier(element.getKey())
225                 );
226                 xsdPlaylist.getAttribution().getIdentifierOrLocation().add(
227                         XSD_FACTORY.createAttributionTypeLocation(element.getValue())
228                 );
229             }
230         }
231     }
232
233     @Override
234     public List<Link> getLinks() {
235         List<Link> links = new ArrayList<>();
236         for (LinkType link : xsdPlaylist.getLink()) {
237             links.add(
238                     PlaylistFactory.createLink(link.getRel(), link.getValue())
239             );
240         }
241         return links;
242     }
243
244     @Override
245     public void addLink(Link link) {
246         if (link != null) {
247             LinkType linkType = XSD_FACTORY.createLinkType();
248             linkType.setRel(link.getRel());
249             linkType.setValue(link.getContent());
250             xsdPlaylist.getLink().add(
251                     linkType
252             );
253         }
254     }
255
256     @Override
257     public List<Meta> getMetas() {
258         List<Meta> metas = new ArrayList<>();
259         for (MetaType meta : xsdPlaylist.getMeta()) {
260             metas.add(
261                     PlaylistFactory.createMeta(meta.getRel(), meta.getValue())
262             );
263         }
264         return metas;
265     }
266
267     @Override
268     public void addMeta(Meta meta) {
269         if (meta != null) {
270             MetaType metaType = XSD_FACTORY.createMetaType();
271             metaType.setRel(meta.getRel());
272             metaType.setValue(meta.getContent());
273             xsdPlaylist.getMeta().add(
274                     metaType
275             );
276         }
277     }
278
279     @Override
280     public List<Extension> getExtensions() {
281         List<Extension> extensions = new ArrayList<>();
282         for (ExtensionType extension : xsdPlaylist.getExtension()) {
283             extensions.add(
284                     PlaylistFactory.createExtension(
285                             extension.getApplication(), extension.getContent()
286                     )
287             );
288         }
289         return extensions;
290     }
291
292     @Override
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(
299                     extensionType
300             );
301         }
302     }
303
304     @Override
305     public List<Track> getTracks() {
306         List<Track> tracks = new ArrayList<>();
307         if (xsdPlaylist.getTrackList() != null) {
308             for (TrackType track : xsdPlaylist.getTrackList().getTrack()) {
309                 tracks.add(
310                         PlaylistFactory.createTrack(
311                                 BinderFactory.createTrackBinder(track)
312                         )
313                 );
314             }
315         }
316         return tracks;
317     }
318
319     @Override
320     public void addTrack(Track track) {
321         if (track != null) {
322             if (xsdPlaylist.getTrackList() == null) {
323                 xsdPlaylist.setTrackList(
324                         XSD_FACTORY.createTrackListType()
325                 );
326             }
327             TrackType trackType = XSD_FACTORY.createTrackType();
328
329             PlaylistFactory.bindTrack(
330                     track,
331                     BinderFactory.createTrackBinder(trackType)
332             );
333
334             xsdPlaylist.getTrackList().getTrack().add(
335                     trackType
336             );
337         }
338     }
339
340     @Override
341     public String getVersion() {
342         return xsdPlaylist.getVersion();
343     }
344
345     @Override
346     public void setVersion(String version) {
347         xsdPlaylist.setVersion(version);
348     }
349
350 }