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
*/
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
*/
/**
* @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
*/
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);
}
/**
{
if (inUrl == null || inUrl.equals("")) {return null;}
String urlstr = inUrl;
+ boolean urlOk = false;
+
// check prefix
- try {
- new URL(urlstr);
+ try
+ {
+ urlOk = new URL(urlstr.replace('[', 'w').replace(']', 'w')).toString() != null;
}
- catch (MalformedURLException e) {
+ catch (MalformedURLException e)
+ {
+ urlOk = false;
+ }
+
+ if (!urlOk)
+ {
// fail if protocol specified
if (urlstr.indexOf("://") >= 0) {return null;}
// add the http protocol
urlstr = urlstr + "/";
}
// Validate current url, return null if not ok
- try {
- URL url = new URL(urlstr);
+ try
+ {
+ URL url = new URL(urlstr.replace('[', 'w').replace(']', 'w'));
// url host must contain a dot
if (url.getHost().indexOf('.') < 0) {return null;}
}
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;
}
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<getNumLayers(); i++) {
+ for (int i=0; i<getNumLayers(); i++)
+ {
String url = getBaseUrl(i);
- if (url != null) {
+ if (url != null)
+ {
sb.append(url);
sb.append(';');
+ sb.append(getFileExtension(i));
+ sb.append(';');
}
}
return sb.toString();