001package org.gbif.utils.rs;
002
003import java.net.MalformedURLException;
004import java.net.URL;
005
006import com.google.common.base.Joiner;
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010/**
011 * Utility class knowing the url layout of rs.gbif.org to access authority and dictionary files.
012 */
013public class RsGbifOrg {
014  private static final Logger LOG = LoggerFactory.getLogger(RsGbifOrg.class);
015  private static final Joiner PATH_JOINER = Joiner.on("/").skipNulls();
016  public static final String DOMAIN = "http://rs.gbif.org/";
017  public static final String FILENAME_BLACKLIST = "blacklisted.txt";
018  public static final String FILENAME_SUPRAGENERIC = "suprageneric.txt";
019  public static final String FILENAME_EPITHETA = "epitheta.txt";
020  public static final String FILENAME_EPITHETA_AMIGOUS = "epitheta_ambigous.txt";
021  public static final String FILENAME_AUTHORS = "authors.txt";
022  public static final String FILENAME_GENERA = "genera.txt";
023  public static final String FILENAME_GENERA_AMIGOUS = "genera_ambigous.txt";
024
025  /**
026   * @param path given as array of individual names that will be concatenated
027   * @return url to file inside rs.gbif.org
028   */
029  public static URL url(String ... path) {
030    try {
031      if (path == null){
032        return new URL(DOMAIN);
033      }
034      return new URL(DOMAIN + PATH_JOINER.join(path));
035    } catch (MalformedURLException e) {
036      LOG.error("Cannot create rs.gbif.org url for path " + PATH_JOINER.join(path), e);
037    }
038    return null;
039  }
040
041  /**
042  * @param filename of dictionary file requested
043  * @return url to file inside to dictionary folder of rs.gbif.org
044  */
045  public static URL dictionaryUrl(String filename) {
046    return url("dictionaries", filename);
047  }
048
049  /**
050   * @param filename of authority dictionary file requested
051   * @return url to file inside to authority folder of rs.gbif.org
052   */
053  public static URL authorityUrl(String filename) {
054    return url("dictionaries", "authority", filename);
055  }
056
057  /**
058   * @param filename of synonyms file requested
059   * @return url to file inside to synonyms dictionary folder of rs.gbif.org
060   */
061  public static URL synonymUrl(String filename) {
062    return url("dictionaries", "synonyms", filename);
063  }
064}