Changeset 34715 in osm for applications
- Timestamp:
- 2018-11-02T00:00:36+01:00 (6 years ago)
- Location:
- applications/viewer/jmapviewer
- Files:
-
- 2 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/tilesources/TemplatedTMSTileSource.java
r33422 r34715 34 34 private String[] randomParts; 35 35 private final Map<String, String> headers = new HashMap<>(); 36 private boolean inverse_zoom = false; 37 private int zoom_offset = 0; 36 38 37 39 // CHECKSTYLE.OFF: SingleSpaceSeparator 38 40 private static final String COOKIE_HEADER = "Cookie"; 39 private static final String PATTERN_ZOOM = "\\{(?:(\\d+)-)?z(?:oom)?([+-]\\d+)?\\}"; 40 private static final String PATTERN_X = "\\{x\\}"; 41 private static final String PATTERN_Y = "\\{y\\}"; 42 private static final String PATTERN_Y_YAHOO = "\\{!y\\}"; 43 private static final String PATTERN_NEG_Y = "\\{-y\\}"; 44 private static final String PATTERN_SWITCH = "\\{switch:([^}]+)\\}"; 45 private static final String PATTERN_HEADER = "\\{header\\(([^,]+),([^}]+)\\)\\}"; 41 private static final Pattern PATTERN_ZOOM = Pattern.compile("\\{(?:(\\d+)-)?z(?:oom)?([+-]\\d+)?\\}"); 42 private static final Pattern PATTERN_X = Pattern.compile("\\{x\\}"); 43 private static final Pattern PATTERN_Y = Pattern.compile("\\{y\\}"); 44 private static final Pattern PATTERN_Y_YAHOO = Pattern.compile("\\{!y\\}"); 45 private static final Pattern PATTERN_NEG_Y = Pattern.compile("\\{-y\\}"); 46 private static final Pattern PATTERN_SWITCH = Pattern.compile("\\{switch:([^}]+)\\}"); 47 private static final Pattern PATTERN_HEADER = Pattern.compile("\\{header\\(([^,]+),([^}]+)\\)\\}"); 48 private static final Pattern PATTERN_PARAM = Pattern.compile("\\{((?:\\d+-)?z(?:oom)?(:?[+-]\\d+)?|x|y|!y|-y|switch:([^}]+))\\}"); 46 49 // CHECKSTYLE.ON: SingleSpaceSeparator 47 50 48 private static final String[] ALL_PATTERNS = {51 private static final Pattern[] ALL_PATTERNS = { 49 52 PATTERN_HEADER, PATTERN_ZOOM, PATTERN_X, PATTERN_Y, PATTERN_Y_YAHOO, PATTERN_NEG_Y, PATTERN_SWITCH 50 53 }; … … 65 68 private void handleTemplate() { 66 69 // Capturing group pattern on switch values 67 Matcher m = P attern.compile(".*"+PATTERN_SWITCH+".*").matcher(baseUrl);68 if (m. matches()) {70 Matcher m = PATTERN_SWITCH.matcher(baseUrl); 71 if (m.find()) { 69 72 rand = new Random(); 70 73 randomParts = m.group(1).split(","); 71 74 } 72 Pattern pattern = Pattern.compile(PATTERN_HEADER);73 75 StringBuffer output = new StringBuffer(); 74 Matcher matcher = pattern.matcher(baseUrl);76 Matcher matcher = PATTERN_HEADER.matcher(baseUrl); 75 77 while (matcher.find()) { 76 78 headers.put(matcher.group(1), matcher.group(2)); … … 79 81 matcher.appendTail(output); 80 82 baseUrl = output.toString(); 83 m = PATTERN_ZOOM.matcher(this.baseUrl); 84 if (m.find()) { 85 if (m.group(1) != null) { 86 inverse_zoom = true; 87 zoom_offset = Integer.parseInt(m.group(1)); 88 } 89 if (m.group(2) != null) { 90 String ofs = m.group(2); 91 if (ofs.startsWith("+")) 92 ofs = ofs.substring(1); 93 zoom_offset += Integer.parseInt(ofs); 94 } 95 } 96 81 97 } 82 98 … … 88 104 @Override 89 105 public String getTileUrl(int zoom, int tilex, int tiley) { 90 int finalZoom = zoom; 91 Matcher m = Pattern.compile(".*"+PATTERN_ZOOM+".*").matcher(this.baseUrl); 92 if (m.matches()) { 93 if (m.group(1) != null) { 94 finalZoom = Integer.parseInt(m.group(1))-zoom; 106 StringBuffer url = new StringBuffer(baseUrl.length()); 107 Matcher matcher = PATTERN_PARAM.matcher(baseUrl); 108 while (matcher.find()) { 109 String replacement = "replace"; 110 switch (matcher.group(1)) { 111 case "z": // PATTERN_ZOOM 112 case "zoom": 113 replacement = Integer.toString((inverse_zoom ? -1 * zoom : zoom) + zoom_offset); 114 break; 115 case "x": // PATTERN_X 116 replacement = Integer.toString(tilex); 117 break; 118 case "y": // PATTERN_Y 119 replacement = Integer.toString(tiley); 120 break; 121 case "!y": // PATTERN_Y_YAHOO 122 replacement = Integer.toString((int) Math.pow(2, zoom-1)-1-tiley); 123 break; 124 case "-y": // PATTERN_NEG_Y 125 replacement = Integer.toString((int) Math.pow(2, zoom)-1-tiley); 126 break; 127 case "switch:": 128 replacement = randomParts[rand.nextInt(randomParts.length)]; 129 break; 130 default: 131 // handle switch/zoom here, as group will contain parameters and switch will not work 132 if (PATTERN_ZOOM.matcher("{" + matcher.group(1) + "}").matches()) { 133 replacement = Integer.toString((inverse_zoom ? -1 * zoom : zoom) + zoom_offset); 134 } else if (PATTERN_SWITCH.matcher("{" + matcher.group(1) + "}").matches()) { 135 replacement = randomParts[rand.nextInt(randomParts.length)]; 136 } else { 137 replacement = '{' + matcher.group(1) + '}'; 138 } 95 139 } 96 if (m.group(2) != null) { 97 String ofs = m.group(2); 98 if (ofs.startsWith("+")) 99 ofs = ofs.substring(1); 100 finalZoom += Integer.parseInt(ofs); 101 } 140 matcher.appendReplacement(url, replacement); 102 141 } 103 String r = this.baseUrl 104 .replaceAll(PATTERN_ZOOM, Integer.toString(finalZoom)) 105 .replaceAll(PATTERN_X, Integer.toString(tilex)) 106 .replaceAll(PATTERN_Y, Integer.toString(tiley)) 107 .replaceAll(PATTERN_Y_YAHOO, Integer.toString((int) Math.pow(2, zoom-1)-1-tiley)) 108 .replaceAll(PATTERN_NEG_Y, Integer.toString((int) Math.pow(2, zoom)-1-tiley)); 109 if (rand != null) { 110 r = r.replaceAll(PATTERN_SWITCH, randomParts[rand.nextInt(randomParts.length)]); 111 } 112 return r; 142 matcher.appendTail(url); 143 return url.toString().replace(" ", "%20"); 113 144 } 114 145 … … 122 153 while (m.find()) { 123 154 boolean isSupportedPattern = false; 124 for ( Stringpattern : ALL_PATTERNS) {125 if ( m.group().matches(pattern)) {155 for (Pattern pattern : ALL_PATTERNS) { 156 if (pattern.matcher(m.group()).matches()) { 126 157 isSupportedPattern = true; 127 158 break;
Note:
See TracChangeset
for help on using the changeset viewer.