Ignore:
Timestamp:
2019-01-05T00:26:34+01:00 (5 years ago)
Author:
simon04
Message:

Extract Mediawiki class

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/properties/HelpAction.java

    r14208 r14641  
    77import java.awt.event.KeyEvent;
    88import java.io.IOException;
    9 import java.io.InputStream;
    10 import java.net.URL;
    119import java.util.ArrayList;
    1210import java.util.Arrays;
     
    1513import java.util.Objects;
    1614import java.util.function.IntFunction;
    17 import java.util.stream.Collectors;
    1815
    1916import javax.swing.AbstractAction;
     
    2118import javax.swing.KeyStroke;
    2219import javax.xml.parsers.ParserConfigurationException;
    23 import javax.xml.xpath.XPath;
    24 import javax.xml.xpath.XPathConstants;
    2520import javax.xml.xpath.XPathExpressionException;
    26 import javax.xml.xpath.XPathFactory;
    2721
    2822import org.openstreetmap.josm.data.osm.IRelation;
    2923import org.openstreetmap.josm.gui.MainApplication;
    3024import org.openstreetmap.josm.spi.preferences.Config;
    31 import org.openstreetmap.josm.tools.HttpClient;
    3225import org.openstreetmap.josm.tools.ImageProvider;
    3326import org.openstreetmap.josm.tools.LanguageInfo;
    3427import org.openstreetmap.josm.tools.Logging;
     28import org.openstreetmap.josm.tools.Mediawiki;
    3529import org.openstreetmap.josm.tools.OpenBrowser;
    3630import org.openstreetmap.josm.tools.Utils;
    37 import org.openstreetmap.josm.tools.XmlUtils;
    38 import org.w3c.dom.Document;
    39 import org.w3c.dom.Node;
    4031import org.xml.sax.SAXException;
    4132
     
    164155    public static void displayHelp(final List<String> pages) {
    165156        try {
    166             // find a page that actually exists in the wiki
    167             // API documentation: https://wiki.openstreetmap.org/w/api.php?action=help&modules=query
    168             final URL url = new URL(Config.getUrls().getOSMWiki() + "/w/api.php?action=query&format=xml&titles=" + pages.stream()
    169                     .map(Utils::encodeUrl)
    170                     .collect(Collectors.joining("|"))
    171             );
    172             final HttpClient.Response conn = HttpClient.create(url).connect();
    173             final Document document;
    174             try (InputStream content = conn.getContent()) {
    175                 document = XmlUtils.parseSafeDOM(content);
    176             }
    177             conn.disconnect();
    178             final XPath xPath = XPathFactory.newInstance().newXPath();
    179             for (String page : pages) {
    180                 String normalized = xPath.evaluate("/api/query/normalized/n[@from='" + page + "']/@to", document);
    181                 if (normalized == null || normalized.isEmpty()) {
    182                     normalized = page;
    183                 }
    184                 final Node node = (Node) xPath.evaluate("/api/query/pages/page[@title='" + normalized + "']", document, XPathConstants.NODE);
    185                 if (node != null
    186                         && node.getAttributes().getNamedItem("missing") == null
    187                         && node.getAttributes().getNamedItem("invalid") == null) {
    188                     OpenBrowser.displayUrl(Config.getUrls().getOSMWiki() + "/wiki/" + page);
    189                     break;
    190                 }
    191             }
     157            new Mediawiki(Config.getUrls().getOSMWiki())
     158                    .findExistingPage(pages)
     159                    .ifPresent(page -> OpenBrowser.displayUrl(Config.getUrls().getOSMWiki() + "/wiki/" + page));
    192160        } catch (IOException | ParserConfigurationException | XPathExpressionException | SAXException e1) {
    193161            Logging.error(e1);
Note: See TracChangeset for help on using the changeset viewer.