2 * Copyright (c) 2015-2019. Developed by Hedgecode.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.hedgecode.xml.xspf;
20 import java.io.UnsupportedEncodingException;
22 import javax.xml.bind.JAXBException;
24 import org.hedgecode.xml.xspf.validate.ValidateException;
26 import static org.hedgecode.xml.xspf.XSPFConstants.Format;
27 import static org.hedgecode.xml.xspf.XSPFConstants.Version;
30 * XSPF API for external applications.
32 * @author Dmitry Samoshin aka gotty
34 public final class XSPF {
36 public static Playlist create() {
37 return create(Format.RELAX_NG);
40 public static Playlist create(Format format) {
41 return new XSPFPlaylist(format);
44 public static Playlist create(Version version) {
45 return new XSPFPlaylist(version);
48 public static Playlist create(String key) {
49 Format format = Format.byName(key);
51 return create(format);
53 Version version = Version.byName(key);
55 return create(version);
60 public static void generate(Playlist playlist, File outputFile) throws XSPFException {
62 throw new XSPFException(XSPFConstants.Errors.NULL_PLAYLIST);
64 playlist.write(outputFile);
65 } catch (JAXBException e) {
66 throw new XSPFException(XSPFConstants.Errors.BINDING_ERROR);
70 public static String generate(Playlist playlist) throws XSPFException {
72 throw new XSPFException(XSPFConstants.Errors.NULL_PLAYLIST);
75 result = playlist.getAsString();
76 } catch (JAXBException e) {
77 throw new XSPFException(XSPFConstants.Errors.BINDING_ERROR);
78 } catch (UnsupportedEncodingException e) {
79 throw new XSPFException(XSPFConstants.Errors.ENCODING_ERROR);
84 public static Playlist create(File inputFile) throws JAXBException {
85 Playlist playlist = PlaylistFactory.createPlaylist(Format.XSD);
86 playlist.read(inputFile);
90 public static Track createTrack(String location, String title) {
91 Track track = PlaylistFactory.createTrack();
92 track.addLocation(location);
93 track.setTitle(title);
97 public static Track addTrack(Playlist playlist, String location, String title) {
98 Track track = createTrack(location, title);
99 playlist.addTrack(track);
103 public static void addTrack(Playlist playlist, Track track) {
104 playlist.addTrack(track);
107 public static void addTracks(
108 Playlist playlist, Playlist tracksPlaylist) throws XSPFException
110 if (playlist == null)
111 throw new XSPFException(XSPFConstants.Errors.NULL_PLAYLIST);
112 if (tracksPlaylist != null) {
113 for (Track track : tracksPlaylist.getTracks()) {
114 playlist.addTrack(track);
119 public static void setProperty(String name, Object value) {
120 XSPFProperties.setProperty(name, value);
123 public static int validate(File inputFile) throws JAXBException {
125 Playlist playlist = create(inputFile);
126 if (playlist != null)
129 } catch (ValidateException e) {
130 result = e.getCode();
135 public static String get(int code) {