]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/gui/map/MapSource.java
c4e294665e128def7d1fcbfb048cb516624d5d00
[GpsPrune.git] / src / tim / prune / gui / map / MapSource.java
1 package tim.prune.gui.map;
2
3 import java.net.MalformedURLException;
4 import java.net.URL;
5 import java.util.regex.Matcher;
6 import java.util.regex.Pattern;
7
8 /**
9  * Class to represent any map source, whether an OsmMapSource
10  * or one of the more complicated ones.
11  * Map sources may contain just one or several layers, and may
12  * build their URLs in different ways depending on the source
13  */
14 public abstract class MapSource
15 {
16         /** File extensions */
17         protected String[] _extensions = null;
18
19         /** Regular expression for catching server wildcards */
20         protected static final Pattern WILD_PATTERN = Pattern.compile("^(.*)\\[(.*)\\](.*)$");
21
22
23         /**
24          * @return the number of layers used in this source
25          */
26         public abstract int getNumLayers();
27
28         /**
29          * @return the name of the source
30          */
31         public abstract String getName();
32
33         /**
34          * @return the base url for the specified layer
35          */
36         public abstract String getBaseUrl(int inLayerNum);
37
38         /**
39          * @return the site name for the specified layer
40          */
41         public abstract String getSiteName(int inLayerNum);
42
43         /**
44          * @return the file extension for the specified layer
45          */
46         public final String getFileExtension(int inLayerNum) {
47                 return _extensions[inLayerNum];
48         }
49
50         /**
51          * Make the URL to get the specified tile
52          * @param inLayerNum number of layer, from 0 (base) to getNumLayers-1 (top)
53          * @param inZoom zoom level
54          * @param inX x coordinate of tile
55          * @param inY y coordinate of tile
56          * @return URL as string
57          */
58         public abstract String makeURL(int inLayerNum, int inZoom, int inX, int inY);
59
60         /**
61          * @return the maximum zoom level for this source
62          */
63         public abstract int getMaxZoomLevel();
64
65         /**
66          * Make a relative file path from the base directory including site name
67          * @param inLayerNum layer number
68          * @param inZoom zoom level
69          * @param inX x coordinate
70          * @param inY y coordinate
71          * @return relative file path as String
72          */
73         public String makeFilePath(int inLayerNum, int inZoom, int inX, int inY)
74         {
75                 return getSiteName(inLayerNum) + inZoom + "/" + inX + "/" + inY + "." + getFileExtension(inLayerNum);
76         }
77
78         /**
79          * Checks the given url for having the right prefix and trailing slash
80          * @param inUrl url to check
81          * @return validated url with correct prefix and trailing slash, or null
82          */
83         public static String fixBaseUrl(String inUrl)
84         {
85                 if (inUrl == null || inUrl.equals("")) {return null;}
86                 String urlstr = inUrl;
87                 boolean urlOk = false;
88
89                 // check prefix
90                 try
91                 {
92                         urlOk = new URL(urlstr.replace('[', 'w').replace(']', 'w')).toString() != null;
93                 }
94                 catch (MalformedURLException e)
95                 {
96                         urlOk = false;
97                 }
98
99                 if (!urlOk)
100                 {
101                         // fail if protocol specified
102                         if (urlstr.indexOf("://") >= 0) {return null;}
103                         // add the http protocol
104                         urlstr = "http://" + urlstr;
105                 }
106                 // check trailing /
107                 if (!urlstr.endsWith("/")) {
108                         urlstr = urlstr + "/";
109                 }
110                 // Validate current url, return null if not ok
111                 try
112                 {
113                         URL url = new URL(urlstr.replace('[', 'w').replace(']', 'w'));
114                         // url host must contain a dot
115                         if (url.getHost().indexOf('.') < 0) {return null;}
116                 }
117                 catch (MalformedURLException e) {
118                         urlstr = null;
119                 }
120                 return urlstr;
121         }
122
123         /**
124          * Fix the site name by stripping off protocol and www.
125          * This is used to create the file path for disk caching
126          * @param inUrl url to strip
127          * @return stripped url
128          */
129         protected static String fixSiteName(String inUrl)
130         {
131                 if (inUrl == null || inUrl.equals("")) {return null;}
132                 String url = inUrl.toLowerCase();
133                 int idx = url.indexOf("://");
134                 if (idx >= 0) {url = url.substring(idx + 3);}
135                 if (url.startsWith("www.")) {url = url.substring(4);}
136                 // Strip out any "[.*]" as well
137                 if (url.indexOf('[') >= 0)
138                 {
139                         Matcher matcher = WILD_PATTERN.matcher(url);
140                         if (matcher.matches()) {
141                                 url = matcher.group(1) + matcher.group(3);
142                                 if (url.length() > 1 && url.charAt(0) == '.') {
143                                         url = url.substring(1);
144                                 }
145                         }
146                 }
147                 return url;
148         }
149
150         /**
151          * @return string which can be written to the Config
152          */
153         public abstract String getConfigString();
154
155         /**
156          * @return semicolon-separated list of base urls and extensions in order
157          */
158         public String getSiteStrings()
159         {
160                 StringBuilder sb = new StringBuilder();
161                 for (int i=0; i<getNumLayers(); i++)
162                 {
163                         String url = getBaseUrl(i);
164                         if (url != null)
165                         {
166                                 sb.append(url);
167                                 sb.append(';');
168                                 sb.append(getFileExtension(i));
169                                 sb.append(';');
170                         }
171                 }
172                 return sb.toString();
173         }
174 }