d32008cb77c6dd0feb20230f9fb3b6ab415fd7dd
[hespiff.git] / src / main / java / org / hedgecode / xml / xspf / bind / XSDTrackBinder.java
1 /*
2  * Copyright (c) 2015. 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.bind;
18
19 import java.math.BigInteger;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.hedgecode.xml.xspf.Extension;
24 import org.hedgecode.xml.xspf.Link;
25 import org.hedgecode.xml.xspf.Meta;
26 import org.hedgecode.xml.xspf.PlaylistFactory;
27 import org.hedgecode.xml.xspf.xsd.ExtensionType;
28 import org.hedgecode.xml.xspf.xsd.LinkType;
29 import org.hedgecode.xml.xspf.xsd.MetaType;
30 import org.hedgecode.xml.xspf.xsd.TrackType;
31
32 /**
33  *
34  *
35  * @author Dmitry Samoshin aka gotty
36  */
37 public class XSDTrackBinder extends AbstractXSDBinder implements TrackBinder {
38
39     private TrackType xsdTrack;
40
41     public XSDTrackBinder() {
42         this.xsdTrack = XSD_FACTORY.createTrackType();
43     }
44
45     public XSDTrackBinder(TrackType xsdTrack) {
46         this.xsdTrack = xsdTrack;
47     }
48
49     public Object getTrack() {
50         return xsdTrack;
51     }
52
53     public void setTrack(Object track) {
54         if (track != null && track instanceof TrackType)
55             this.xsdTrack = (TrackType) track;
56     }
57
58     @Override
59     public List<String> getLocations() {
60         return xsdTrack.getLocation();
61     }
62
63     @Override
64     public void addLocation(String location) {
65         if (location != null) {
66             xsdTrack.getLocation().add(
67                     location
68             );
69         }
70     }
71
72     @Override
73     public List<String> getIdentifiers() {
74         return xsdTrack.getIdentifier();
75     }
76
77     @Override
78     public void addIdentifier(String identifier) {
79         if (identifier != null) {
80             xsdTrack.getIdentifier().add(
81                     identifier
82             );
83         }
84     }
85
86     @Override
87     public String getTitle() {
88         return xsdTrack.getTitle();
89     }
90
91     @Override
92     public void setTitle(String title) {
93         xsdTrack.setTitle(title);
94     }
95
96     @Override
97     public String getCreator() {
98         return xsdTrack.getCreator();
99     }
100
101     @Override
102     public void setCreator(String creator) {
103         xsdTrack.setCreator(creator);
104     }
105
106     @Override
107     public String getAnnotation() {
108         return xsdTrack.getAnnotation();
109     }
110
111     @Override
112     public void setAnnotation(String annotation) {
113         xsdTrack.setAnnotation(annotation);
114     }
115
116     @Override
117     public String getInfo() {
118         return xsdTrack.getInfo();
119     }
120
121     @Override
122     public void setInfo(String info) {
123         xsdTrack.setInfo(info);
124     }
125
126     @Override
127     public String getImage() {
128         return xsdTrack.getImage();
129     }
130
131     @Override
132     public void setImage(String image) {
133         xsdTrack.setImage(image);
134     }
135
136     @Override
137     public String getAlbum() {
138         return xsdTrack.getAlbum();
139     }
140
141     @Override
142     public void setAlbum(String album) {
143         xsdTrack.setAlbum(album);
144     }
145
146     @Override
147     public Integer getTrackNum() {
148         BigInteger trackNum = xsdTrack.getTrackNum();
149         return trackNum != null ? trackNum.intValue() : null;
150     }
151
152     @Override
153     public void setTrackNum(Integer trackNum) {
154         xsdTrack.setTrackNum(
155                 trackNum != null ? BigInteger.valueOf(trackNum) : null
156         );
157     }
158
159     @Override
160     public Integer getDuration() {
161         BigInteger duration = xsdTrack.getDuration();
162         return duration != null ? duration.intValue() : null;
163     }
164
165     @Override
166     public void setDuration(Integer duration) {
167         xsdTrack.setDuration(
168                 duration != null ? BigInteger.valueOf(duration) : null
169         );
170     }
171
172     @Override
173     public List<Link> getLinks() {
174         List<Link> links = new ArrayList<>();
175         for (LinkType link : xsdTrack.getLink()) {
176             links.add(
177                     PlaylistFactory.createLink(link.getRel(), link.getValue())
178             );
179         }
180         return links;
181     }
182
183     @Override
184     public void addLink(Link link) {
185         if (link != null) {
186             LinkType linkType = XSD_FACTORY.createLinkType();
187             linkType.setRel(link.getRel());
188             linkType.setValue(link.getContent());
189             xsdTrack.getLink().add(
190                     linkType
191             );
192         }
193     }
194
195     @Override
196     public List<Meta> getMetas() {
197         List<Meta> metas = new ArrayList<>();
198         for (MetaType meta : xsdTrack.getMeta()) {
199             metas.add(
200                     PlaylistFactory.createMeta(meta.getRel(), meta.getValue())
201             );
202         }
203         return metas;
204     }
205
206     @Override
207     public void addMeta(Meta meta) {
208         if (meta != null) {
209             MetaType metaType = XSD_FACTORY.createMetaType();
210             metaType.setRel(meta.getRel());
211             metaType.setValue(meta.getContent());
212             xsdTrack.getMeta().add(
213                     metaType
214             );
215         }
216     }
217
218     @Override
219     public List<Extension> getExtensions() {
220         List<Extension> extensions = new ArrayList<>();
221         for (ExtensionType extension : xsdTrack.getExtension()) {
222             extensions.add(
223                     PlaylistFactory.createExtension(
224                             extension.getApplication(), extension.getContent()
225                     )
226             );
227         }
228         return extensions;
229     }
230
231     @Override
232     public void addExtension(Extension extension) {
233         if (extension != null) {
234             ExtensionType extensionType = XSD_FACTORY.createExtensionType();
235             extensionType.setApplication(extension.getApplication());
236             extensionType.getContent().addAll(extension.getContent());
237             xsdTrack.getExtension().add(
238                     extensionType
239             );
240         }
241     }
242
243 }