source: osm/applications/editors/josm/plugins/imagery_offset_db/src/iodb/ImageryIdGenerator.java@ 34370

Last change on this file since 34370 was 34370, checked in by donvip, 6 years ago

fix #16438 - StringIndexOutOfBoundsException

File size: 3.2 KB
Line 
1// License: WTFPL. For details, see LICENSE file.
2package iodb;
3
4import java.util.Arrays;
5import java.util.Map;
6import java.util.Set;
7import java.util.TreeMap;
8import java.util.TreeSet;
9
10import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
11
12/**
13 * Generate unique imagery identifier based on its type and URL.
14 *
15 * @author Zverik
16 * @license WTFPL
17 */
18public final class ImageryIdGenerator {
19
20 private ImageryIdGenerator() {
21 // Hide default constructor for utilities classes
22 }
23
24 public static String getImageryID(String url, ImageryType type) {
25 if (url == null)
26 return null;
27
28 // predefined layers
29 if (ImageryType.BING.equals(type) || url.contains("tiles.virtualearth.net"))
30 return "bing";
31
32 if (ImageryType.SCANEX.equals(type) && url.toLowerCase().equals("irs"))
33 return "scanex_irs";
34
35 if (ImageryType.TMS.equals(type) && url.toLowerCase().matches(".+tiles\\.mapbox\\.com/v[3-9]/openstreetmap\\.map.*"))
36 return "mapbox";
37
38 boolean isWMS = ImageryType.WMS.equals(type);
39
40 // System.out.println(url);
41
42 // Remove protocol
43 int i = url.indexOf("://");
44 if (i > 0) {
45 url = url.substring(i + 3);
46 }
47
48 // Split URL into address and query string
49 i = url.indexOf('?');
50 String query = "";
51 if (i > 0) {
52 query = url.substring(i);
53 url = url.substring(0, i);
54 }
55
56 // Parse query parameters into a sorted map
57 final Set<String> removeWMSParams = new TreeSet<>(Arrays.asList(new String[] {
58 "srs", "width", "height", "bbox", "service", "request", "version", "format", "styles", "transparent"
59 }));
60 Map<String, String> qparams = new TreeMap<>();
61 String[] qparamsStr = query.length() > 1 ? query.substring(1).split("&") : new String[0];
62 for (String param : qparamsStr) {
63 String[] kv = param.split("=");
64 kv[0] = kv[0].toLowerCase();
65 // WMS: if this is WMS, remove all parameters except map and layers
66 if (isWMS && removeWMSParams.contains(kv[0]))
67 continue;
68 // TMS: skip parameters with variable values and Mapbox's access token
69 if ((kv.length > 1 && kv[1].indexOf('{') >= 0 && kv[1].indexOf('}') > 0) || kv[0].equals("access_token"))
70 continue;
71 qparams.put(kv[0].toLowerCase(), kv.length > 1 ? kv[1] : null);
72 }
73
74 // Reconstruct query parameters
75 StringBuilder sb = new StringBuilder();
76 for (String qk : qparams.keySet()) {
77 if (sb.length() > 0)
78 sb.append('&');
79 else if (query.length() > 0)
80 sb.append('?');
81 sb.append(qk).append('=').append(qparams.get(qk));
82 }
83 query = sb.toString();
84
85 // TMS: remove /{zoom} and /{y}.png parts
86 url = url.replaceAll("\\/\\{[^}]+\\}(?:\\.\\w+)?", "");
87 // TMS: remove variable parts
88 url = url.replaceAll("\\{[^}]+\\}", "");
89 while (url.contains("..")) {
90 url = url.replace("..", ".");
91 }
92 if (url.startsWith("."))
93 url = url.substring(1);
94
95 return url + query;
96 }
97}
Note: See TracBrowser for help on using the repository browser.