1 package tim.prune.gui.map;
3 import java.net.MalformedURLException;
5 import java.util.regex.Matcher;
6 import java.util.regex.Pattern;
9 * Class to represent any map source, whether an OsmMapSource
10 * or one of the more complicated ones.
11 * Map sources may contain just one or several layers, and may
12 * build their URLs in different ways depending on the source
14 public abstract class MapSource
16 /** File extensions */
17 protected String[] _extensions = null;
19 /** Regular expression for catching server wildcards */
20 protected static final Pattern WILD_PATTERN = Pattern.compile("^(.*)\\[(.*)\\](.*)$");
24 * @return the number of layers used in this source
26 public abstract int getNumLayers();
29 * @return the name of the source
31 public abstract String getName();
34 * @return the base url for the specified layer
36 public abstract String getBaseUrl(int inLayerNum);
39 * @return the site name for the specified layer
41 public abstract String getSiteName(int inLayerNum);
44 * @return the file extension for the specified layer
46 public final String getFileExtension(int inLayerNum) {
47 return _extensions[inLayerNum];
51 * Make the URL to get the specified tile
52 * @param inLayerNum number of layer, from 0 (base) to getNumLayers-1 (top)
53 * @param inZoom zoom level
54 * @param inX x coordinate of tile
55 * @param inY y coordinate of tile
56 * @return URL as string
58 public abstract String makeURL(int inLayerNum, int inZoom, int inX, int inY);
61 * @return the maximum zoom level for this source
63 public abstract int getMaxZoomLevel();
66 * Make a relative file path from the base directory including site name
67 * @param inLayerNum layer number
68 * @param inZoom zoom level
69 * @param inX x coordinate
70 * @param inY y coordinate
71 * @return relative file path as String
73 public String makeFilePath(int inLayerNum, int inZoom, int inX, int inY)
75 return getSiteName(inLayerNum) + inZoom + "/" + inX + "/" + inY + "." + getFileExtension(inLayerNum);
79 * Checks the given url for having the right prefix and trailing slash
80 * @param inUrl url to check
81 * @return validated url with correct prefix and trailing slash, or null
83 public static String fixBaseUrl(String inUrl)
85 if (inUrl == null || inUrl.equals("")) {return null;}
86 String urlstr = inUrl;
89 new URL(urlstr.replace('[', 'w').replace(']', 'w'));
90 // There's a warning that this URL object isn't used, but it's enough to know the parse
91 // was successful without an exception being thrown.
93 catch (MalformedURLException e)
95 // fail if protocol specified
96 if (urlstr.indexOf("://") >= 0) {return null;}
97 // add the http protocol
98 urlstr = "http://" + urlstr;
101 if (!urlstr.endsWith("/")) {
102 urlstr = urlstr + "/";
104 // Validate current url, return null if not ok
106 URL url = new URL(urlstr.replace('[', 'w').replace(']', 'w'));
107 // url host must contain a dot
108 if (url.getHost().indexOf('.') < 0) {return null;}
110 catch (MalformedURLException e) {
117 * Fix the site name by stripping off protocol and www.
118 * This is used to create the file path for disk caching
119 * @param inUrl url to strip
120 * @return stripped url
122 protected static String fixSiteName(String inUrl)
124 if (inUrl == null || inUrl.equals("")) {return null;}
125 String url = inUrl.toLowerCase();
126 int idx = url.indexOf("://");
127 if (idx >= 0) {url = url.substring(idx + 3);}
128 if (url.startsWith("www.")) {url = url.substring(4);}
129 // Strip out any "[.*]" as well
130 if (url.indexOf('[') >= 0)
132 Matcher matcher = WILD_PATTERN.matcher(url);
133 if (matcher.matches()) {
134 url = matcher.group(1) + matcher.group(3);
135 if (url.length() > 1 && url.charAt(0) == '.') {
136 url = url.substring(1);
144 * @return string which can be written to the Config
146 public abstract String getConfigString();
149 * @return semicolon-separated list of base urls and extensions in order
151 public String getSiteStrings()
153 StringBuilder sb = new StringBuilder();
154 for (int i=0; i<getNumLayers(); i++)
156 String url = getBaseUrl(i);
161 sb.append(getFileExtension(i));
165 return sb.toString();