/* * Copyright (c) 2015-2019. Developed by Hedgecode. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.hedgecode.xml.xspf.bind; import java.math.BigInteger; import java.util.ArrayList; import java.util.List; import org.hedgecode.xml.xspf.rng.Album; import org.hedgecode.xml.xspf.rng.Duration; import org.hedgecode.xml.xspf.rng.Identifier; import org.hedgecode.xml.xspf.rng.Location; import org.hedgecode.xml.xspf.rng.Track; import org.hedgecode.xml.xspf.rng.TrackNum; /** * * * @author Dmitry Samoshin aka gotty */ public class RNGTrackBinder extends AbstractRNGBinder implements TrackBinder { private Track rngTrack; public RNGTrackBinder() { this.rngTrack = RNG_FACTORY.createTrack(); } public RNGTrackBinder(Track rngTrack) { this.rngTrack = rngTrack; } @Override protected Object getElement(Class elementClass) { for (Object element : rngTrack.getLocationOrIdentifierOrTitle()) { if (element.getClass().equals(elementClass)) return element; } return null; } @Override protected List getElements(Class elementClass) { List elements = new ArrayList<>(); for (Object element : rngTrack.getLocationOrIdentifierOrTitle()) { if (element.getClass().equals(elementClass)) elements.add(element); } return elements; } @Override protected List getAllElements() { return rngTrack.getLocationOrIdentifierOrTitle(); } @Override public List getLocations() { List locations = new ArrayList<>(); for (Object element : getElements(Location.class)) { Location locationElement = (Location) element; locations.add( locationElement.getValue() ); } return locations; } @Override public void addLocation(String location) { if (location != null) { Location locationElement = RNG_FACTORY.createLocation(); locationElement.setValue(location); getAllElements().add(locationElement); } } @Override public List getIdentifiers() { List identifiers = new ArrayList<>(); for (Object element : getElements(Identifier.class)) { Identifier identifierElement = (Identifier) element; identifiers.add( identifierElement.getValue() ); } return identifiers; } @Override public void addIdentifier(String identifier) { if (identifier != null) { Identifier identifierElement = RNG_FACTORY.createIdentifier(); identifierElement.setValue(identifier); getAllElements().add(identifierElement); } } @Override public String getAlbum() { Album albumElement = (Album) getElement(Album.class); return albumElement != null ? albumElement.getContent() : null; } @Override public void setAlbum(String album) { Album albumElement = (Album) getElement(Album.class); if (albumElement != null) getAllElements().remove(albumElement); if (album != null) { albumElement = RNG_FACTORY.createAlbum(); albumElement.setContent(album); getAllElements().add(albumElement); } } @Override public Integer getTrackNum() { TrackNum trackNumElement = (TrackNum) getElement(TrackNum.class); return trackNumElement != null && trackNumElement.getValue() != null ? trackNumElement.getValue().intValue() : null; } @Override public void setTrackNum(Integer trackNum) { TrackNum trackNumElement = (TrackNum) getElement(TrackNum.class); if (trackNumElement != null) getAllElements().remove(trackNumElement); if (trackNum != null) { trackNumElement = RNG_FACTORY.createTrackNum(); trackNumElement.setValue( BigInteger.valueOf(trackNum) ); getAllElements().add(trackNumElement); } } @Override public Integer getDuration() { Duration durationElement = (Duration) getElement(Duration.class); return durationElement != null && durationElement.getValue() != null ? durationElement.getValue().intValue() : null; } @Override public void setDuration(Integer duration) { Duration durationElement = (Duration) getElement(Duration.class); if (durationElement != null) getAllElements().remove(durationElement); if (duration != null) { durationElement = RNG_FACTORY.createDuration(); durationElement.setValue( BigInteger.valueOf(duration) ); getAllElements().add(durationElement); } } }