Changeset 31881 in osm for applications/editors/josm/plugins/wikipedia/src
- Timestamp:
- 2015-12-28T11:57:17+01:00 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaApp.java
r31861 r31881 4 4 import java.io.IOException; 5 5 import java.io.InputStream; 6 import java.io.OutputStreamWriter;7 import java.net.HttpURLConnection;8 6 import java.net.URL; 7 import java.nio.charset.StandardCharsets; 9 8 import java.util.AbstractList; 10 9 import java.util.ArrayList; … … 20 19 import java.util.regex.Pattern; 21 20 21 import javax.xml.parsers.DocumentBuilder; 22 22 import javax.xml.parsers.DocumentBuilderFactory; 23 import javax.xml.parsers.ParserConfigurationException; 24 import javax.xml.xpath.XPath; 23 25 import javax.xml.xpath.XPathConstants; 24 26 import javax.xml.xpath.XPathExpression; … … 30 32 import org.openstreetmap.josm.data.osm.Tag; 31 33 import org.openstreetmap.josm.tools.CheckParameterUtil; 34 import org.openstreetmap.josm.tools.HttpClient; 32 35 import org.openstreetmap.josm.tools.Predicate; 33 36 import org.openstreetmap.josm.tools.Utils; … … 40 43 41 44 public static Pattern WIKIDATA_PATTERN = Pattern.compile("Q\\d+"); 45 private static final DocumentBuilder DOCUMENT_BUILDER = newDocumentBuilder(); 46 private static final XPath X_PATH = XPathFactory.newInstance().newXPath(); 42 47 43 48 private WikipediaApp() { … … 50 55 final String url = "https://tools.wmflabs.org/wp-world/marks.php?" 51 56 + "bbox=" + bbox + "&LANG=" + wikipediaLang; 52 Main.info("Wikipedia: GET " + url);53 57 // parse XML document 54 final XPathExpression xpathPlacemark = X PathFactory.newInstance().newXPath().compile("//Placemark");55 final XPathExpression xpathName = X PathFactory.newInstance().newXPath().compile("name/text()");56 final XPathExpression xpathCoord = X PathFactory.newInstance().newXPath().compile("Point/coordinates/text()");57 final XPathExpression xpathDescr = X PathFactory.newInstance().newXPath().compile("description");58 try (final InputStream in = Utils.openURL(new URL(url))) {59 Document doc = D ocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);58 final XPathExpression xpathPlacemark = X_PATH.compile("//Placemark"); 59 final XPathExpression xpathName = X_PATH.compile("name/text()"); 60 final XPathExpression xpathCoord = X_PATH.compile("Point/coordinates/text()"); 61 final XPathExpression xpathDescr = X_PATH.compile("description"); 62 try (final InputStream in = HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContent()) { 63 Document doc = DOCUMENT_BUILDER.parse(in); 60 64 NodeList nodes = (NodeList) xpathPlacemark.evaluate(doc, XPathConstants.NODESET); 61 65 // construct WikipediaEntry for each XML element … … 84 88 + "&depth=" + depth 85 89 + "&cat=" + Utils.encodeUrl(category); 86 Main.info("Wikipedia: GET " + url); 87 try (final InputStream in = Utils.openURL(new URL(url)); 88 final Scanner scanner = new Scanner(in, "UTF-8").useDelimiter("\n")) { 90 91 try (final Scanner scanner = new Scanner( 92 HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContentReader()) 93 .useDelimiter("\n")) { 89 94 final List<WikipediaEntry> entries = new ArrayList<>(); 90 95 while (scanner.hasNext()) { … … 119 124 final String url = "https://tools.wmflabs.org/wiwosm/osmjson/getGeoJSON.php?action=check" 120 125 + "&lang=" + wikipediaLang; 121 Main.info("Wikipedia: POST " + url + " " + articleNames);122 126 123 127 try { 124 final HttpURLConnection connection = Utils.openHttpConnection(new URL(url)); 125 connection.setDoOutput(true); 126 127 try (final OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8")) { 128 out.write("articles=" + Utils.encodeUrl(Utils.join(",", articleNames))); 129 } 130 131 try (final Scanner scanner = new Scanner(connection.getInputStream(), "UTF-8").useDelimiter("\n")) { 128 final String requestBody = "articles=" + Utils.encodeUrl(Utils.join(",", articleNames)); 129 try (final Scanner scanner = new Scanner( 130 HttpClient.create(new URL(url), "PUT").setReasonForRequest("Wikipedia") 131 .setRequestBody(requestBody.getBytes(StandardCharsets.UTF_8)) 132 .connect().getContentReader()) 133 .useDelimiter("\n")) { 132 134 while (scanner.hasNext()) { 133 135 //[article]\t[0|1] … … 186 188 } 187 189 })); 188 Main.info("Wikipedia: GET " + url);189 190 final Map<String, String> r = new TreeMap<>(); 190 try (final InputStream in = Utils.openURL(new URL(url))) {191 final Document xml = D ocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);192 final NodeList nodes = (NodeList) X PathFactory.newInstance().newXPath().compile("//entity").evaluate(xml, XPathConstants.NODESET);191 try (final InputStream in = HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContent()) { 192 final Document xml = DOCUMENT_BUILDER.parse(in); 193 final NodeList nodes = (NodeList) X_PATH.compile("//entity").evaluate(xml, XPathConstants.NODESET); 193 194 for (int i = 0; i < nodes.getLength(); i++) { 194 195 final Node node = nodes.item(i); 195 final String wikidata = (String) X PathFactory.newInstance().newXPath().compile("./@id").evaluate(node, XPathConstants.STRING);196 final String wikipedia = (String) X PathFactory.newInstance().newXPath().compile("./sitelinks/sitelink/@title").evaluate(node, XPathConstants.STRING);196 final String wikidata = (String) X_PATH.compile("./@id").evaluate(node, XPathConstants.STRING); 197 final String wikipedia = (String) X_PATH.compile("./sitelinks/sitelink/@title").evaluate(node, XPathConstants.STRING); 197 198 r.put(wikipedia, wikidata); 198 199 } … … 213 214 "&format=xml" + 214 215 (preferredLanguage != null ? "&languages=" + preferredLanguage + "&languagefallback=en" : ""); 215 Main.info("Wikipedia: GET " + url); 216 try (final InputStream in = Utils.openURL(new URL(url))) { 217 final Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in); 218 final Node label = (Node) XPathFactory.newInstance().newXPath().compile("//label").evaluate(xml, XPathConstants.NODE); 216 try (final InputStream in = HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContent()) { 217 final Document xml = DOCUMENT_BUILDER.parse(in); 218 final Node label = (Node) X_PATH.compile("//label").evaluate(xml, XPathConstants.NODE); 219 219 if (label == null && preferredLanguage != null) { 220 220 return getLabelForWikidata(wikidataId, null); … … 222 222 return null; 223 223 } else { 224 return (String) X PathFactory.newInstance().newXPath().compile("./@value").evaluate(label, XPathConstants.STRING);224 return (String) X_PATH.compile("./@value").evaluate(label, XPathConstants.STRING); 225 225 } 226 226 } … … 239 239 "&lllimit=500" + 240 240 "&format=xml"; 241 Main.info("Wikipedia: GET " + url); 242 try (final InputStream in = Utils.openURL(new URL(url))) { 243 final Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in); 244 final NodeList nodes = (NodeList) XPathFactory.newInstance().newXPath().compile("//ll").evaluate(xml, XPathConstants.NODESET); 241 try (final InputStream in = HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContent()) { 242 final Document xml = DOCUMENT_BUILDER.parse(in); 243 final NodeList nodes = (NodeList) X_PATH.compile("//ll").evaluate(xml, XPathConstants.NODESET); 245 244 for (int i = 0; i < nodes.getLength(); i++) { 246 245 final String lang = nodes.item(i).getAttributes().getNamedItem("lang").getTextContent(); … … 370 369 + "&lang=" + wikipediaLang 371 370 + "&article=" + Utils.encodeUrl(wikipediaArticle); 372 Main.info("Wikipedia: GET " + url); 373 try (final InputStream in = Utils.openURL(new URL(url)); 374 final Scanner scanner = new Scanner(in, "UTF-8")) { 371 try (final Scanner scanner = new Scanner( 372 HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContentReader())) { 375 373 wiwosmStatus = scanner.hasNextInt() && scanner.nextInt() == 1; 376 374 } … … 419 417 }; 420 418 } 419 420 private static DocumentBuilder newDocumentBuilder() { 421 try { 422 return DocumentBuilderFactory.newInstance().newDocumentBuilder(); 423 } catch (ParserConfigurationException e) { 424 Main.warn("Cannot create DocumentBuilder"); 425 Main.warn(e); 426 throw new RuntimeException(e); 427 } 428 } 421 429 }
Note:
See TracChangeset
for help on using the changeset viewer.