Changeset 28470 in osm for applications
- Timestamp:
- 2012-07-11T21:11:08+02:00 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaToggleDialog.java
r28469 r28470 16 16 import java.util.Collection; 17 17 import java.util.Collections; 18 import java.util.HashMap; 18 19 import java.util.HashSet; 19 import java.util.LinkedList;20 20 import java.util.List; 21 21 import java.util.Map; … … 53 53 import org.openstreetmap.josm.tools.LanguageInfo; 54 54 import org.openstreetmap.josm.tools.OpenBrowser; 55 import org.openstreetmap.josm.tools.Utils; 55 56 import org.w3c.dom.Document; 56 57 import org.w3c.dom.NodeList; … … 94 95 public JLabel getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 95 96 JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 96 if (articles.contains(((WikipediaEntry) value).name)) { 97 final WikipediaEntry entry = (WikipediaEntry) value; 98 if (entry.getWiwosmStatus() != null && entry.getWiwosmStatus()) { 99 label.setIcon(ImageProvider.getIfAvailable("misc", "grey_check")); 100 label.setToolTipText(tr("Available via WIWOSM server")); 101 } else if (articles.contains(entry.wikipediaArticle)) { 97 102 label.setIcon(ImageProvider.getIfAvailable("misc", "green_check")); 103 label.setToolTipText(tr("Available in local dataset")); 104 } else { 105 label.setToolTipText(tr("Not linked yet")); 98 106 } 99 107 return label; … … 107 115 if (index >= 0) { 108 116 final String description = ((WikipediaEntry) model.getElementAt(index)).description; 109 return description == null ? null : ("<html>" + description + "</html>"); 110 } else { 111 return null; 112 } 117 if (description != null) { 118 return "<html>" + description + "</html>"; 119 } 120 } 121 return super.getToolTipText(e); 113 122 } 114 123 }; … … 128 137 } 129 138 // decode URL for nicer value 130 try { 131 url = URLDecoder.decode(url, "UTF-8"); 132 } catch (UnsupportedEncodingException ex) { 133 throw new IllegalStateException(ex); 134 } 139 url = decodeURL(url); 135 140 // extract Wikipedia language and 136 141 final Matcher m = Pattern.compile("https?://(\\w*)\\.wikipedia\\.org/wiki/(.*)").matcher(url); … … 147 152 final String wikipediaLang, wikipediaArticle; 148 153 final LatLon coordinate; 154 private Boolean wiwosmStatus; 149 155 150 156 public WikipediaEntry(String name, String description, LatLon coordinate) { … … 170 176 171 177 protected final String getHrefFromDescription() { 178 if (description == null) { 179 return null; 180 } 172 181 final Matcher m = Pattern.compile(".*href=\"(.+?)\".*").matcher(description); 173 182 if (m.matches()) { … … 183 192 } 184 193 194 private void updateWiwosmStatus() { 195 try { 196 final String url = "http://toolserver.org/~master/osmjson/getGeoJSON.php?action=check" 197 + "&lang=" + wikipediaLang 198 + "&article=" + encodeURL(wikipediaArticle); 199 System.out.println("Wikipedia: GET " + url); 200 final Scanner scanner = new Scanner(new URL(url).openStream()); 201 wiwosmStatus = scanner.hasNextInt() && scanner.nextInt() == 1; 202 } catch (IOException ex) { 203 throw new RuntimeException(ex); 204 } 205 } 206 207 public void setWiwosmStatus(Boolean wiwosmStatus) { 208 this.wiwosmStatus = wiwosmStatus; 209 } 210 211 public Boolean getWiwosmStatus() { 212 return wiwosmStatus; 213 } 214 185 215 @Override 186 216 public String toString() { … … 191 221 public int compareTo(WikipediaEntry o) { 192 222 return name.compareTo(o.name); 223 } 224 } 225 226 private void setWikipediaEntries(Collection<WikipediaEntry> entries) { 227 Collection<String> articleNames = new ArrayList<String>(); 228 for (WikipediaEntry i : entries) { 229 articleNames.add(i.wikipediaArticle); 230 } 231 final String url = "https://toolserver.org/~simon04/getGeoJSONStatus.php" 232 + "?lang=" + wikipediaLang.get() 233 + "&articles=" + encodeURL(Utils.join(",", articleNames)); 234 System.out.println("Wikipedia: GET " + url); 235 Map<String, Boolean> status = new HashMap<String, Boolean>(); 236 237 try { 238 final Scanner scanner = new Scanner(new URL(url).openStream()).useDelimiter("\n"); 239 while (scanner.hasNext()) { 240 //[article]\t[0|1] 241 final String line = scanner.next(); 242 final String[] x = line.split("\t"); 243 if (x.length == 2) { 244 status.put(x[0], "1".equals(x[1])); 245 } else { 246 System.err.println("Unknown element "+line); 247 } 248 } 249 } catch (Exception ex) { 250 throw new RuntimeException(ex); 251 } 252 253 model.clear(); 254 for (WikipediaEntry i : entries) { 255 i.setWiwosmStatus(status.get(i.wikipediaArticle)); 256 model.addElement(i); 193 257 } 194 258 } … … 220 284 NodeList nodes = (NodeList) xpathPlacemark.evaluate(doc, XPathConstants.NODESET); 221 285 // construct WikipediaEntry for each XML element 222 List<WikipediaEntry> entries = new LinkedList<WikipediaEntry>();286 List<WikipediaEntry> entries = new ArrayList<WikipediaEntry>(nodes.getLength()); 223 287 for (int i = 0; i < nodes.getLength(); i++) { 224 288 final String[] coord = xpathCoord.evaluate(nodes.item(i)).split(","); … … 233 297 Collections.sort(entries); 234 298 // add entries to list model 235 model.clear(); 236 for (WikipediaEntry i : entries) { 237 model.addElement(i); 238 } 299 setWikipediaEntries(entries); 239 300 } catch (Exception ex) { 240 301 throw new RuntimeException(ex); … … 259 320 + "wikilang=" + wikipediaLang.get() 260 321 + "&wikifam=.wikipedia.org" 261 + "&basecat=" + URLEncoder.encode(category, "UTF-8")322 + "&basecat=" + encodeURL(category) 262 323 + "&basedeep=3&templates=&mode=al&format=csv"; 263 324 System.out.println("Wikipedia: GET " + url); 264 325 final Scanner scanner = new Scanner(new URL(url).openStream()).useDelimiter("\n"); 265 if (scanner.hasNext()) { 266 model.clear(); 267 } 326 final List<WikipediaEntry> entries = new ArrayList<WikipediaEntry>(); 268 327 while (scanner.hasNext()) { 269 328 final String article = scanner.next().split("\t")[1].replace("_", " "); 270 model.addElement(new WikipediaEntry(article, wikipediaLang.get(), article)); 271 } 329 entries.add(new WikipediaEntry(article, wikipediaLang.get(), article)); 330 } 331 Collections.sort(entries); 332 setWikipediaEntries(entries); 272 333 } catch (IOException ex) { 273 334 throw new RuntimeException(ex); … … 291 352 url = entry.getHrefFromDescription(); 292 353 } else { 293 url = "http://" + entry.wikipediaLang + ".wikipedia.org/wiki/" + entry.wikipediaArticle.replace(" ", "_"); 354 url = "http://" + entry.wikipediaLang + ".wikipedia.org/wiki/" 355 + encodeURL(entry.wikipediaArticle.replace(" ", "_")); 294 356 } 295 357 System.out.println("Wikipedia: opening " + url); … … 362 424 } else if (wp != null) { 363 425 //wikipedia=[lang]:[article] 364 try { 365 String[] item = URLDecoder.decode(wp, "UTF-8").split(":", 2); 366 if (item.length == 2 && wikipediaLang.get().equals(item[0])) { 367 r.add(item[1].replace("_", " ")); 368 } 369 } catch (UnsupportedEncodingException ex) { 370 throw new IllegalStateException(ex); 426 String[] item = decodeURL(wp).split(":", 2); 427 if (item.length == 2 && wikipediaLang.get().equals(item[0])) { 428 r.add(item[1].replace("_", " ")); 371 429 } 372 430 } … … 382 440 //wikipedia:[lang]=[lang]:[article] 383 441 //wikipedia:[lang]=[article] 384 try { 385 String[] item = URLDecoder.decode(wpLang, "UTF-8").split(":", 2); 386 r.add(item[item.length == 2 ? 1 : 0].replace("_", " ")); 387 } catch (UnsupportedEncodingException ex) { 388 throw new IllegalStateException(ex); 389 } 442 String[] item = decodeURL(wpLang).split(":", 2); 443 r.add(item[item.length == 2 ? 1 : 0].replace("_", " ")); 390 444 } 391 445 return r; … … 419 473 list.repaint(); 420 474 } 475 476 public static String decodeURL(String url) { 477 try { 478 return URLDecoder.decode(url, "UTF-8"); 479 } catch (UnsupportedEncodingException ex) { 480 throw new IllegalStateException(ex); 481 } 482 } 483 484 public static String encodeURL(String url) { 485 try { 486 return URLEncoder.encode(url, "UTF-8"); 487 } catch (UnsupportedEncodingException ex) { 488 throw new IllegalStateException(ex); 489 } 490 } 421 491 }
Note:
See TracChangeset
for help on using the changeset viewer.