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

org.opentripplanner.updater.GenericXmlDataSource Maven / Gradle / Ivy

package org.opentripplanner.updater;

import java.util.List;
import java.util.Map;
import org.opentripplanner.util.xml.XmlDataListDownloader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class GenericXmlDataSource implements DataSource {

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

  private String url;

  protected List updates = List.of();

  private final XmlDataListDownloader xmlDownloader;

  /**
   * Initialize GenericXmlDataSource.
   *
   * @param url URL to fetch XML file
   * @param xpath XPath to list elements in XML file
   */
  public GenericXmlDataSource(String url, String xpath) {
    this.url = url;
    this.xmlDownloader = new XmlDataListDownloader<>();
    this.xmlDownloader.setPath(xpath);
    this.xmlDownloader.setDataFactory(attributes -> parseElement(attributes));
  }

  /**
   * Fetch current data about given type and availability from this source.
   *
   * @return true if this operation may have changed something in the list of types.
   */
  @Override
  public boolean update() {
    List updates = xmlDownloader.download(url, false);
    if (updates != null) {
      synchronized (this) {
        this.updates = updates;
      }
      return true;
    }
    LOG.info("Can't update entities from: {}, keeping current list.", url);
    return false;
  }

  /**
   * @return a List of all currently known objects. The updater will use this to update the Graph.
   */
  @Override
  public List getUpdates() {
    return updates;
  }

  protected abstract T parseElement(Map attributes);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy