[LIB-4] Add hespiff source files
[hespiff.git] / src / main / java / org / hedgecode / xml / xspf / XSPF.java
diff --git a/src/main/java/org/hedgecode/xml/xspf/XSPF.java b/src/main/java/org/hedgecode/xml/xspf/XSPF.java
new file mode 100644 (file)
index 0000000..845c57f
--- /dev/null
@@ -0,0 +1,139 @@
+/*
+ * 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;
+
+import java.io.File;
+import java.io.UnsupportedEncodingException;
+
+import javax.xml.bind.JAXBException;
+
+import org.hedgecode.xml.xspf.validate.ValidateException;
+
+import static org.hedgecode.xml.xspf.XSPFConstants.Format;
+import static org.hedgecode.xml.xspf.XSPFConstants.Version;
+
+/**
+ * XSPF API for external applications.
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public final class XSPF {
+
+    public static Playlist create() {
+        return create(Format.RELAX_NG);
+    }
+
+    public static Playlist create(Format format) {
+        return new XSPFPlaylist(format);
+    }
+
+    public static Playlist create(Version version) {
+        return new XSPFPlaylist(version);
+    }
+
+    public static Playlist create(String key) {
+        Format format = Format.byName(key);
+        if (format != null)
+            return create(format);
+
+        Version version = Version.byName(key);
+        if (version != null)
+            return create(version);
+
+        return null;
+    }
+
+    public static void generate(Playlist playlist, File outputFile) throws XSPFException {
+        if (playlist == null)
+            throw new XSPFException(XSPFConstants.Errors.NULL_PLAYLIST);
+        try {
+            playlist.write(outputFile);
+        } catch (JAXBException e) {
+            throw new XSPFException(XSPFConstants.Errors.BINDING_ERROR);
+        }
+    }
+
+    public static String generate(Playlist playlist) throws XSPFException {
+        if (playlist == null)
+            throw new XSPFException(XSPFConstants.Errors.NULL_PLAYLIST);
+        String result;
+        try {
+            result = playlist.getAsString();
+        } catch (JAXBException e) {
+            throw new XSPFException(XSPFConstants.Errors.BINDING_ERROR);
+        } catch (UnsupportedEncodingException e) {
+            throw new XSPFException(XSPFConstants.Errors.ENCODING_ERROR);
+        }
+        return result;
+    }
+
+    public static Playlist create(File inputFile) throws JAXBException {
+        Playlist playlist = PlaylistFactory.createPlaylist(Format.XSD);
+        playlist.read(inputFile);
+        return playlist;
+    }
+
+    public static Track createTrack(String location, String title) {
+        Track track = PlaylistFactory.createTrack();
+        track.addLocation(location);
+        track.setTitle(title);
+        return track;
+    }
+
+    public static Track addTrack(Playlist playlist, String location, String title) {
+        Track track = createTrack(location, title);
+        playlist.addTrack(track);
+        return track;
+    }
+
+    public static void addTrack(Playlist playlist, Track track) {
+        playlist.addTrack(track);
+    }
+
+    public static void addTracks(
+            Playlist playlist, Playlist tracksPlaylist) throws XSPFException
+    {
+        if (playlist == null)
+            throw new XSPFException(XSPFConstants.Errors.NULL_PLAYLIST);
+        if (tracksPlaylist != null) {
+            for (Track track : tracksPlaylist.getTracks()) {
+                playlist.addTrack(track);
+            }
+        }
+    }
+
+    public static void setProperty(String name, Object value) {
+        Properties.setProperty(name, value);
+    }
+
+    public static int validate(File inputFile) throws JAXBException {
+        int result = 0;
+        Playlist playlist = create(inputFile);
+        if (playlist != null)
+            try {
+                playlist.validate();
+            } catch (ValidateException e) {
+                result = e.getCode();
+            }
+        return result;
+    }
+
+    public static String get(int code) {
+        return null; // todo
+    }
+
+}