2 * Copyright (c) 2015. 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 = (Charset) Properties.getProperty(
170 Properties.CHARSET, Charset.class
172 ByteArrayOutputStream os = (ByteArrayOutputStream) getAsStream();
178 private Marshaller createMarshaller(Class xmlRootClass) throws JAXBException {
179 JAXBContext jaxbContext = JAXBContext.newInstance(xmlRootClass);
180 Marshaller marshaller = jaxbContext.createMarshaller();
181 Charset charset = (Charset) Properties.getProperty(Properties.CHARSET, Charset.class);
182 marshaller.setProperty(
183 Marshaller.JAXB_ENCODING,
186 marshaller.setProperty(
187 Marshaller.JAXB_FORMATTED_OUTPUT,
188 Properties.getProperty(Properties.FORMATTED, Boolean.class)
190 Object standalone = Properties.getProperty(Properties.STANDALONE, Boolean.class);
191 if (Boolean.FALSE.equals(standalone)) {
192 marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
193 marshaller.setProperty(
194 XSPFConstants.XML_HEADER_NAME,
195 XSPFConstants.XML_HEADER_VALUE.replace("%CHARSET%", charset.name())
201 private Unmarshaller createUnmarshaller(Class xmlRootClass) throws JAXBException {
202 JAXBContext jaxbContext = JAXBContext.newInstance(xmlRootClass);
203 return jaxbContext.createUnmarshaller();
206 private Object extractRootObject(Class xmlRootClass, Object unmarshallingObject) {
207 if (xmlRootClass.getAnnotation(XmlRootElement.class) != null)
208 return unmarshallingObject;
209 return unmarshallingObject instanceof JAXBElement
210 ? ((JAXBElement) unmarshallingObject).getValue()
215 private Object wrapPlaylist(PlaylistBinder binder) {
216 if (binder.getPlaylistClass().getAnnotation(XmlRootElement.class) != null)
217 return binder.getPlaylist();
218 return binder.wrapPlaylist();
222 public void validate() throws ValidateException {