All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opentripplanner.updater.bike_rental.GenericXmlBikeRentalDataSource Maven / Gradle / Ivy

There is a newer version: 2.5.0
Show newest version
package org.opentripplanner.updater.bike_rental;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.opentripplanner.routing.bike_rental.BikeRentalStation;
import org.opentripplanner.updater.UpdaterDataSourceParameters;
import org.opentripplanner.util.xml.XmlDataListDownloader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class GenericXmlBikeRentalDataSource implements BikeRentalDataSource {

    private static final Logger LOG = LoggerFactory.getLogger(GenericXmlBikeRentalDataSource.class);

    private final String url;

    List stations = new ArrayList<>();

    private final XmlDataListDownloader xmlDownloader;


    public GenericXmlBikeRentalDataSource(UpdaterDataSourceParameters config, String path) {
        url = config.getUrl();
        xmlDownloader = new XmlDataListDownloader<>();
        xmlDownloader.setPath(path);
        /* TODO Do not make this class abstract, but instead make the client
         * provide itself the factory?
         */
        xmlDownloader.setDataFactory(this::makeStation);
    }

    @Override
    public boolean update() {
        List newStations = xmlDownloader.download(url, false);
        if (newStations != null) {
            synchronized(this) {
                stations = newStations;
            }
            return true;
        }
        LOG.info("Can't update bike rental station list from: " + url + ", keeping current list.");
        return false;
    }

    @Override
    public synchronized List getStations() {
        return stations;
    }

    public void setReadAttributes(boolean readAttributes) {
        // if readAttributes is true, read XML attributes of selected elements, instead of children
        xmlDownloader.setReadAttributes(readAttributes);
    }

    public abstract BikeRentalStation makeStation(Map attributes);

    @Override
    public String toString() {
        return getClass().getName() + "(" + url + ")";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy