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.bind;
19 import java.math.BigInteger;
20 import java.util.ArrayList;
21 import java.util.List;
23 import org.hedgecode.xml.xspf.rng.Album;
24 import org.hedgecode.xml.xspf.rng.Duration;
25 import org.hedgecode.xml.xspf.rng.Identifier;
26 import org.hedgecode.xml.xspf.rng.Location;
27 import org.hedgecode.xml.xspf.rng.Track;
28 import org.hedgecode.xml.xspf.rng.TrackNum;
33 * @author Dmitry Samoshin aka gotty
35 public class RNGTrackBinder extends AbstractRNGBinder implements TrackBinder {
37 private Track rngTrack;
39 public RNGTrackBinder() {
40 this.rngTrack = RNG_FACTORY.createTrack();
43 public RNGTrackBinder(Track rngTrack) {
44 this.rngTrack = rngTrack;
48 protected Object getElement(Class elementClass) {
49 for (Object element : rngTrack.getLocationOrIdentifierOrTitle()) {
50 if (element.getClass().equals(elementClass))
57 protected List<Object> getElements(Class elementClass) {
58 List<Object> elements = new ArrayList<>();
59 for (Object element : rngTrack.getLocationOrIdentifierOrTitle()) {
60 if (element.getClass().equals(elementClass))
61 elements.add(element);
67 protected List<Object> getAllElements() {
68 return rngTrack.getLocationOrIdentifierOrTitle();
72 public List<String> getLocations() {
73 List<String> locations = new ArrayList<>();
74 for (Object element : getElements(Location.class)) {
75 Location locationElement = (Location) element;
77 locationElement.getValue()
84 public void addLocation(String location) {
85 if (location != null) {
86 Location locationElement = RNG_FACTORY.createLocation();
87 locationElement.setValue(location);
88 getAllElements().add(locationElement);
93 public List<String> getIdentifiers() {
94 List<String> identifiers = new ArrayList<>();
95 for (Object element : getElements(Identifier.class)) {
96 Identifier identifierElement = (Identifier) element;
98 identifierElement.getValue()
105 public void addIdentifier(String identifier) {
106 if (identifier != null) {
107 Identifier identifierElement = RNG_FACTORY.createIdentifier();
108 identifierElement.setValue(identifier);
109 getAllElements().add(identifierElement);
114 public String getAlbum() {
115 Album albumElement = (Album) getElement(Album.class);
116 return albumElement != null ? albumElement.getContent() : null;
120 public void setAlbum(String album) {
121 Album albumElement = (Album) getElement(Album.class);
122 if (albumElement != null)
123 getAllElements().remove(albumElement);
125 albumElement = RNG_FACTORY.createAlbum();
126 albumElement.setContent(album);
127 getAllElements().add(albumElement);
132 public Integer getTrackNum() {
133 TrackNum trackNumElement = (TrackNum) getElement(TrackNum.class);
134 return trackNumElement != null && trackNumElement.getValue() != null
135 ? trackNumElement.getValue().intValue()
140 public void setTrackNum(Integer trackNum) {
141 TrackNum trackNumElement = (TrackNum) getElement(TrackNum.class);
142 if (trackNumElement != null)
143 getAllElements().remove(trackNumElement);
144 if (trackNum != null) {
145 trackNumElement = RNG_FACTORY.createTrackNum();
146 trackNumElement.setValue(
147 BigInteger.valueOf(trackNum)
149 getAllElements().add(trackNumElement);
154 public Integer getDuration() {
155 Duration durationElement = (Duration) getElement(Duration.class);
156 return durationElement != null && durationElement.getValue() != null
157 ? durationElement.getValue().intValue()
162 public void setDuration(Integer duration) {
163 Duration durationElement = (Duration) getElement(Duration.class);
164 if (durationElement != null)
165 getAllElements().remove(durationElement);
166 if (duration != null) {
167 durationElement = RNG_FACTORY.createDuration();
168 durationElement.setValue(
169 BigInteger.valueOf(duration)
171 getAllElements().add(durationElement);