[LIB-4] Add hespiff source files
[hespiff.git] / src / main / java / org / hedgecode / xml / xspf / bind / XSDPlaylistBinder.java
diff --git a/src/main/java/org/hedgecode/xml/xspf/bind/XSDPlaylistBinder.java b/src/main/java/org/hedgecode/xml/xspf/bind/XSDPlaylistBinder.java
new file mode 100644 (file)
index 0000000..0a7a346
--- /dev/null
@@ -0,0 +1,350 @@
+/*
+ * 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.Date;
+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.Attribution;
+import org.hedgecode.xml.xspf.Extension;
+import org.hedgecode.xml.xspf.Link;
+import org.hedgecode.xml.xspf.Meta;
+import org.hedgecode.xml.xspf.PlaylistFactory;
+import org.hedgecode.xml.xspf.Track;
+import org.hedgecode.xml.xspf.xsd.ExtensionType;
+import org.hedgecode.xml.xspf.xsd.LinkType;
+import org.hedgecode.xml.xspf.xsd.MetaType;
+import org.hedgecode.xml.xspf.xsd.PlaylistType;
+import org.hedgecode.xml.xspf.xsd.TrackType;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public class XSDPlaylistBinder extends AbstractXSDBinder implements PlaylistBinder {
+
+    private static final String REF_LOCATION_NAME = "location";
+    private static final String REF_IDENTIFIER_NAME = "identifier";
+
+    private PlaylistType xsdPlaylist;
+
+    public XSDPlaylistBinder() {
+        this.xsdPlaylist = XSD_FACTORY.createPlaylistType();
+    }
+
+    public XSDPlaylistBinder(PlaylistType xsdPlaylist) {
+        this.xsdPlaylist = xsdPlaylist;
+    }
+
+    @Override
+    public String getPackageName() {
+        return xsdPlaylist.getClass().getPackage().getName();
+    }
+
+    @Override
+    public Class getPlaylistClass() {
+        return xsdPlaylist.getClass();
+    }
+
+    @Override
+    public Object getPlaylist() {
+        return xsdPlaylist;
+    }
+
+    @Override
+    public void setPlaylist(Object playlist) {
+        if (playlist instanceof PlaylistType)
+            this.xsdPlaylist = (PlaylistType) playlist;
+    }
+
+    @Override
+    public JAXBElement wrapPlaylist() {
+        return XSD_FACTORY.createPlaylist(xsdPlaylist);
+    }
+
+    @Override
+    public String getTitle() {
+        return xsdPlaylist.getTitle();
+    }
+
+    @Override
+    public void setTitle(String title) {
+        xsdPlaylist.setTitle(title);
+    }
+
+    @Override
+    public String getCreator() {
+        return xsdPlaylist.getCreator();
+    }
+
+    @Override
+    public void setCreator(String creator) {
+        xsdPlaylist.setCreator(creator);
+    }
+
+    @Override
+    public String getAnnotation() {
+        return xsdPlaylist.getAnnotation();
+    }
+
+    @Override
+    public void setAnnotation(String annotation) {
+        xsdPlaylist.setAnnotation(annotation);
+    }
+
+    @Override
+    public String getInfo() {
+        return xsdPlaylist.getInfo();
+    }
+
+    @Override
+    public void setInfo(String info) {
+        xsdPlaylist.setInfo(info);
+    }
+
+    @Override
+    public String getLocation() {
+        return xsdPlaylist.getLocation();
+    }
+
+    @Override
+    public void setLocation(String location) {
+        xsdPlaylist.setLocation(location);
+    }
+
+    @Override
+    public String getIdentifier() {
+        return xsdPlaylist.getIdentifier();
+    }
+
+    @Override
+    public void setIdentifier(String identifier) {
+        xsdPlaylist.setIdentifier(identifier);
+    }
+
+    @Override
+    public String getImage() {
+        return xsdPlaylist.getImage();
+    }
+
+    @Override
+    public void setImage(String image) {
+        xsdPlaylist.setImage(image);
+    }
+
+    @Override
+    public Date getDate() {
+        XMLGregorianCalendar calendar = xsdPlaylist.getDate();
+        if (calendar != null)
+            return calendar.toGregorianCalendar().getTime();
+        return null;
+    }
+
+    @Override
+    public void setDate(Date date) {
+        XMLGregorianCalendar xmlCalendar = null;
+        if (date != null) {
+            GregorianCalendar gCalendar = new GregorianCalendar();
+            gCalendar.setTime(date);
+            try {
+                xmlCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gCalendar);
+            } catch (DatatypeConfigurationException ignored) {}
+        }
+        xsdPlaylist.setDate(xmlCalendar);
+    }
+
+    @Override
+    public String getLicense() {
+        return xsdPlaylist.getLicense();
+    }
+
+    @Override
+    public void setLicense(String license) {
+        xsdPlaylist.setLicense(license);
+    }
+
+    @Override
+    public Attribution getAttribution() {
+        if (xsdPlaylist.getAttribution() != null) {
+            List<JAXBElement<String>> elements = xsdPlaylist.getAttribution().getIdentifierOrLocation();
+            Attribution attribution = PlaylistFactory.createAttribution();
+            String identifier = null, location = null;
+            for (JAXBElement<String> element : elements) {
+
+                if (REF_IDENTIFIER_NAME.equals(element.getName().getLocalPart()))
+                    identifier = element.getValue();
+                else if (REF_LOCATION_NAME.equals(element.getName().getLocalPart()))
+                    location = element.getValue();
+
+                if (identifier != null && location != null) {
+                    attribution.add(identifier, location);
+                    identifier = null;
+                    location = null;
+                }
+            }
+            return attribution;
+        }
+        return null;
+    }
+
+    @Override
+    public void setAttribution(Attribution attribution) {
+        if (attribution != null) {
+            if (xsdPlaylist.getAttribution() == null) {
+                xsdPlaylist.setAttribution(
+                        XSD_FACTORY.createAttributionType()
+                );
+            }
+            for (Map.Entry<String, String> element : attribution.get().entrySet()) {
+                xsdPlaylist.getAttribution().getIdentifierOrLocation().add(
+                        XSD_FACTORY.createAttributionTypeIdentifier(element.getKey())
+                );
+                xsdPlaylist.getAttribution().getIdentifierOrLocation().add(
+                        XSD_FACTORY.createAttributionTypeLocation(element.getValue())
+                );
+            }
+        }
+    }
+
+    @Override
+    public List<Link> getLinks() {
+        List<Link> links = new ArrayList<>();
+        for (LinkType link : xsdPlaylist.getLink()) {
+            links.add(
+                    PlaylistFactory.createLink(link.getRel(), link.getValue())
+            );
+        }
+        return links;
+    }
+
+    @Override
+    public void addLink(Link link) {
+        if (link != null) {
+            LinkType linkType = XSD_FACTORY.createLinkType();
+            linkType.setRel(link.getRel());
+            linkType.setValue(link.getContent());
+            xsdPlaylist.getLink().add(
+                    linkType
+            );
+        }
+    }
+
+    @Override
+    public List<Meta> getMetas() {
+        List<Meta> metas = new ArrayList<>();
+        for (MetaType meta : xsdPlaylist.getMeta()) {
+            metas.add(
+                    PlaylistFactory.createMeta(meta.getRel(), meta.getValue())
+            );
+        }
+        return metas;
+    }
+
+    @Override
+    public void addMeta(Meta meta) {
+        if (meta != null) {
+            MetaType metaType = XSD_FACTORY.createMetaType();
+            metaType.setRel(meta.getRel());
+            metaType.setValue(meta.getContent());
+            xsdPlaylist.getMeta().add(
+                    metaType
+            );
+        }
+    }
+
+    @Override
+    public List<Extension> getExtensions() {
+        List<Extension> extensions = new ArrayList<>();
+        for (ExtensionType extension : xsdPlaylist.getExtension()) {
+            extensions.add(
+                    PlaylistFactory.createExtension(
+                            extension.getApplication(), extension.getContent()
+                    )
+            );
+        }
+        return extensions;
+    }
+
+    @Override
+    public void addExtension(Extension extension) {
+        if (extension != null) {
+            ExtensionType extensionType = XSD_FACTORY.createExtensionType();
+            extensionType.setApplication(extension.getApplication());
+            extensionType.getContent().addAll(extension.getContent());
+            xsdPlaylist.getExtension().add(
+                    extensionType
+            );
+        }
+    }
+
+    @Override
+    public List<Track> getTracks() {
+        List<Track> tracks = new ArrayList<>();
+        if (xsdPlaylist.getTrackList() != null) {
+            for (TrackType track : xsdPlaylist.getTrackList().getTrack()) {
+                tracks.add(
+                        PlaylistFactory.createTrack(
+                                BinderFactory.createTrackBinder(track)
+                        )
+                );
+            }
+        }
+        return tracks;
+    }
+
+    @Override
+    public void addTrack(Track track) {
+        if (track != null) {
+            if (xsdPlaylist.getTrackList() == null) {
+                xsdPlaylist.setTrackList(
+                        XSD_FACTORY.createTrackListType()
+                );
+            }
+            TrackType trackType = XSD_FACTORY.createTrackType();
+
+            PlaylistFactory.bindTrack(
+                    track,
+                    BinderFactory.createTrackBinder(trackType)
+            );
+
+            xsdPlaylist.getTrackList().getTrack().add(
+                    trackType
+            );
+        }
+    }
+
+    @Override
+    public String getVersion() {
+        return xsdPlaylist.getVersion();
+    }
+
+    @Override
+    public void setVersion(String version) {
+        xsdPlaylist.setVersion(version);
+    }
+
+}