Ticket #23485: 23485.patch

File 23485.patch, 7.3 KB (added by taylor.smock, 3 months ago)

Check for required elements in ELI SyncEditorLayerIndex since XSD syntax does not allow us to validate the desired XML format

  • scripts/SyncEditorLayerIndex.java

    Subject: [PATCH] #23485: JOSM crashes when opening Imagery Preferences
    ---
    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
    diff --git a/scripts/SyncEditorLayerIndex.java b/scripts/SyncEditorLayerIndex.java
    a b  
    66import java.io.BufferedReader;
    77import java.io.BufferedWriter;
    88import java.io.IOException;
     9import java.io.InputStream;
    910import java.io.OutputStreamWriter;
    1011import java.io.Writer;
    1112import java.lang.reflect.Field;
    1213import java.net.MalformedURLException;
     14import java.net.URI;
    1315import java.net.URL;
    1416import java.nio.charset.Charset;
    1517import java.nio.file.Files;
     
    3739import java.util.regex.Pattern;
    3840import java.util.stream.Collectors;
    3941
    40 import jakarta.json.Json;
    41 import jakarta.json.JsonArray;
    42 import jakarta.json.JsonNumber;
    43 import jakarta.json.JsonObject;
    44 import jakarta.json.JsonReader;
    45 import jakarta.json.JsonString;
    46 import jakarta.json.JsonValue;
    47 
    4842import org.openstreetmap.gui.jmapviewer.Coordinate;
    4943import org.openstreetmap.josm.data.Preferences;
    5044import org.openstreetmap.josm.data.imagery.ImageryInfo;
     
    5751import org.openstreetmap.josm.data.validation.routines.DomainValidator;
    5852import org.openstreetmap.josm.io.imagery.ImageryReader;
    5953import org.openstreetmap.josm.spi.preferences.Config;
     54import org.openstreetmap.josm.tools.HttpClient;
     55import org.openstreetmap.josm.tools.Http1Client;
    6056import org.openstreetmap.josm.tools.ImageProvider;
    6157import org.openstreetmap.josm.tools.JosmRuntimeException;
    6258import org.openstreetmap.josm.tools.Logging;
     
    6662import org.openstreetmap.josm.tools.Utils;
    6763import org.xml.sax.SAXException;
    6864
     65import jakarta.json.Json;
     66import jakarta.json.JsonArray;
     67import jakarta.json.JsonNumber;
     68import jakarta.json.JsonObject;
     69import jakarta.json.JsonReader;
     70import jakarta.json.JsonString;
     71import jakarta.json.JsonValue;
     72
    6973/**
    7074 * Compare and analyse the differences of the editor layer index and the JOSM imagery list.
    7175 * The goal is to keep both lists in sync.
     
    236240    }
    237241
    238242    void loadSkip() throws IOException {
     243        if (Files.notExists(Paths.get(ignoreInputFile))) {
     244            return;
     245        }
    239246        final Pattern pattern = Pattern.compile("^\\|\\| *(ELI|Ignore) *\\|\\| *\\{\\{\\{(.+)\\}\\}\\} *\\|\\|");
    240247        try (BufferedReader fr = Files.newBufferedReader(Paths.get(ignoreInputFile), UTF_8)) {
    241248            String line;
     
    494501    }
    495502
    496503    void loadJosmEntries() throws IOException, SAXException, ReflectiveOperationException {
     504        if (Files.notExists(Paths.get(josmInputFile))) {
     505            HttpClient.setFactory(Http1Client::new);
     506            final HttpClient client = HttpClient.create(URI.create("https://josm.openstreetmap.de/maps").toURL());
     507            try (InputStream inputStream = client.connect().getContent()) {
     508                Files.copy(inputStream, Paths.get(josmInputFile));
     509            } finally {
     510                client.disconnect();
     511            }
     512        }
     513
    497514        try (ImageryReader reader = new ImageryReader(josmInputFile)) {
    498515            josmEntries = reader.parse();
    499516        }
    500517
    501518        for (ImageryInfo e : josmEntries) {
     519            if (!e.isValid()) {
     520                myprintln("~~~ JOSM-Entry missing fields (" + String.join(", ", e.getMissingFields()) + "): " + getDescription(e));
     521            }
    502522            if (isBlank(getUrl(e))) {
    503523                myprintln("~~~ JOSM-Entry without URL: " + getDescription(e));
    504524                continue;
  • src/org/openstreetmap/josm/data/imagery/ImageryInfo.java

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
    diff --git a/src/org/openstreetmap/josm/data/imagery/ImageryInfo.java b/src/org/openstreetmap/josm/data/imagery/ImageryInfo.java
    a b  
    1919import java.util.regex.Pattern;
    2020import java.util.stream.Collectors;
    2121
    22 import jakarta.json.Json;
    23 import jakarta.json.JsonObject;
    24 import jakarta.json.JsonReader;
    2522import javax.swing.ImageIcon;
    2623
    2724import org.openstreetmap.josm.data.StructUtils.StructEntry;
     
    3936import org.openstreetmap.josm.tools.StreamUtils;
    4037import org.openstreetmap.josm.tools.Utils;
    4138
     39import jakarta.json.Json;
     40import jakarta.json.JsonObject;
     41import jakarta.json.JsonReader;
     42
    4243/**
    4344 * Class that stores info about an image background layer.
    4445 *
     
    205206        }
    206207    }
    207208
     209    private static final String[] EMPTY_STRING = new String[0];
     210
    208211    private double pixelPerDegree;
    209212    /** maximum zoom level for TMS imagery */
    210213    private int defaultMaxZoom;
     
    963966        }
    964967    }
    965968
     969    /**
     970     * Check to see if this info is valid (the XSD is overly permissive due to limitations of the XSD syntax)
     971     * @return {@code true} if this info is valid
     972     * @since xxx
     973     */
     974    public boolean isValid() {
     975        return this.getName() != null &&
     976                this.getId() != null &&
     977                this.getSourceType() != null &&
     978                this.getUrl() != null &&
     979                this.getImageryCategory() != null;
     980    }
     981
     982    /**
     983     * Get the missing fields for this info
     984     * @return The missing fields, or an empty array
     985     */
     986    public String[] getMissingFields() {
     987        if (this.isValid()) {
     988            return EMPTY_STRING;
     989        }
     990        List<String> missingFields = new ArrayList<>();
     991        if (this.getName() == null) {
     992            missingFields.add("name");
     993        }
     994        if (this.getId() == null) {
     995            missingFields.add("id");
     996        }
     997        if (this.getSourceType() == null) {
     998            missingFields.add("type");
     999        }
     1000        if (this.getUrl() == null) {
     1001            missingFields.add("url");
     1002        }
     1003        if (this.getImageryCategory() == null) {
     1004            missingFields.add("category");
     1005        }
     1006        return missingFields.toArray(new String[0]);
     1007    }
     1008
    9661009    /**
    9671010     * Return the sorted list of activated source IDs.
    9681011     * @return sorted list of activated source IDs
  • src/org/openstreetmap/josm/data/imagery/ImageryLayerInfo.java

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
    diff --git a/src/org/openstreetmap/josm/data/imagery/ImageryLayerInfo.java b/src/org/openstreetmap/josm/data/imagery/ImageryLayerInfo.java
    a b  
    166166                reader = new ImageryReader(source);
    167167                reader.setFastFail(fastFail);
    168168                Collection<ImageryInfo> result = reader.parse();
     169                // See #23485 (remove invalid source entries)
     170                result.removeIf(info -> !info.isValid());
    169171                newLayers.addAll(result);
    170172            } catch (IOException ex) {
    171173                loadError = true;