Changeset 32625 in osm
- Timestamp:
- 2016-07-10T22:56:57+02:00 (9 years ago)
- Location:
- applications/editors/josm/plugins/wikipedia
- Files:
-
- 2 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaApp.java
r32624 r32625 121 121 } 122 122 123 static List<WikidataEntry> getWikidataEntriesForQuery(final String language, final String query) { 124 try { 125 final String url = "https://www.wikidata.org/w/api.php" + 126 "?action=wbsearchentities" + 127 "&language=" + language + 128 "&strictlanguage=false" + 129 "&search=" + Utils.encodeUrl(query) + 130 "&limit=50" + 131 "&format=xml"; 132 final List<WikidataEntry> r = new ArrayList<>(); 133 try (final InputStream in = HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContent()) { 134 final Document xml = DOCUMENT_BUILDER.parse(in); 135 final NodeList nodes = (NodeList) X_PATH.compile("//entity").evaluate(xml, XPathConstants.NODESET); 136 final XPathExpression xpathId = X_PATH.compile("@id"); 137 final XPathExpression xpathLabel = X_PATH.compile("@label"); 138 for (int i = 0; i < nodes.getLength(); i++) { 139 final Node node = nodes.item(i); 140 final String id = (String) xpathId.evaluate(node, XPathConstants.STRING); 141 final String label = (String) xpathLabel.evaluate(node, XPathConstants.STRING); 142 r.add(new WikidataEntry(id, label, null)); 143 } 144 } 145 return r; 146 } catch (Exception ex) { 147 throw new RuntimeException(ex); 148 } 149 } 150 123 151 static List<WikipediaEntry> getEntriesFromCategory(String wikipediaLang, String category, int depth) { 124 152 try { -
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaPlugin.java
r31857 r32625 15 15 MainMenu.add(Main.main.menu.dataMenu, new WikipediaAddNamesAction()); 16 16 MainMenu.add(Main.main.menu.dataMenu, new FetchWikidataAction()); 17 MainMenu.add(Main.main.menu.dataMenu, new WikidataItemSearchDialog.Action()); 17 18 } 18 19 -
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaToggleDialog.java
r32624 r32625 55 55 new SideButton(new WikipediaLoadCategoryAction()), 56 56 new SideButton(new PasteWikipediaArticlesAction()), 57 new SideButton(new AddWikipediaTagAction()), 57 new SideButton(new AddWikipediaTagAction(list)), 58 58 new SideButton(new WikipediaSettingsAction(), false))); 59 59 updateTitle(); … … 61 61 /** A string describing the context (use-case) for determining the dialog title */ 62 62 String titleContext = null; 63 final StringProperty wikipediaLang = new StringProperty("wikipedia.lang", LanguageInfo.getJOSMLocaleCode().substring(0, 2)); 63 static final StringProperty wikipediaLang = new StringProperty("wikipedia.lang", LanguageInfo.getJOSMLocaleCode().substring(0, 2)); 64 64 final Set<String> articles = new HashSet<>(); 65 65 final DefaultListModel<WikipediaEntry> model = new DefaultListModel<>(); … … 287 287 } 288 288 289 class AddWikipediaTagAction extends AbstractAction { 290 291 public AddWikipediaTagAction() { 289 static class AddWikipediaTagAction extends AbstractAction { 290 291 private final JList<WikipediaEntry> list; 292 293 public AddWikipediaTagAction(JList<WikipediaEntry> list) { 292 294 super(tr("Add Tag")); 295 this.list = list; 293 296 new ImageProvider("pastetags").getResource().attachImageIcon(this, true); 294 297 putValue(SHORT_DESCRIPTION, tr("Adds a ''wikipedia'' tag corresponding to this article to the selected objects")); … … 297 300 @Override 298 301 public void actionPerformed(ActionEvent e) { 299 if (list.getSelectedValue() != null) { 300 Tag tag = ((WikipediaEntry) list.getSelectedValue()).createWikipediaTag(); 301 if (tag != null) { 302 final Collection<OsmPrimitive> selected = Main.getLayerManager().getEditDataSet().getSelected(); 303 if (!GuiUtils.confirmOverwrite(tag.getKey(), tag.getValue(), selected)) { 304 return; 305 } 306 ChangePropertyCommand cmd = new ChangePropertyCommand( 307 selected, 308 tag.getKey(), tag.getValue()); 309 Main.main.undoRedo.add(cmd); 310 Main.worker.submit(new FetchWikidataAction.Fetcher(selected)); 311 } 312 } 302 addTag(list.getSelectedValue()); 303 } 304 305 static void addTag(WikipediaEntry entry) { 306 if (entry == null) { 307 return; 308 } 309 addTag(entry.createWikipediaTag()); 310 } 311 312 static void addTag(Tag tag) { 313 if (tag == null) { 314 return; 315 } 316 final Collection<OsmPrimitive> selected = Main.getLayerManager().getEditDataSet().getSelected(); 317 if (!GuiUtils.confirmOverwrite(tag.getKey(), tag.getValue(), selected)) { 318 return; 319 } 320 ChangePropertyCommand cmd = new ChangePropertyCommand( 321 selected, 322 tag.getKey(), tag.getValue()); 323 Main.main.undoRedo.add(cmd); 324 Main.worker.submit(new FetchWikidataAction.Fetcher(selected)); 313 325 } 314 326 } -
applications/editors/josm/plugins/wikipedia/test/unit/org/wikipedia/WikipediaAppTest.java
r32624 r32625 147 147 } 148 148 })); 149 } 150 151 @Test 152 public void testForQuery() throws Exception { 153 final List<WikipediaApp.WikidataEntry> entries = WikipediaApp.getWikidataEntriesForQuery("de", "Österreich"); 154 assertThat(entries.get(0).wikipediaArticle, is("Q40")); 155 assertThat(entries.get(0).wikipediaLang, is("wikidata")); 156 // assertThat(entries.get(0).label, is("Österreich")); 149 157 } 150 158
Note:
See TracChangeset
for help on using the changeset viewer.