Changeset 31847 in osm for applications/editors/josm/plugins/wikipedia
- Timestamp:
- 2015-12-22T18:51:31+01:00 (9 years ago)
- 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 3 3 4 4 import java.io.IOException; 5 import java.io.InputStream; 5 6 import java.io.OutputStreamWriter; 6 7 import java.io.UnsupportedEncodingException; 8 import java.net.HttpURLConnection; 7 9 import java.net.URL; 8 import java.net.URLConnection;9 10 import java.net.URLDecoder; 10 11 import java.net.URLEncoder; … … 52 53 final XPathExpression xpathCoord = XPathFactory.newInstance().newXPath().compile("Point/coordinates/text()"); 53 54 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 } 69 72 } catch (Exception ex) { 70 73 throw new RuntimeException(ex); … … 79 82 + "&cat=" + encodeURL(category); 80 83 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 } 88 93 } catch (IOException ex) { 89 94 throw new RuntimeException(ex); … … 114 119 115 120 try { 116 URLConnection connection = new URL(url) .openConnection();121 final HttpURLConnection connection = Utils.openHttpConnection(new URL(url)); 117 122 connection.setDoOutput(true); 118 123 119 try (OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8")) { 124 try (final OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8")) { 120 125 out.write("articles=" + encodeURL(Utils.join(",", articleNames))); 121 126 } 122 127 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 } 132 138 } 133 139 } … … 170 176 "&format=xml"; 171 177 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 } 178 186 } 179 187 return r; … … 298 306 + "&article=" + encodeURL(wikipediaArticle); 299 307 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 } 302 312 } catch (IOException ex) { 303 313 throw new RuntimeException(ex); -
applications/editors/josm/plugins/wikipedia/test/unit/org/wikipedia/WikipediaAppTest.java
r30550 r31847 1 1 package org.wikipedia; 2 2 3 import org.junit.Before; 3 4 import org.junit.Test; 5 import org.openstreetmap.josm.Main; 4 6 import org.openstreetmap.josm.data.coor.LatLon; 5 7 import org.openstreetmap.josm.tools.Predicate; … … 19 21 20 22 public class WikipediaAppTest { 23 @Before 24 public void setUp() throws Exception { 25 Main.initApplicationPreferences(); 26 } 21 27 22 28 @Test
Note:
See TracChangeset
for help on using the changeset viewer.