--- /dev/null
+/*
+ * Copyright (c) 2015. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.xml.xspf.bind;
+
+import java.util.ArrayList;
+import java.util.GregorianCalendar;
+import java.util.List;
+import java.util.Map;
+
+import javax.xml.bind.JAXBElement;
+import javax.xml.datatype.DatatypeConfigurationException;
+import javax.xml.datatype.DatatypeFactory;
+import javax.xml.datatype.XMLGregorianCalendar;
+import javax.xml.namespace.QName;
+
+import org.hedgecode.xml.xspf.PlaylistFactory;
+import org.hedgecode.xml.xspf.XSPFConstants;
+import org.hedgecode.xml.xspf.rng.Attribution;
+import org.hedgecode.xml.xspf.rng.Date;
+import org.hedgecode.xml.xspf.rng.Identifier;
+import org.hedgecode.xml.xspf.rng.License;
+import org.hedgecode.xml.xspf.rng.Location;
+import org.hedgecode.xml.xspf.rng.Playlist;
+import org.hedgecode.xml.xspf.rng.Track;
+import org.hedgecode.xml.xspf.rng.TrackList;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public class RNGPlaylistBinder extends AbstractRNGBinder implements PlaylistBinder {
+
+ private Playlist rngPlaylist;
+
+ public RNGPlaylistBinder() {
+ this.rngPlaylist = RNG_FACTORY.createPlaylist();
+ }
+
+ public RNGPlaylistBinder(Playlist rngPlaylist) {
+ this.rngPlaylist = rngPlaylist;
+ }
+
+ @Override
+ protected Object getElement(Class elementClass) {
+ for (Object element : rngPlaylist.getTitleOrCreatorOrAnnotation()) {
+ if (element.getClass().equals(elementClass))
+ return element;
+ }
+ return null;
+ }
+
+ @Override
+ protected List<Object> getElements(Class elementClass) {
+ List<Object> elements = new ArrayList<>();
+ for (Object element : rngPlaylist.getTitleOrCreatorOrAnnotation()) {
+ if (element.getClass().equals(elementClass))
+ elements.add(element);
+ }
+ return elements;
+ }
+
+ @Override
+ protected List<Object> getAllElements() {
+ return rngPlaylist.getTitleOrCreatorOrAnnotation();
+ }
+
+ @Override
+ public String getPackageName() {
+ return rngPlaylist.getClass().getPackage().getName();
+ }
+
+ @Override
+ public Class getPlaylistClass() {
+ return rngPlaylist.getClass();
+ }
+
+ @Override
+ public Object getPlaylist() {
+ return rngPlaylist;
+ }
+
+ @Override
+ public void setPlaylist(Object playlist) {
+ if (playlist instanceof Playlist)
+ this.rngPlaylist = (Playlist) playlist;
+ }
+
+ @Override
+ public JAXBElement wrapPlaylist() {
+ return new JAXBElement<>(
+ XSPFConstants.PLAYLIST_QNAME,
+ Playlist.class,
+ rngPlaylist
+ );
+ }
+
+ @Override
+ public String getLocation() {
+ Location locationElement = (Location) getElement(Location.class);
+ return locationElement != null ? locationElement.getValue() : null;
+ }
+
+ @Override
+ public void setLocation(String location) {
+ Location locationElement = (Location) getElement(Location.class);
+ if (locationElement != null)
+ getAllElements().remove(locationElement);
+ if (location != null) {
+ locationElement = RNG_FACTORY.createLocation();
+ locationElement.setValue(location);
+ getAllElements().add(locationElement);
+ }
+ }
+
+ @Override
+ public String getIdentifier() {
+ Identifier identifierElement = (Identifier) getElement(Identifier.class);
+ return identifierElement != null ? identifierElement.getValue() : null;
+ }
+
+ @Override
+ public void setIdentifier(String identifier) {
+ Identifier identifierElement = (Identifier) getElement(Identifier.class);
+ if (identifierElement != null)
+ getAllElements().remove(identifierElement);
+ if (identifier != null) {
+ identifierElement = RNG_FACTORY.createIdentifier();
+ identifierElement.setValue(identifier);
+ getAllElements().add(identifierElement);
+ }
+ }
+
+ @Override
+ public java.util.Date getDate() {
+ Date dateElement = (Date) getElement(Date.class);
+ return dateElement != null && dateElement.getValue() != null
+ ? dateElement.getValue().toGregorianCalendar().getTime()
+ : null;
+ }
+
+ @Override
+ public void setDate(java.util.Date date) {
+ Date dateElement = (Date) getElement(Date.class);
+ if (dateElement != null)
+ getAllElements().remove(dateElement);
+ if (date != null) {
+ dateElement = RNG_FACTORY.createDate();
+ GregorianCalendar gCalendar = new GregorianCalendar();
+ gCalendar.setTime(date);
+ XMLGregorianCalendar xmlCalendar = null;
+ try {
+ xmlCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gCalendar);
+ } catch (DatatypeConfigurationException ignored) {}
+ dateElement.setValue(xmlCalendar);
+ getAllElements().add(dateElement);
+ }
+ }
+
+ @Override
+ public String getLicense() {
+ License licenseElement = (License) getElement(License.class);
+ return licenseElement != null ? licenseElement.getValue() : null;
+ }
+
+ @Override
+ public void setLicense(String license) {
+ License licenseElement = (License) getElement(License.class);
+ if (licenseElement != null)
+ getAllElements().remove(licenseElement);
+ if (license != null) {
+ licenseElement = RNG_FACTORY.createLicense();
+ licenseElement.setValue(license);
+ getAllElements().add(licenseElement);
+ }
+ }
+
+ @Override
+ public org.hedgecode.xml.xspf.Attribution getAttribution() {
+ Attribution attributionElement = (Attribution) getElement(Attribution.class);
+ if (attributionElement != null) {
+ org.hedgecode.xml.xspf.Attribution attribution = PlaylistFactory.createAttribution();
+ String identifier = null, location = null;
+ for (Object element : attributionElement.getIdentifierOrLocation()) {
+
+ if (element.getClass().equals(Identifier.class))
+ identifier = ((Identifier) element).getValue();
+ else if (element.getClass().equals(Location.class))
+ location = ((Location) element).getValue();
+
+ if (identifier != null && location != null) {
+ attribution.add(identifier, location);
+ identifier = null;
+ location = null;
+ }
+ }
+ return attribution;
+ }
+ return null;
+ }
+
+ @Override
+ public void setAttribution(org.hedgecode.xml.xspf.Attribution attribution) {
+ Attribution attributionElement = (Attribution) getElement(Attribution.class);
+ if (attributionElement != null)
+ getAllElements().remove(attributionElement);
+ if (attribution != null) {
+ attributionElement = RNG_FACTORY.createAttribution();
+ for (Map.Entry<String, String> element : attribution.get().entrySet()) {
+ Location location = RNG_FACTORY.createLocation();
+ location.setValue(element.getKey());
+ Identifier identifier = RNG_FACTORY.createIdentifier();
+ identifier.setValue(element.getValue());
+ attributionElement.getIdentifierOrLocation().add(location);
+ attributionElement.getIdentifierOrLocation().add(identifier);
+ }
+ getAllElements().add(attributionElement);
+ }
+ }
+
+ @Override
+ public List<org.hedgecode.xml.xspf.Track> getTracks() {
+ List<org.hedgecode.xml.xspf.Track> tracks = new ArrayList<>();
+ TrackList traclListElement = (TrackList) getElement(TrackList.class);
+ if (traclListElement != null) {
+ for (Track track : traclListElement.getTrack()) {
+ tracks.add(
+ PlaylistFactory.createTrack(
+ BinderFactory.createTrackBinder(track)
+ )
+ );
+ }
+ }
+ return tracks;
+ }
+
+ @Override
+ public void addTrack(org.hedgecode.xml.xspf.Track track) {
+ if (track != null) {
+ TrackList trackListElement = (TrackList) getElement(TrackList.class);
+ if (trackListElement == null) {
+ trackListElement = RNG_FACTORY.createTrackList();
+ getAllElements().add(trackListElement);
+ }
+
+ Track trackElement = RNG_FACTORY.createTrack();
+ PlaylistFactory.bindTrack(
+ track,
+ BinderFactory.createTrackBinder(trackElement)
+ );
+
+ trackListElement.getTrack().add(
+ trackElement
+ );
+ }
+ }
+
+ @Override
+ public String getVersion() {
+ return rngPlaylist.getVersion();
+ }
+
+ @Override
+ public void setVersion(String version) {
+ rngPlaylist.setVersion(version);
+ }
+
+}