Changeset 32694 in osm for applications/editors/josm
- Timestamp:
- 2016-07-21T17:55:47+02:00 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaApp.java
r32691 r32694 2 2 package org.wikipedia; 3 3 4 import java.io.BufferedReader; 4 5 import java.io.IOException; 5 6 import java.io.InputStream; … … 11 12 import java.util.Collection; 12 13 import java.util.Collections; 14 import java.util.Comparator; 13 15 import java.util.HashMap; 14 16 import java.util.List; … … 16 18 import java.util.Map; 17 19 import java.util.Objects; 18 import java.util.Scanner;19 20 import java.util.TreeMap; 20 21 import java.util.regex.Matcher; … … 145 146 + "&cat=" + Utils.encodeUrl(category); 146 147 147 try (final Scanner scanner = new Scanner( 148 HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContentReader()) 149 .useDelimiter("\n")) { 150 final List<WikipediaEntry> entries = new ArrayList<>(); 151 while (scanner.hasNext()) { 152 final String article = scanner.next().trim().replace("_", " "); 153 entries.add(new WikipediaEntry(wikipediaLang, article)); 154 } 155 return entries; 148 try (final BufferedReader reader = HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia") 149 .connect().getContentReader()) { 150 return reader.lines() 151 .map(line -> new WikipediaEntry(wikipediaLang, line.trim().replace("_", " "))) 152 .collect(Collectors.toList()); 156 153 } 157 154 } catch (IOException ex) { … … 161 158 162 159 static List<WikipediaEntry> getEntriesFromClipboard(final String wikipediaLang) { 163 return Arrays.stream(Utils.getClipboardContent().split("[\\n\\r]+")) 160 return Pattern.compile("[\\n\\r]+") 161 .splitAsStream(Utils.getClipboardContent()) 164 162 .map(x -> new WikipediaEntry(wikipediaLang, x)) 165 163 .collect(Collectors.toList()); … … 178 176 try { 179 177 final String requestBody = "articles=" + Utils.encodeUrl(articleNames.stream().collect(Collectors.joining(","))); 180 try (final Scanner scanner = new Scanner( 181 HttpClient.create(new URL(url), "POST").setReasonForRequest("Wikipedia") 178 try (final BufferedReader reader = HttpClient.create(new URL(url), "POST").setReasonForRequest("Wikipedia") 182 179 .setHeader("Content-Type", "application/x-www-form-urlencoded") 183 180 .setRequestBody(requestBody.getBytes(StandardCharsets.UTF_8)) 184 .connect().getContentReader()) 185 .useDelimiter("\n")) { 186 while (scanner.hasNext()) { 181 .connect().getContentReader()) { 182 reader.lines().forEach(line -> { 187 183 //[article]\t[0|1] 188 final String line = scanner.next();189 184 final String[] x = line.split("\t"); 190 185 if (x.length == 2) { … … 193 188 Main.error("Unknown element " + line); 194 189 } 195 } 190 }); 196 191 } 197 192 } catch (Exception ex) { … … 208 203 return Stream.of(p.get("wikidata")).filter(Objects::nonNull); 209 204 } 210 final Map<String, String> tags = p.getKeys();211 205 return Stream 212 .of( 213 WikipediaLangArticle.parseTag("wikipedia", tags.get("wikipedia")), 214 WikipediaLangArticle.parseTag("wikipedia:" + wikipediaLang, tags.get("wikipedia:" + wikipediaLang)) 215 ).filter(Objects::nonNull) 216 .filter(wikipediaLang::equals) 206 .of("wikipedia", "wikipedia:" + wikipediaLang) 207 .map(key -> WikipediaLangArticle.parseTag(key, p.get(key))) 208 .filter(Objects::nonNull) 209 .filter(wp -> wikipediaLang.equals(wp.lang)) 217 210 .map(wp -> wp.article); 218 211 } … … 223 216 static Map<String, String> getWikidataForArticles(String wikipediaLang, List<String> articles) { 224 217 if (articles.size() > 50) { 225 final Map<String, String> wikidataItems = new HashMap<>(); 226 for (final List<String> chunk : partitionList(articles, 50)) { 227 wikidataItems.putAll(getWikidataForArticles(wikipediaLang, chunk)); 228 } 229 return wikidataItems; 218 return partitionList(articles, 50).stream() 219 .flatMap(chunk -> getWikidataForArticles(wikipediaLang, chunk).entrySet().stream()) 220 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); 230 221 } 231 222 try { … … 290 281 static List<WikidataEntry> getLabelForWikidata(List<? extends WikipediaEntry> entries, Locale locale, String ... preferredLanguage) { 291 282 if (entries.size() > 50) { 292 final List<WikidataEntry> entriesWithLabel = new ArrayList<>(entries.size()); 293 for (final List<? extends WikipediaEntry> chunk : partitionList(entries, 50)) { 294 entriesWithLabel.addAll(getLabelForWikidata(chunk, locale, preferredLanguage)); 295 } 296 return entriesWithLabel; 283 return partitionList(entries, 50).stream() 284 .flatMap(chunk -> getLabelForWikidata(chunk, locale, preferredLanguage).stream()) 285 .collect(Collectors.toList()); 297 286 } 298 287 try { … … 501 490 @Override 502 491 public int compareTo(WikipediaEntry o) { 503 final int c = AlphanumComparator.getInstance().compare(label, o.label); 504 return c != 0 ? c : AlphanumComparator.getInstance().compare(wikipediaArticle, o.wikipediaArticle); 492 return Comparator 493 .<WikipediaEntry, String>comparing(x -> x.label, AlphanumComparator.getInstance()) 494 .thenComparing(x -> x.wikipediaArticle, AlphanumComparator.getInstance()) 495 .compare(this, o); 505 496 } 506 497 }
Note:
See TracChangeset
for help on using the changeset viewer.