[LIB-4] Extend copyright for 2019 year
[hespiff.git] / src / main / java / org / hedgecode / xml / xspf / XSPF.java
1 /*
2  * Copyright (c) 2015-2019. 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;
18
19 import java.io.File;
20 import java.io.UnsupportedEncodingException;
21
22 import javax.xml.bind.JAXBException;
23
24 import org.hedgecode.xml.xspf.validate.ValidateException;
25
26 import static org.hedgecode.xml.xspf.XSPFConstants.Format;
27 import static org.hedgecode.xml.xspf.XSPFConstants.Version;
28
29 /**
30  * XSPF API for external applications.
31  *
32  * @author Dmitry Samoshin aka gotty
33  */
34 public final class XSPF {
35
36     public static Playlist create() {
37         return create(Format.RELAX_NG);
38     }
39
40     public static Playlist create(Format format) {
41         return new XSPFPlaylist(format);
42     }
43
44     public static Playlist create(Version version) {
45         return new XSPFPlaylist(version);
46     }
47
48     public static Playlist create(String key) {
49         Format format = Format.byName(key);
50         if (format != null)
51             return create(format);
52
53         Version version = Version.byName(key);
54         if (version != null)
55             return create(version);
56
57         return null;
58     }
59
60     public static void generate(Playlist playlist, File outputFile) throws XSPFException {
61         if (playlist == null)
62             throw new XSPFException(XSPFConstants.Errors.NULL_PLAYLIST);
63         try {
64             playlist.write(outputFile);
65         } catch (JAXBException e) {
66             throw new XSPFException(XSPFConstants.Errors.BINDING_ERROR);
67         }
68     }
69
70     public static String generate(Playlist playlist) throws XSPFException {
71         if (playlist == null)
72             throw new XSPFException(XSPFConstants.Errors.NULL_PLAYLIST);
73         String result;
74         try {
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);
80         }
81         return result;
82     }
83
84     public static Playlist create(File inputFile) throws JAXBException {
85         Playlist playlist = PlaylistFactory.createPlaylist(Format.XSD);
86         playlist.read(inputFile);
87         return playlist;
88     }
89
90     public static Track createTrack(String location, String title) {
91         Track track = PlaylistFactory.createTrack();
92         track.addLocation(location);
93         track.setTitle(title);
94         return track;
95     }
96
97     public static Track addTrack(Playlist playlist, String location, String title) {
98         Track track = createTrack(location, title);
99         playlist.addTrack(track);
100         return track;
101     }
102
103     public static void addTrack(Playlist playlist, Track track) {
104         playlist.addTrack(track);
105     }
106
107     public static void addTracks(
108             Playlist playlist, Playlist tracksPlaylist) throws XSPFException
109     {
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);
115             }
116         }
117     }
118
119     public static void setProperty(String name, Object value) {
120         Properties.setProperty(name, value);
121     }
122
123     public static int validate(File inputFile) throws JAXBException {
124         int result = 0;
125         Playlist playlist = create(inputFile);
126         if (playlist != null)
127             try {
128                 playlist.validate();
129             } catch (ValidateException e) {
130                 result = e.getCode();
131             }
132         return result;
133     }
134
135     public static String get(int code) {
136         return null; // todo
137     }
138
139 }