X-Git-Url: https://gitweb.fperrin.net/?a=blobdiff_plain;f=tim%2Fprune%2Fgui%2Fmap%2FMapSource.java;h=88ab85aa5e11eb4f45b22c4a0e4cc20ce7708a03;hb=a6197ddcaac11c0b943183da7d46169742d024af;hp=35590a6e9a44964179050986d7ce6dc7c8475386;hpb=f35b6d628f68e3b5ef19965ad8988d0dd1eb8efa;p=GpsPrune.git diff --git a/tim/prune/gui/map/MapSource.java b/tim/prune/gui/map/MapSource.java index 35590a6..88ab85a 100644 --- a/tim/prune/gui/map/MapSource.java +++ b/tim/prune/gui/map/MapSource.java @@ -2,6 +2,8 @@ package tim.prune.gui.map; import java.net.MalformedURLException; import java.net.URL; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * Class to represent any map source, whether an OsmMapSource @@ -11,6 +13,13 @@ import java.net.URL; */ public abstract class MapSource { + /** File extensions */ + protected String[] _extensions = null; + + /** Regular expression for catching server wildcards */ + protected static final Pattern WILD_PATTERN = Pattern.compile("^(.*)\\[(.*)\\](.*)$"); + + /** * @return the number of layers used in this source */ @@ -34,7 +43,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 +72,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); } /** @@ -75,9 +86,12 @@ public abstract class MapSource String urlstr = inUrl; // check prefix try { - new URL(urlstr); + new URL(urlstr.replace('[', 'w').replace(']', 'w')); + // There's a warning that this URL object isn't used, but it's enough to know the parse + // was successful without an exception being thrown. } - catch (MalformedURLException e) { + catch (MalformedURLException e) + { // fail if protocol specified if (urlstr.indexOf("://") >= 0) {return null;} // add the http protocol @@ -89,7 +103,7 @@ public abstract class MapSource } // Validate current url, return null if not ok try { - URL url = new URL(urlstr); + URL url = new URL(urlstr.replace('[', 'w').replace(']', 'w')); // url host must contain a dot if (url.getHost().indexOf('.') < 0) {return null;} } @@ -112,6 +126,17 @@ public abstract class MapSource int idx = url.indexOf("://"); if (idx >= 0) {url = url.substring(idx + 3);} if (url.startsWith("www.")) {url = url.substring(4);} + // Strip out any "[.*]" as well + if (url.indexOf('[') >= 0) + { + Matcher matcher = WILD_PATTERN.matcher(url); + if (matcher.matches()) { + url = matcher.group(1) + matcher.group(3); + if (url.length() > 1 && url.charAt(0) == '.') { + url = url.substring(1); + } + } + } return url; } @@ -121,16 +146,20 @@ 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() { StringBuilder sb = new StringBuilder(); - for (int i=0; i