- Timestamp:
- 2021-03-17T15:07:13+01:00 (4 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/data/imagery
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/imagery/TemplatedWMSTileSource.java
r17502 r17578 2 2 package org.openstreetmap.josm.data.imagery; 3 3 4 import static org.openstreetmap.josm. tools.I18n.tr;4 import static org.openstreetmap.josm.data.imagery.ImageryPatterns.PATTERN_PARAM; 5 5 6 6 import java.text.DecimalFormat; 7 7 import java.text.DecimalFormatSymbols; 8 8 import java.text.NumberFormat; 9 import java.util.Arrays;10 9 import java.util.Locale; 11 10 import java.util.Map; … … 14 13 import java.util.concurrent.ConcurrentHashMap; 15 14 import java.util.regex.Matcher; 16 import java.util.regex.Pattern;17 15 18 16 import org.openstreetmap.gui.jmapviewer.interfaces.TemplatedTileSource; … … 20 18 import org.openstreetmap.josm.data.projection.Projection; 21 19 import org.openstreetmap.josm.gui.layer.WMSLayer; 22 import org.openstreetmap.josm.tools.CheckParameterUtil;23 20 import org.openstreetmap.josm.tools.Utils; 24 21 … … 30 27 */ 31 28 public class TemplatedWMSTileSource extends AbstractWMSTileSource implements TemplatedTileSource { 32 // CHECKSTYLE.OFF: SingleSpaceSeparator33 private static final Pattern PATTERN_HEADER = Pattern.compile("\\{header\\(([^,]+),([^}]+)\\)\\}");34 private static final Pattern PATTERN_PROJ = Pattern.compile("\\{proj\\}");35 private static final Pattern PATTERN_WKID = Pattern.compile("\\{wkid\\}");36 private static final Pattern PATTERN_BBOX = Pattern.compile("\\{bbox\\}");37 private static final Pattern PATTERN_W = Pattern.compile("\\{w\\}");38 private static final Pattern PATTERN_S = Pattern.compile("\\{s\\}");39 private static final Pattern PATTERN_E = Pattern.compile("\\{e\\}");40 private static final Pattern PATTERN_N = Pattern.compile("\\{n\\}");41 private static final Pattern PATTERN_WIDTH = Pattern.compile("\\{width\\}");42 private static final Pattern PATTERN_HEIGHT = Pattern.compile("\\{height\\}");43 private static final Pattern PATTERN_TIME = Pattern.compile("\\{time\\}"); // Sentinel-244 private static final Pattern PATTERN_PARAM = Pattern.compile("\\{([^}]+)\\}");45 // CHECKSTYLE.ON: SingleSpaceSeparator46 29 47 30 private static final NumberFormat LATLON_FORMAT = new DecimalFormat("###0.0000000", new DecimalFormatSymbols(Locale.US)); 48 49 private static final Pattern[] ALL_PATTERNS = {50 PATTERN_HEADER, PATTERN_PROJ, PATTERN_WKID, PATTERN_BBOX,51 PATTERN_W, PATTERN_S, PATTERN_E, PATTERN_N,52 PATTERN_WIDTH, PATTERN_HEIGHT, PATTERN_TIME,53 };54 31 55 32 private final Set<String> serverProjections; … … 68 45 this.headers.putAll(info.getCustomHttpHeaders()); 69 46 this.date = info.getDate(); 70 handleTemplate();47 this.baseUrl = ImageryPatterns.handleHeaderTemplate(baseUrl, headers); 71 48 initProjection(); 72 49 // Bounding box coordinates have to be switched for WMS 1.3.0 EPSG:4326. … … 170 147 */ 171 148 public static void checkUrl(String url) { 172 CheckParameterUtil.ensureParameterNotNull(url, "url"); 173 Matcher m = PATTERN_PARAM.matcher(url); 174 while (m.find()) { 175 boolean isSupportedPattern = Arrays.stream(ALL_PATTERNS) 176 .anyMatch(pattern -> pattern.matcher(m.group()).matches()); 177 if (!isSupportedPattern) { 178 throw new IllegalArgumentException( 179 tr("{0} is not a valid WMS argument. Please check this server URL:\n{1}", m.group(), url)); 180 } 181 } 182 } 183 184 private void handleTemplate() { 185 // Capturing group pattern on switch values 186 StringBuffer output = new StringBuffer(); 187 Matcher matcher = PATTERN_HEADER.matcher(this.baseUrl); 188 while (matcher.find()) { 189 headers.put(matcher.group(1), matcher.group(2)); 190 matcher.appendReplacement(output, ""); 191 } 192 matcher.appendTail(output); 193 this.baseUrl = output.toString(); 149 ImageryPatterns.checkWmsUrlPatterns(url); 194 150 } 195 151 } -
trunk/src/org/openstreetmap/josm/data/imagery/WMTSTileSource.java
r16643 r17578 2 2 package org.openstreetmap.josm.data.imagery; 3 3 4 import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; 5 import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; 4 6 import static org.openstreetmap.josm.data.imagery.GetCapabilitiesParseHelper.QN_OWS_DCP; 5 7 import static org.openstreetmap.josm.data.imagery.GetCapabilitiesParseHelper.QN_OWS_GET; … … 37 39 import java.util.concurrent.ConcurrentHashMap; 38 40 import java.util.function.BiFunction; 39 import java.util.regex.Matcher;40 import java.util.regex.Pattern;41 41 import java.util.stream.Collectors; 42 42 … … 107 107 // CHECKSTYLE.ON: SingleSpaceSeparator 108 108 109 private static final String PATTERN_HEADER = "\\{header\\(([^,]+),([^}]+)\\)\\}";110 111 109 private static final String URL_GET_ENCODING_PARAMS = "SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER={layer}&STYLE={style}&" 112 110 + "FORMAT={format}&tileMatrixSet={TileMatrixSet}&tileMatrix={TileMatrix}&tileRow={TileRow}&tileCol={TileCol}"; 113 114 private static final String[] ALL_PATTERNS = {115 PATTERN_HEADER,116 };117 111 118 112 private int cachedTileSize = -1; … … 140 134 141 135 /** 142 *143 136 * class representing WMTS TileMatrixSet 144 137 * This connects projection and TileMatrix (how the map is divided in tiles) 145 * 138 * @since 13733 146 139 */ 147 140 public static class TileMatrixSet { … … 208 201 /** 209 202 * Class representing WMTS Layer information 210 * 203 * @since 13733 211 204 */ 212 205 public static class Layer { … … 298 291 /** 299 292 * Exception thrown when parser doesn't find expected information in GetCapabilities document 300 * 293 * @since 13733 301 294 */ 302 295 public static class WMTSGetCapabilitiesException extends Exception { … … 368 361 CheckParameterUtil.ensureThat(info.getDefaultLayers().size() < 2, "At most 1 default layer for WMTS is supported"); 369 362 this.headers.putAll(info.getCustomHttpHeaders()); 370 this.baseUrl = GetCapabilitiesParseHelper.normalizeCapabilitiesUrl( handleTemplate(info.getUrl()));363 this.baseUrl = GetCapabilitiesParseHelper.normalizeCapabilitiesUrl(ImageryPatterns.handleHeaderTemplate(info.getUrl(), headers)); 371 364 WMTSCapabilities capabilities = getCapabilities(baseUrl, headers); 372 365 this.layers = capabilities.getLayers(); … … 444 437 } 445 438 446 private String handleTemplate(String url) {447 Pattern pattern = Pattern.compile(PATTERN_HEADER);448 StringBuffer output = new StringBuffer();449 Matcher matcher = pattern.matcher(url);450 while (matcher.find()) {451 this.headers.put(matcher.group(1), matcher.group(2));452 matcher.appendReplacement(output, "");453 }454 matcher.appendTail(output);455 return output.toString();456 }457 458 439 /** 459 440 * Call remote server and parse response to WMTSCapabilities object … … 482 463 Collection<Layer> layers = null; 483 464 for (int event = reader.getEventType(); reader.hasNext(); event = reader.next()) { 484 if (event == XMLStreamReader.START_ELEMENT) {465 if (event == START_ELEMENT) { 485 466 QName qName = reader.getName(); 486 467 if (QN_OWS_OPERATIONS_METADATA.equals(qName)) { … … 528 509 Collection<Layer> layers = new ArrayList<>(); 529 510 for (int event = reader.getEventType(); 530 reader.hasNext() && !(event == XMLStreamReader.END_ELEMENT && QN_CONTENTS.equals(reader.getName()));511 reader.hasNext() && !(event == END_ELEMENT && QN_CONTENTS.equals(reader.getName())); 531 512 event = reader.next()) { 532 if (event == XMLStreamReader.START_ELEMENT) {513 if (event == START_ELEMENT) { 533 514 QName qName = reader.getName(); 534 515 if (QN_LAYER.equals(qName)) { … … 574 555 575 556 for (int event = reader.getEventType(); 576 reader.hasNext() && !(event == XMLStreamReader.END_ELEMENT && QN_LAYER.equals(reader.getName()));557 reader.hasNext() && !(event == END_ELEMENT && QN_LAYER.equals(reader.getName())); 577 558 event = reader.next()) { 578 if (event == XMLStreamReader.START_ELEMENT) {559 if (event == START_ELEMENT) { 579 560 QName qName = reader.getName(); 580 561 tagStack.push(qName); … … 612 593 } 613 594 // need to get event type from reader, as parsing might have change position of reader 614 if (reader.getEventType() == XMLStreamReader.END_ELEMENT) {595 if (reader.getEventType() == END_ELEMENT) { 615 596 QName start = tagStack.pop(); 616 597 if (!start.equals(reader.getName())) { … … 643 624 Dimension ret = new Dimension(); 644 625 for (int event = reader.getEventType(); 645 reader.hasNext() && !(event == XMLStreamReader.END_ELEMENT && 646 QN_DIMENSION.equals(reader.getName())); 626 reader.hasNext() && !(event == END_ELEMENT && QN_DIMENSION.equals(reader.getName())); 647 627 event = reader.next()) { 648 if (event == XMLStreamReader.START_ELEMENT) {628 if (event == START_ELEMENT) { 649 629 QName qName = reader.getName(); 650 630 if (QN_OWS_IDENTIFIER.equals(qName)) { … … 670 650 String ret = null; 671 651 for (int event = reader.getEventType(); 672 reader.hasNext() && !(event == XMLStreamReader.END_ELEMENT && 673 QN_TILEMATRIX_SET_LINK.equals(reader.getName())); 652 reader.hasNext() && !(event == END_ELEMENT && QN_TILEMATRIX_SET_LINK.equals(reader.getName())); 674 653 event = reader.next()) { 675 if (event == XMLStreamReader.START_ELEMENT && QN_TILEMATRIXSET.equals(reader.getName())) {654 if (event == START_ELEMENT && QN_TILEMATRIXSET.equals(reader.getName())) { 676 655 ret = reader.getElementText(); 677 656 } … … 689 668 TileMatrixSetBuilder matrixSet = new TileMatrixSetBuilder(); 690 669 for (int event = reader.getEventType(); 691 reader.hasNext() && !(event == XMLStreamReader.END_ELEMENT && QN_TILEMATRIXSET.equals(reader.getName()));670 reader.hasNext() && !(event == END_ELEMENT && QN_TILEMATRIXSET.equals(reader.getName())); 692 671 event = reader.next()) { 693 if (event == XMLStreamReader.START_ELEMENT) {672 if (event == START_ELEMENT) { 694 673 QName qName = reader.getName(); 695 674 if (QN_OWS_IDENTIFIER.equals(qName)) { … … 717 696 TileMatrix ret = new TileMatrix(); 718 697 for (int event = reader.getEventType(); 719 reader.hasNext() && !(event == XMLStreamReader.END_ELEMENT && QN_TILEMATRIX.equals(reader.getName()));698 reader.hasNext() && !(event == END_ELEMENT && QN_TILEMATRIX.equals(reader.getName())); 720 699 event = reader.next()) { 721 if (event == XMLStreamReader.START_ELEMENT) {700 if (event == START_ELEMENT) { 722 701 QName qName = reader.getName(); 723 702 if (QN_OWS_IDENTIFIER.equals(qName)) { … … 772 751 LatLon upperCorner = null; 773 752 for (int event = reader.getEventType(); 774 reader.hasNext() && !(event == XMLStreamReader.END_ELEMENT && 775 QN_OWS_WGS84_BOUNDING_BOX.equals(reader.getName())); 753 reader.hasNext() && !(event == END_ELEMENT && QN_OWS_WGS84_BOUNDING_BOX.equals(reader.getName())); 776 754 event = reader.next()) { 777 if (event == XMLStreamReader.START_ELEMENT) {755 if (event == START_ELEMENT) { 778 756 QName qName = reader.getName(); 779 757 if (QN_OWS_LOWER_CORNER.equals(qName)) { … … 800 778 private static WMTSCapabilities parseOperationMetadata(XMLStreamReader reader) throws XMLStreamException { 801 779 for (int event = reader.getEventType(); 802 reader.hasNext() && !(event == XMLStreamReader.END_ELEMENT && 803 QN_OWS_OPERATIONS_METADATA.equals(reader.getName())); 780 reader.hasNext() && !(event == END_ELEMENT && QN_OWS_OPERATIONS_METADATA.equals(reader.getName())); 804 781 event = reader.next()) { 805 if (event == XMLStreamReader.START_ELEMENT &&782 if (event == START_ELEMENT && 806 783 QN_OWS_OPERATION.equals(reader.getName()) && 807 784 "GetTile".equals(reader.getAttributeValue("", "name")) && … … 909 886 } 910 887 911 url = url.replace All("\\{layer\\}", this.currentLayer.identifier)912 .replace All("\\{format\\}", this.currentLayer.format)913 .replace All("\\{TileMatrixSet\\}", this.currentTileMatrixSet.identifier)914 .replace All("\\{TileMatrix\\}", tileMatrix.identifier)915 .replace All("\\{TileRow\\}", Integer.toString(tiley))916 .replace All("\\{TileCol\\}", Integer.toString(tilex))888 url = url.replace("{layer}", this.currentLayer.identifier) 889 .replace("{format}", this.currentLayer.format) 890 .replace("{TileMatrixSet}", this.currentTileMatrixSet.identifier) 891 .replace("{TileMatrix}", tileMatrix.identifier) 892 .replace("{TileRow}", Integer.toString(tiley)) 893 .replace("{TileCol}", Integer.toString(tilex)) 917 894 .replaceAll("(?i)\\{style\\}", this.currentLayer.style); 918 895 … … 925 902 926 903 /** 927 * 904 * Returns TileMatrix that's working on given zoom level 928 905 * @param zoom zoom level 929 906 * @return TileMatrix that's working on this zoom level … … 1057 1034 */ 1058 1035 public static void checkUrl(String url) { 1059 CheckParameterUtil.ensureParameterNotNull(url, "url"); 1060 Matcher m = Pattern.compile("\\{[^}]*\\}").matcher(url); 1061 while (m.find()) { 1062 boolean isSupportedPattern = Arrays.stream(ALL_PATTERNS).anyMatch(pattern -> m.group().matches(pattern)); 1063 if (!isSupportedPattern) { 1064 throw new IllegalArgumentException( 1065 tr("{0} is not a valid WMS argument. Please check this server URL:\n{1}", m.group(), url)); 1066 } 1067 } 1036 ImageryPatterns.checkWmtsUrlPatterns(url); 1068 1037 } 1069 1038
Note:
See TracChangeset
for help on using the changeset viewer.