1 package tim.prune.gui.map;
3 import java.net.MalformedURLException;
8 * Class to represent any map source, whether an OsmMapSource
9 * or one of the more complicated ones.
10 * Map sources may contain just one or several layers, and may
11 * build their URLs in different ways depending on the source
13 public abstract class MapSource
15 /** File extensions */
16 protected String[] _extensions = null;
20 * @return the number of layers used in this source
22 public abstract int getNumLayers();
25 * @return the name of the source
27 public abstract String getName();
30 * @return the base url for the specified layer
32 public abstract String getBaseUrl(int inLayerNum);
35 * @return the site name for the specified layer
37 public abstract String getSiteName(int inLayerNum);
40 * @return the file extension for the specified layer
42 public final String getFileExtension(int inLayerNum) {
43 return _extensions[inLayerNum];
47 * Make the URL to get the specified tile
48 * @param inLayerNum number of layer, from 0 (base) to getNumLayers-1 (top)
49 * @param inZoom zoom level
50 * @param inX x coordinate of tile
51 * @param inY y coordinate of tile
52 * @return URL as string
54 public abstract String makeURL(int inLayerNum, int inZoom, int inX, int inY);
57 * @return the maximum zoom level for this source
59 public abstract int getMaxZoomLevel();
62 * Make a relative file path from the base directory including site name
63 * @param inLayerNum layer number
64 * @param inZoom zoom level
65 * @param inX x coordinate
66 * @param inY y coordinate
67 * @return relative file path as String
69 public String makeFilePath(int inLayerNum, int inZoom, int inX, int inY)
71 return getSiteName(inLayerNum) + inZoom + "/" + inX + "/" + inY + "." + getFileExtension(inLayerNum);
75 * Checks the given url for having the right prefix and trailing slash
76 * @param inUrl url to check
77 * @return validated url with correct prefix and trailing slash, or null
79 public static String fixBaseUrl(String inUrl)
81 if (inUrl == null || inUrl.equals("")) {return null;}
82 String urlstr = inUrl;
83 boolean urlOk = false;
88 urlOk = new URL(urlstr.replace('[', 'w').replace(']', 'w')).toString() != null;
90 catch (MalformedURLException e)
97 // fail if protocol specified
98 if (urlstr.indexOf("://") >= 0) {return null;}
99 // add the http protocol
100 urlstr = "http://" + urlstr;
103 if (!urlstr.endsWith("/") && !urlstr.contains("?")) {
104 urlstr = urlstr + "/";
106 // Validate current url, return null if not ok
109 URL url = new URL(urlstr.replace('[', 'w').replace(']', 'w'));
110 // url host must contain a dot
111 if (url.getHost().indexOf('.') < 0) {return null;}
113 catch (MalformedURLException e) {
121 * @return string which can be written to the Config
123 public abstract String getConfigString();
126 * @return semicolon-separated list of base urls and extensions in order
128 public String getSiteStrings()
130 StringBuilder sb = new StringBuilder();
131 for (int i=0; i<getNumLayers(); i++)
133 String url = getBaseUrl(i);
138 sb.append(getFileExtension(i));
142 return sb.toString();