]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/gui/map/MapSource.java
Version 13.4, May 2012
[GpsPrune.git] / tim / prune / gui / map / MapSource.java
index c2311ae186ac89a077d265be2fba3b35973787b6..e3ac2ed2227a1825d01526c87800c3abdd9819d4 100644 (file)
@@ -11,6 +11,10 @@ import java.net.URL;
  */
 public abstract class MapSource
 {
+       /** File extensions */
+       protected String[] _extensions = null;
+
+
        /**
         * @return the number of layers used in this source
         */
@@ -34,7 +38,9 @@ public abstract class MapSource
        /**
         * @return the file extension for the specified layer
         */
-       public abstract String getFileExtension(int inLayerNum);
+       public final String getFileExtension(int inLayerNum) {
+               return _extensions[inLayerNum];
+       }
 
        /**
         * Make the URL to get the specified tile
@@ -61,7 +67,7 @@ public abstract class MapSource
         */
        public String makeFilePath(int inLayerNum, int inZoom, int inX, int inY)
        {
-               return getSiteName(inLayerNum) + inZoom + "/" + inX + "/" + inY + getFileExtension(inLayerNum);
+               return getSiteName(inLayerNum) + inZoom + "/" + inX + "/" + inY + "." + getFileExtension(inLayerNum);
        }
 
        /**
@@ -69,23 +75,34 @@ public abstract class MapSource
         * @param inUrl url to check
         * @return validated url with correct prefix and trailing slash, or null
         */
-       protected static String fixBaseUrl(String inUrl)
+       public static String fixBaseUrl(String inUrl)
        {
                if (inUrl == null || inUrl.equals("")) {return null;}
-               String url = inUrl;
+               String urlstr = inUrl;
                // check prefix
                try {
-                       new URL(url);
+                       new URL(urlstr);
                }
                catch (MalformedURLException e) {
+                       // fail if protocol specified
+                       if (urlstr.indexOf("://") >= 0) {return null;}
                        // add the http protocol
-                       url = "http://" + url;
+                       urlstr = "http://" + urlstr;
                }
                // check trailing /
-               if (!url.endsWith("/")) {
-                       url = url + "/";
+               if (!urlstr.endsWith("/")) {
+                       urlstr = urlstr + "/";
                }
-               return url;
+               // Validate current url, return null if not ok
+               try {
+                       URL url = new URL(urlstr);
+                       // url host must contain a dot
+                       if (url.getHost().indexOf('.') < 0) {return null;}
+               }
+               catch (MalformedURLException e) {
+                       urlstr = null;
+               }
+               return urlstr;
        }
 
        /**
@@ -110,15 +127,22 @@ public abstract class MapSource
        public abstract String getConfigString();
 
        /**
-        * @return semicolon-separated list of base urls in order
+        * @return semicolon-separated list of base urls and extensions in order
         */
        public String getSiteStrings()
        {
-               String s = "";
-               for (int i=0; i<getNumLayers(); i++) {
+               StringBuilder sb = new StringBuilder();
+               for (int i=0; i<getNumLayers(); i++)
+               {
                        String url = getBaseUrl(i);
-                       if (url != null) {s = s + url + ";";}
+                       if (url != null)
+                       {
+                               sb.append(url);
+                               sb.append(';');
+                               sb.append(getFileExtension(i));
+                               sb.append(';');
+                       }
                }
-               return s;
+               return sb.toString();
        }
 }