X-Git-Url: https://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=src%2Ftim%2Fprune%2Fgui%2Fmap%2FSiteNameUtils.java;fp=src%2Ftim%2Fprune%2Fgui%2Fmap%2FSiteNameUtils.java;h=cf53f728198854878753dd88a1fc8f5f7f13e4e8;hp=0000000000000000000000000000000000000000;hb=cd5dd0c207b676067e85e0885b90f05445b7e229;hpb=1db53356139320890a8d10e982865a1899e11b81 diff --git a/src/tim/prune/gui/map/SiteNameUtils.java b/src/tim/prune/gui/map/SiteNameUtils.java new file mode 100644 index 0000000..cf53f72 --- /dev/null +++ b/src/tim/prune/gui/map/SiteNameUtils.java @@ -0,0 +1,73 @@ +package tim.prune.gui.map; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Helper functions for manipulating tile site names + */ +public abstract class SiteNameUtils +{ + /** Regular expression for catching server wildcards */ + private static final Pattern WILD_PATTERN = Pattern.compile("^(.*)\\[(.*)\\](.*)$"); + + + /** + * If the base url contains something like [1234], then pick a server + * @param inBaseUrl base url + * @return modified base url + */ + public static String pickServerUrl(String inBaseUrl) + { + if (inBaseUrl == null || inBaseUrl.indexOf('[') < 0) { + return inBaseUrl; + } + // Check for [.*] (once only) + // Only need to support one, make things a bit easier + final Matcher matcher = WILD_PATTERN.matcher(inBaseUrl); + // if not, return base url unchanged + if (!matcher.matches()) { + return inBaseUrl; + } + // if so, pick one at random and replace in the String + final String match = matcher.group(2); + final int numMatches = match.length(); + String server = null; + if (numMatches > 0) + { + int matchNum = (int) Math.floor(Math.random() * numMatches); + server = "" + match.charAt(matchNum); + } + final String result = matcher.group(1) + (server==null?"":server) + matcher.group(3); + return result; + } + + + /** + * Fix the site name by stripping off protocol and www. + * This is used to create the file path for disk caching + * @param inUrl url to strip + * @return stripped url + */ + public static String convertUrlToDirectory(String inUrl) + { + if (inUrl == null || inUrl.equals("")) {return null;} + String url = inUrl.toLowerCase(); + int idx = url.indexOf("://"); + if (idx >= 0) {url = url.substring(idx + 3);} + if (url.startsWith("www.")) {url = url.substring(4);} + // Strip out any "[.*]" as well + if (url.indexOf('[') >= 0) + { + Matcher matcher = WILD_PATTERN.matcher(url); + if (matcher.matches()) + { + url = matcher.group(1) + matcher.group(3); + if (url.length() > 1 && url.charAt(0) == '.') { + url = url.substring(1); + } + } + } + return url; + } +}