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;
19 import java.io.ByteArrayOutputStream;
21 import java.io.OutputStream;
22 import java.io.UnsupportedEncodingException;
23 import java.nio.charset.Charset;
24 import java.util.Date;
25 import java.util.List;
27 import javax.xml.bind.JAXBContext;
28 import javax.xml.bind.JAXBElement;
29 import javax.xml.bind.JAXBException;
30 import javax.xml.bind.Marshaller;
31 import javax.xml.bind.Unmarshaller;
32 import javax.xml.bind.annotation.XmlRootElement;
34 import org.hedgecode.xml.xspf.bind.Binder;
35 import org.hedgecode.xml.xspf.bind.BinderFactory;
36 import org.hedgecode.xml.xspf.bind.PlaylistBinder;
37 import org.hedgecode.xml.xspf.validate.ValidateException;
39 import static org.hedgecode.xml.xspf.XSPFConstants.Format;
44 * @author Dmitry Samoshin aka gotty
46 public class XMLBindPlaylist extends AbstractXMLBindElement implements Playlist {
48 private PlaylistBinder binder;
50 protected XMLBindPlaylist(PlaylistBinder binder) {
52 this.binder = BinderFactory.createPlaylistBinder();
58 protected XMLBindPlaylist(Format format) {
59 this.binder = BinderFactory.createPlaylistBinder(format);
63 private void setDefaults() {
64 binder.setVersion(XSPFConstants.XSPF_VERSION);
68 protected Binder getBinder() {
73 public String getLocation() {
74 return binder.getLocation();
78 public void setLocation(String location) {
79 binder.setLocation(location);
83 public String getIdentifier() {
84 return binder.getIdentifier();
88 public void setIdentifier(String identifier) {
89 binder.setIdentifier(identifier);
93 public Date getDate() {
94 return binder.getDate();
98 public void setDate(Date date) {
103 public String getLicense() {
104 return binder.getLicense();
108 public void setLicense(String license) {
109 binder.setLicense(license);
113 public Attribution getAttribution() {
114 return binder.getAttribution();
118 public void setAttribution(Attribution attribution) {
119 binder.setAttribution(attribution);
123 public List<Track> getTracks() {
124 return binder.getTracks();
128 public void addTrack(Track track) {
129 binder.addTrack(track);
132 public void read(File inputFile) throws JAXBException {
135 binder.getPlaylistClass(),
137 binder.getPlaylistClass()
138 ).unmarshal(inputFile)
144 public void write(File outputFile) throws JAXBException {
146 binder.getPlaylistClass()
148 wrapPlaylist(binder),
154 public OutputStream getAsStream() throws JAXBException {
155 ByteArrayOutputStream baos = new ByteArrayOutputStream();
157 binder.getPlaylistClass()
159 wrapPlaylist(binder),
166 public String getAsString()
167 throws JAXBException, UnsupportedEncodingException
169 Charset charset = XSPFProperties.getCharset();
170 ByteArrayOutputStream os = (ByteArrayOutputStream) getAsStream();
176 private Marshaller createMarshaller(Class xmlRootClass) throws JAXBException {
177 JAXBContext jaxbContext = JAXBContext.newInstance(xmlRootClass);
178 Marshaller marshaller = jaxbContext.createMarshaller();
179 Charset charset = XSPFProperties.getCharset();
180 marshaller.setProperty(
181 Marshaller.JAXB_ENCODING,
184 marshaller.setProperty(
185 Marshaller.JAXB_FORMATTED_OUTPUT,
186 XSPFProperties.getBoolean(XSPFProperties.FORMATTED)
188 if (Boolean.FALSE.equals(XSPFProperties.getBoolean(XSPFProperties.STANDALONE))) {
189 marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
190 marshaller.setProperty(
191 XSPFConstants.XML_HEADER_NAME,
192 XSPFConstants.XML_HEADER_VALUE.replace("%CHARSET%", charset.name())
198 private Unmarshaller createUnmarshaller(Class xmlRootClass) throws JAXBException {
199 JAXBContext jaxbContext = JAXBContext.newInstance(xmlRootClass);
200 return jaxbContext.createUnmarshaller();
203 private Object extractRootObject(Class xmlRootClass, Object unmarshallingObject) {
204 if (xmlRootClass.getAnnotation(XmlRootElement.class) != null)
205 return unmarshallingObject;
206 return unmarshallingObject instanceof JAXBElement
207 ? ((JAXBElement) unmarshallingObject).getValue()
212 private Object wrapPlaylist(PlaylistBinder binder) {
213 if (binder.getPlaylistClass().getAnnotation(XmlRootElement.class) != null)
214 return binder.getPlaylist();
215 return binder.wrapPlaylist();
219 public void validate() throws ValidateException {