Ignore:
Timestamp:
2015-12-22T18:51:31+01:00 (9 years ago)
Author:
simon04
Message:

JOSM/wikipedia: use try-with-resource and utility URL methods

Location:
applications/editors/josm/plugins/wikipedia
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaApp.java

    r30738 r31847  
    33
    44import java.io.IOException;
     5import java.io.InputStream;
    56import java.io.OutputStreamWriter;
    67import java.io.UnsupportedEncodingException;
     8import java.net.HttpURLConnection;
    79import java.net.URL;
    8 import java.net.URLConnection;
    910import java.net.URLDecoder;
    1011import java.net.URLEncoder;
     
    5253            final XPathExpression xpathCoord = XPathFactory.newInstance().newXPath().compile("Point/coordinates/text()");
    5354            final XPathExpression xpathDescr = XPathFactory.newInstance().newXPath().compile("description");
    54             Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new URL(url).openStream());
    55             NodeList nodes = (NodeList) xpathPlacemark.evaluate(doc, XPathConstants.NODESET);
    56             // construct WikipediaEntry for each XML element
    57             List<WikipediaEntry> entries = new ArrayList<>(nodes.getLength());
    58             for (int i = 0; i < nodes.getLength(); i++) {
    59                 final String[] coord = xpathCoord.evaluate(nodes.item(i)).split(",");
    60                 if (coord.length <= 2) {
    61                     continue;
    62                 }
    63                 final String name = xpathName.evaluate(nodes.item(i));
    64                 final String descr = xpathDescr.evaluate(nodes.item(i));
    65                 entries.add(new WikipediaEntry(name, descr,
    66                         new LatLon(Double.parseDouble(coord[1]), Double.parseDouble(coord[0]))));
    67             }
    68             return entries;
     55            try (final InputStream in = Utils.openURL(new URL(url))) {
     56                Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
     57                NodeList nodes = (NodeList) xpathPlacemark.evaluate(doc, XPathConstants.NODESET);
     58                // construct WikipediaEntry for each XML element
     59                List<WikipediaEntry> entries = new ArrayList<>(nodes.getLength());
     60                for (int i = 0; i < nodes.getLength(); i++) {
     61                    final String[] coord = xpathCoord.evaluate(nodes.item(i)).split(",");
     62                    if (coord.length <= 2) {
     63                        continue;
     64                    }
     65                    final String name = xpathName.evaluate(nodes.item(i));
     66                    final String descr = xpathDescr.evaluate(nodes.item(i));
     67                    entries.add(new WikipediaEntry(name, descr,
     68                            new LatLon(Double.parseDouble(coord[1]), Double.parseDouble(coord[0]))));
     69                }
     70                return entries;
     71            }
    6972        } catch (Exception ex) {
    7073            throw new RuntimeException(ex);
     
    7982                    + "&cat=" + encodeURL(category);
    8083            System.out.println("Wikipedia: GET " + url);
    81             final Scanner scanner = new Scanner(new URL(url).openStream(), "UTF-8").useDelimiter("\n");
    82             final List<WikipediaEntry> entries = new ArrayList<>();
    83             while (scanner.hasNext()) {
    84                 final String article = scanner.next().trim().replace("_", " ");
    85                 entries.add(new WikipediaEntry(article, wikipediaLang, article));
    86             }
    87             return entries;
     84            try (final InputStream in = Utils.openURL(new URL(url));
     85                 final Scanner scanner = new Scanner(in, "UTF-8").useDelimiter("\n")) {
     86                final List<WikipediaEntry> entries = new ArrayList<>();
     87                while (scanner.hasNext()) {
     88                    final String article = scanner.next().trim().replace("_", " ");
     89                    entries.add(new WikipediaEntry(article, wikipediaLang, article));
     90                }
     91                return entries;
     92            }
    8893        } catch (IOException ex) {
    8994            throw new RuntimeException(ex);
     
    114119
    115120            try {
    116                 URLConnection connection = new URL(url).openConnection();
     121                final HttpURLConnection connection = Utils.openHttpConnection(new URL(url));
    117122                connection.setDoOutput(true);
    118123
    119                 try (OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8")) {
     124                try (final OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8")) {
    120125                        out.write("articles=" + encodeURL(Utils.join(",", articleNames)));
    121126                }
    122127
    123                 final Scanner scanner = new Scanner(connection.getInputStream(), "UTF-8").useDelimiter("\n");
    124                 while (scanner.hasNext()) {
    125                     //[article]\t[0|1]
    126                     final String line = scanner.next();
    127                     final String[] x = line.split("\t");
    128                     if (x.length == 2) {
    129                         status.put(x[0], "1".equals(x[1]));
    130                     } else {
    131                         Main.error("Unknown element " + line);
     128                try (final Scanner scanner = new Scanner(connection.getInputStream(), "UTF-8").useDelimiter("\n")) {
     129                    while (scanner.hasNext()) {
     130                        //[article]\t[0|1]
     131                        final String line = scanner.next();
     132                        final String[] x = line.split("\t");
     133                        if (x.length == 2) {
     134                            status.put(x[0], "1".equals(x[1]));
     135                        } else {
     136                            Main.error("Unknown element " + line);
     137                        }
    132138                    }
    133139                }
     
    170176                    "&format=xml";
    171177            System.out.println("Wikipedia: GET " + url);
    172             final Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new URL(url).openStream());
    173             final NodeList nodes = (NodeList) XPathFactory.newInstance().newXPath().compile("//ll").evaluate(xml, XPathConstants.NODESET);
    174             for (int i = 0; i < nodes.getLength(); i++) {
    175                 final String lang = nodes.item(i).getAttributes().getNamedItem("lang").getTextContent();
    176                 final String name = nodes.item(i).getTextContent();
    177                 r.add(new WikipediaLangArticle(lang, name));
     178            try (final InputStream in = Utils.openURL(new URL(url))) {
     179                final Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
     180                final NodeList nodes = (NodeList) XPathFactory.newInstance().newXPath().compile("//ll").evaluate(xml, XPathConstants.NODESET);
     181                for (int i = 0; i < nodes.getLength(); i++) {
     182                    final String lang = nodes.item(i).getAttributes().getNamedItem("lang").getTextContent();
     183                    final String name = nodes.item(i).getTextContent();
     184                    r.add(new WikipediaLangArticle(lang, name));
     185                }
    178186            }
    179187            return r;
     
    298306                        + "&article=" + encodeURL(wikipediaArticle);
    299307                System.out.println("Wikipedia: GET " + url);
    300                 final Scanner scanner = new Scanner(new URL(url).openStream(), "UTF-8");
    301                 wiwosmStatus = scanner.hasNextInt() && scanner.nextInt() == 1;
     308                try (final InputStream in = Utils.openURL(new URL(url));
     309                     final Scanner scanner = new Scanner(in, "UTF-8")) {
     310                    wiwosmStatus = scanner.hasNextInt() && scanner.nextInt() == 1;
     311                }
    302312            } catch (IOException ex) {
    303313                throw new RuntimeException(ex);
  • applications/editors/josm/plugins/wikipedia/test/unit/org/wikipedia/WikipediaAppTest.java

    r30550 r31847  
    11package org.wikipedia;
    22
     3import org.junit.Before;
    34import org.junit.Test;
     5import org.openstreetmap.josm.Main;
    46import org.openstreetmap.josm.data.coor.LatLon;
    57import org.openstreetmap.josm.tools.Predicate;
     
    1921
    2022public class WikipediaAppTest {
     23    @Before
     24    public void setUp() throws Exception {
     25        Main.initApplicationPreferences();
     26    }
    2127
    2228    @Test
Note: See TracChangeset for help on using the changeset viewer.