]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/gui/map/SiteNameUtils.java
Version 20.4, May 2021
[GpsPrune.git] / src / tim / prune / gui / map / SiteNameUtils.java
1 package tim.prune.gui.map;
2
3 import java.util.regex.Matcher;
4 import java.util.regex.Pattern;
5
6 /**
7  * Helper functions for manipulating tile site names
8  */
9 public abstract class SiteNameUtils
10 {
11         /** Regular expression for catching server wildcards */
12         private static final Pattern WILD_PATTERN = Pattern.compile("^(.*)\\[(.*)\\](.*)$");
13
14
15         /**
16          * If the base url contains something like [1234], then pick a server
17          * @param inBaseUrl base url
18          * @return modified base url
19          */
20         public static String pickServerUrl(String inBaseUrl)
21         {
22                 if (inBaseUrl == null || inBaseUrl.indexOf('[') < 0) {
23                         return inBaseUrl;
24                 }
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()) {
30                         return inBaseUrl;
31                 }
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();
35                 String server = null;
36                 if (numMatches > 0)
37                 {
38                         int matchNum = (int) Math.floor(Math.random() * numMatches);
39                         server = "" + match.charAt(matchNum);
40                 }
41                 final String result = matcher.group(1) + (server==null?"":server) + matcher.group(3);
42                 return result;
43         }
44
45
46         /**
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
51          */
52         public static String convertUrlToDirectory(String inUrl)
53         {
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)
61                 {
62                         Matcher matcher = WILD_PATTERN.matcher(url);
63                         if (matcher.matches())
64                         {
65                                 url = matcher.group(1) + matcher.group(3);
66                                 if (url.length() > 1 && url.charAt(0) == '.') {
67                                         url = url.substring(1);
68                                 }
69                         }
70                 }
71                 return url;
72         }
73 }