Changeset 17578 in josm for trunk


Ignore:
Timestamp:
2021-03-17T15:07:13+01:00 (4 years ago)
Author:
Don-vip
Message:

fix #20610 - WMTS/WMS code deduplication

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  
    22package org.openstreetmap.josm.data.imagery;
    33
    4 import static org.openstreetmap.josm.tools.I18n.tr;
     4import static org.openstreetmap.josm.data.imagery.ImageryPatterns.PATTERN_PARAM;
    55
    66import java.text.DecimalFormat;
    77import java.text.DecimalFormatSymbols;
    88import java.text.NumberFormat;
    9 import java.util.Arrays;
    109import java.util.Locale;
    1110import java.util.Map;
     
    1413import java.util.concurrent.ConcurrentHashMap;
    1514import java.util.regex.Matcher;
    16 import java.util.regex.Pattern;
    1715
    1816import org.openstreetmap.gui.jmapviewer.interfaces.TemplatedTileSource;
     
    2018import org.openstreetmap.josm.data.projection.Projection;
    2119import org.openstreetmap.josm.gui.layer.WMSLayer;
    22 import org.openstreetmap.josm.tools.CheckParameterUtil;
    2320import org.openstreetmap.josm.tools.Utils;
    2421
     
    3027 */
    3128public class TemplatedWMSTileSource extends AbstractWMSTileSource implements TemplatedTileSource {
    32     // CHECKSTYLE.OFF: SingleSpaceSeparator
    33     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-2
    44     private static final Pattern PATTERN_PARAM  = Pattern.compile("\\{([^}]+)\\}");
    45     // CHECKSTYLE.ON: SingleSpaceSeparator
    4629
    4730    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     };
    5431
    5532    private final Set<String> serverProjections;
     
    6845        this.headers.putAll(info.getCustomHttpHeaders());
    6946        this.date = info.getDate();
    70         handleTemplate();
     47        this.baseUrl = ImageryPatterns.handleHeaderTemplate(baseUrl, headers);
    7148        initProjection();
    7249        // Bounding box coordinates have to be switched for WMS 1.3.0 EPSG:4326.
     
    170147     */
    171148    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);
    194150    }
    195151}
  • trunk/src/org/openstreetmap/josm/data/imagery/WMTSTileSource.java

    r16643 r17578  
    22package org.openstreetmap.josm.data.imagery;
    33
     4import static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
     5import static javax.xml.stream.XMLStreamConstants.START_ELEMENT;
    46import static org.openstreetmap.josm.data.imagery.GetCapabilitiesParseHelper.QN_OWS_DCP;
    57import static org.openstreetmap.josm.data.imagery.GetCapabilitiesParseHelper.QN_OWS_GET;
     
    3739import java.util.concurrent.ConcurrentHashMap;
    3840import java.util.function.BiFunction;
    39 import java.util.regex.Matcher;
    40 import java.util.regex.Pattern;
    4141import java.util.stream.Collectors;
    4242
     
    107107    // CHECKSTYLE.ON: SingleSpaceSeparator
    108108
    109     private static final String PATTERN_HEADER = "\\{header\\(([^,]+),([^}]+)\\)\\}";
    110 
    111109    private static final String URL_GET_ENCODING_PARAMS = "SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER={layer}&STYLE={style}&"
    112110            + "FORMAT={format}&tileMatrixSet={TileMatrixSet}&tileMatrix={TileMatrix}&tileRow={TileRow}&tileCol={TileCol}";
    113 
    114     private static final String[] ALL_PATTERNS = {
    115         PATTERN_HEADER,
    116     };
    117111
    118112    private int cachedTileSize = -1;
     
    140134
    141135    /**
    142      *
    143136     * class representing WMTS TileMatrixSet
    144137     * This connects projection and TileMatrix (how the map is divided in tiles)
    145      *
     138     * @since 13733
    146139     */
    147140    public static class TileMatrixSet {
     
    208201    /**
    209202     * Class representing WMTS Layer information
    210      *
     203     * @since 13733
    211204     */
    212205    public static class Layer {
     
    298291    /**
    299292     * Exception thrown when parser doesn't find expected information in GetCapabilities document
    300      *
     293     * @since 13733
    301294     */
    302295    public static class WMTSGetCapabilitiesException extends Exception {
     
    368361        CheckParameterUtil.ensureThat(info.getDefaultLayers().size() < 2, "At most 1 default layer for WMTS is supported");
    369362        this.headers.putAll(info.getCustomHttpHeaders());
    370         this.baseUrl = GetCapabilitiesParseHelper.normalizeCapabilitiesUrl(handleTemplate(info.getUrl()));
     363        this.baseUrl = GetCapabilitiesParseHelper.normalizeCapabilitiesUrl(ImageryPatterns.handleHeaderTemplate(info.getUrl(), headers));
    371364        WMTSCapabilities capabilities = getCapabilities(baseUrl, headers);
    372365        this.layers = capabilities.getLayers();
     
    444437    }
    445438
    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 
    458439    /**
    459440     * Call remote server and parse response to WMTSCapabilities object
     
    482463                Collection<Layer> layers = null;
    483464                for (int event = reader.getEventType(); reader.hasNext(); event = reader.next()) {
    484                     if (event == XMLStreamReader.START_ELEMENT) {
     465                    if (event == START_ELEMENT) {
    485466                        QName qName = reader.getName();
    486467                        if (QN_OWS_OPERATIONS_METADATA.equals(qName)) {
     
    528509        Collection<Layer> layers = new ArrayList<>();
    529510        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()));
    531512                event = reader.next()) {
    532             if (event == XMLStreamReader.START_ELEMENT) {
     513            if (event == START_ELEMENT) {
    533514                QName qName = reader.getName();
    534515                if (QN_LAYER.equals(qName)) {
     
    574555
    575556        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()));
    577558                event = reader.next()) {
    578             if (event == XMLStreamReader.START_ELEMENT) {
     559            if (event == START_ELEMENT) {
    579560                QName qName = reader.getName();
    580561                tagStack.push(qName);
     
    612593            }
    613594            // 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) {
    615596                QName start = tagStack.pop();
    616597                if (!start.equals(reader.getName())) {
     
    643624        Dimension ret = new Dimension();
    644625        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()));
    647627                event = reader.next()) {
    648             if (event == XMLStreamReader.START_ELEMENT) {
     628            if (event == START_ELEMENT) {
    649629                QName qName = reader.getName();
    650630                if (QN_OWS_IDENTIFIER.equals(qName)) {
     
    670650        String ret = null;
    671651        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()));
    674653                event = reader.next()) {
    675             if (event == XMLStreamReader.START_ELEMENT && QN_TILEMATRIXSET.equals(reader.getName())) {
     654            if (event == START_ELEMENT && QN_TILEMATRIXSET.equals(reader.getName())) {
    676655                ret = reader.getElementText();
    677656            }
     
    689668        TileMatrixSetBuilder matrixSet = new TileMatrixSetBuilder();
    690669        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()));
    692671                event = reader.next()) {
    693                     if (event == XMLStreamReader.START_ELEMENT) {
     672                    if (event == START_ELEMENT) {
    694673                        QName qName = reader.getName();
    695674                        if (QN_OWS_IDENTIFIER.equals(qName)) {
     
    717696        TileMatrix ret = new TileMatrix();
    718697        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()));
    720699                event = reader.next()) {
    721             if (event == XMLStreamReader.START_ELEMENT) {
     700            if (event == START_ELEMENT) {
    722701                QName qName = reader.getName();
    723702                if (QN_OWS_IDENTIFIER.equals(qName)) {
     
    772751        LatLon upperCorner = null;
    773752        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()));
    776754                event = reader.next()) {
    777             if (event == XMLStreamReader.START_ELEMENT) {
     755            if (event == START_ELEMENT) {
    778756                QName qName = reader.getName();
    779757                if (QN_OWS_LOWER_CORNER.equals(qName)) {
     
    800778    private static WMTSCapabilities parseOperationMetadata(XMLStreamReader reader) throws XMLStreamException {
    801779        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()));
    804781                event = reader.next()) {
    805             if (event == XMLStreamReader.START_ELEMENT &&
     782            if (event == START_ELEMENT &&
    806783                    QN_OWS_OPERATION.equals(reader.getName()) &&
    807784                    "GetTile".equals(reader.getAttributeValue("", "name")) &&
     
    909886        }
    910887
    911         url = url.replaceAll("\\{layer\\}", this.currentLayer.identifier)
    912                 .replaceAll("\\{format\\}", this.currentLayer.format)
    913                 .replaceAll("\\{TileMatrixSet\\}", this.currentTileMatrixSet.identifier)
    914                 .replaceAll("\\{TileMatrix\\}", tileMatrix.identifier)
    915                 .replaceAll("\\{TileRow\\}", Integer.toString(tiley))
    916                 .replaceAll("\\{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))
    917894                .replaceAll("(?i)\\{style\\}", this.currentLayer.style);
    918895
     
    925902
    926903    /**
    927      *
     904     * Returns TileMatrix that's working on given zoom level
    928905     * @param zoom zoom level
    929906     * @return TileMatrix that's working on this zoom level
     
    10571034     */
    10581035    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);
    10681037    }
    10691038
Note: See TracChangeset for help on using the changeset viewer.