1 package tim.prune.gui.map;
3 import java.util.regex.Matcher;
4 import java.util.regex.Pattern;
7 * Helper functions for manipulating tile site names
9 public abstract class SiteNameUtils
11 /** Regular expression for catching server wildcards */
12 private static final Pattern WILD_PATTERN = Pattern.compile("^(.*)\\[(.*)\\](.*)$");
16 * If the base url contains something like [1234], then pick a server
17 * @param inBaseUrl base url
18 * @return modified base url
20 public static String pickServerUrl(String inBaseUrl)
22 if (inBaseUrl == null || inBaseUrl.indexOf('[') < 0) {
25 // Check for [.*] (once only)
26 // Only need to support one, make things a bit easier
27 final Matcher matcher = WILD_PATTERN.matcher(inBaseUrl);
28 // if not, return base url unchanged
29 if (!matcher.matches()) {
32 // if so, pick one at random and replace in the String
33 final String match = matcher.group(2);
34 final int numMatches = match.length();
38 int matchNum = (int) Math.floor(Math.random() * numMatches);
39 server = "" + match.charAt(matchNum);
41 final String result = matcher.group(1) + (server==null?"":server) + matcher.group(3);
47 * Fix the site name by stripping off protocol and www.
48 * This is used to create the file path for disk caching
49 * @param inUrl url to strip
50 * @return stripped url
52 public static String convertUrlToDirectory(String inUrl)
54 if (inUrl == null || inUrl.equals("")) {return null;}
55 String url = inUrl.toLowerCase();
56 int idx = url.indexOf("://");
57 if (idx >= 0) {url = url.substring(idx + 3);}
58 if (url.startsWith("www.")) {url = url.substring(4);}
59 // Strip out any "[.*]" as well
60 if (url.indexOf('[') >= 0)
62 Matcher matcher = WILD_PATTERN.matcher(url);
63 if (matcher.matches())
65 url = matcher.group(1) + matcher.group(3);
66 if (url.length() > 1 && url.charAt(0) == '.') {
67 url = url.substring(1);