1 package tim.prune.gui.map;
3 import java.net.MalformedURLException;
7 * Class to represent any map source, whether an OsmMapSource
8 * or one of the more complicated ones.
9 * Map sources may contain just one or several layers, and may
10 * build their URLs in different ways depending on the source
12 public abstract class MapSource
14 /** File extensions */
15 protected String[] _extensions = null;
19 * @return the number of layers used in this source
21 public abstract int getNumLayers();
24 * @return the name of the source
26 public abstract String getName();
29 * @return the base url for the specified layer
31 public abstract String getBaseUrl(int inLayerNum);
34 * @return the site name for the specified layer
36 public abstract String getSiteName(int inLayerNum);
39 * @return the file extension for the specified layer
41 public final String getFileExtension(int inLayerNum) {
42 return _extensions[inLayerNum];
46 * Make the URL to get the specified tile
47 * @param inLayerNum number of layer, from 0 (base) to getNumLayers-1 (top)
48 * @param inZoom zoom level
49 * @param inX x coordinate of tile
50 * @param inY y coordinate of tile
51 * @return URL as string
53 public abstract String makeURL(int inLayerNum, int inZoom, int inX, int inY);
56 * @return the maximum zoom level for this source
58 public abstract int getMaxZoomLevel();
61 * Make a relative file path from the base directory including site name
62 * @param inLayerNum layer number
63 * @param inZoom zoom level
64 * @param inX x coordinate
65 * @param inY y coordinate
66 * @return relative file path as String
68 public String makeFilePath(int inLayerNum, int inZoom, int inX, int inY)
70 return getSiteName(inLayerNum) + inZoom + "/" + inX + "/" + inY + "." + getFileExtension(inLayerNum);
74 * Checks the given url for having the right prefix and trailing slash
75 * @param inUrl url to check
76 * @return validated url with correct prefix and trailing slash, or null
78 public static String fixBaseUrl(String inUrl)
80 if (inUrl == null || inUrl.equals("")) {return null;}
81 String urlstr = inUrl;
86 catch (MalformedURLException e) {
87 // fail if protocol specified
88 if (urlstr.indexOf("://") >= 0) {return null;}
89 // add the http protocol
90 urlstr = "http://" + urlstr;
93 if (!urlstr.endsWith("/")) {
94 urlstr = urlstr + "/";
96 // Validate current url, return null if not ok
98 URL url = new URL(urlstr);
99 // url host must contain a dot
100 if (url.getHost().indexOf('.') < 0) {return null;}
102 catch (MalformedURLException e) {
109 * Fix the site name by stripping off protocol and www.
110 * This is used to create the file path for disk caching
111 * @param inUrl url to strip
112 * @return stripped url
114 protected static String fixSiteName(String inUrl)
116 if (inUrl == null || inUrl.equals("")) {return null;}
117 String url = inUrl.toLowerCase();
118 int idx = url.indexOf("://");
119 if (idx >= 0) {url = url.substring(idx + 3);}
120 if (url.startsWith("www.")) {url = url.substring(4);}
125 * @return string which can be written to the Config
127 public abstract String getConfigString();
130 * @return semicolon-separated list of base urls and extensions in order
132 public String getSiteStrings()
134 StringBuilder sb = new StringBuilder();
135 for (int i=0; i<getNumLayers(); i++)
137 String url = getBaseUrl(i);
142 sb.append(getFileExtension(i));
146 return sb.toString();