[LIB-4] Add hespiff source files
[hespiff.git] / src / main / java / org / hedgecode / xml / xspf / bind / RNGPlaylistBinder.java
1 /*
2  * Copyright (c) 2015. 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.GregorianCalendar;
21 import java.util.List;
22 import java.util.Map;
23
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;
29
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;
40
41 /**
42  *
43  *
44  * @author Dmitry Samoshin aka gotty
45  */
46 public class RNGPlaylistBinder extends AbstractRNGBinder implements PlaylistBinder {
47
48     private Playlist rngPlaylist;
49
50     public RNGPlaylistBinder() {
51         this.rngPlaylist = RNG_FACTORY.createPlaylist();
52     }
53
54     public RNGPlaylistBinder(Playlist rngPlaylist) {
55         this.rngPlaylist = rngPlaylist;
56     }
57
58     @Override
59     protected Object getElement(Class elementClass) {
60         for (Object element : rngPlaylist.getTitleOrCreatorOrAnnotation()) {
61             if (element.getClass().equals(elementClass))
62                 return element;
63         }
64         return null;
65     }
66
67     @Override
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);
73         }
74         return elements;
75     }
76
77     @Override
78     protected List<Object> getAllElements() {
79         return rngPlaylist.getTitleOrCreatorOrAnnotation();
80     }
81
82     @Override
83     public String getPackageName() {
84         return rngPlaylist.getClass().getPackage().getName();
85     }
86
87     @Override
88     public Class getPlaylistClass() {
89         return rngPlaylist.getClass();
90     }
91
92     @Override
93     public Object getPlaylist() {
94         return rngPlaylist;
95     }
96
97     @Override
98     public void setPlaylist(Object playlist) {
99         if (playlist instanceof Playlist)
100             this.rngPlaylist = (Playlist) playlist;
101     }
102
103     @Override
104     public JAXBElement wrapPlaylist() {
105         return new JAXBElement<>(
106                 XSPFConstants.PLAYLIST_QNAME,
107                 Playlist.class,
108                 rngPlaylist
109         );
110     }
111
112     @Override
113     public String getLocation() {
114         Location locationElement = (Location) getElement(Location.class);
115         return locationElement != null ? locationElement.getValue() : null;
116     }
117
118     @Override
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);
127         }
128     }
129
130     @Override
131     public String getIdentifier() {
132         Identifier identifierElement = (Identifier) getElement(Identifier.class);
133         return identifierElement != null ? identifierElement.getValue() : null;
134     }
135
136     @Override
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);
145         }
146     }
147
148     @Override
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()
153                 : null;
154     }
155
156     @Override
157     public void setDate(java.util.Date date) {
158         Date dateElement = (Date) getElement(Date.class);
159         if (dateElement != null)
160             getAllElements().remove(dateElement);
161         if (date != null) {
162             dateElement = RNG_FACTORY.createDate();
163             GregorianCalendar gCalendar = new GregorianCalendar();
164             gCalendar.setTime(date);
165             XMLGregorianCalendar xmlCalendar = null;
166             try {
167                 xmlCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gCalendar);
168             } catch (DatatypeConfigurationException ignored) {}
169             dateElement.setValue(xmlCalendar);
170             getAllElements().add(dateElement);
171         }
172     }
173
174     @Override
175     public String getLicense() {
176         License licenseElement = (License) getElement(License.class);
177         return licenseElement != null ? licenseElement.getValue() : null;
178     }
179
180     @Override
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);
189         }
190     }
191
192     @Override
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()) {
199
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();
204
205                 if (identifier != null && location != null) {
206                     attribution.add(identifier, location);
207                     identifier = null;
208                     location = null;
209                 }
210             }
211             return attribution;
212         }
213         return null;
214     }
215
216     @Override
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);
230             }
231             getAllElements().add(attributionElement);
232         }
233     }
234
235     @Override
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()) {
241                 tracks.add(
242                         PlaylistFactory.createTrack(
243                                 BinderFactory.createTrackBinder(track)
244                         )
245                 );
246             }
247         }
248         return tracks;
249     }
250
251     @Override
252     public void addTrack(org.hedgecode.xml.xspf.Track track) {
253         if (track != null) {
254             TrackList trackListElement = (TrackList) getElement(TrackList.class);
255             if (trackListElement == null) {
256                 trackListElement = RNG_FACTORY.createTrackList();
257                 getAllElements().add(trackListElement);
258             }
259
260             Track trackElement = RNG_FACTORY.createTrack();
261             PlaylistFactory.bindTrack(
262                     track,
263                     BinderFactory.createTrackBinder(trackElement)
264             );
265
266             trackListElement.getTrack().add(
267                     trackElement
268             );
269         }
270     }
271
272     @Override
273     public String getVersion() {
274         return rngPlaylist.getVersion();
275     }
276
277     @Override
278     public void setVersion(String version) {
279         rngPlaylist.setVersion(version);
280     }
281
282 }