Changeset 31857 in osm for applications/editors/josm/plugins/wikipedia
- Timestamp:
- 2015-12-25T20:03:27+01:00 (9 years ago)
- Location:
- applications/editors/josm/plugins/wikipedia
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/wikipedia/build.xml
r31114 r31857 5 5 <property name="commit.message" value="Commit message"/> 6 6 <!-- enter the *lowest* JOSM version this plugin is currently compatible with --> 7 <property name="plugin.main.version" value=" 7817"/>7 <property name="plugin.main.version" value="9149"/> 8 8 <property name="plugin.canloadatruntime" value="true"/> 9 9 -
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaApp.java
r31852 r31857 29 29 import org.openstreetmap.josm.data.osm.OsmPrimitive; 30 30 import org.openstreetmap.josm.data.osm.Tag; 31 import org.openstreetmap.josm.tools.CheckParameterUtil; 31 32 import org.openstreetmap.josm.tools.Predicate; 32 33 import org.openstreetmap.josm.tools.Utils; … … 37 38 38 39 public final class WikipediaApp { 40 41 public static Pattern WIKIDATA_PATTERN = Pattern.compile("Q\\d+"); 39 42 40 43 private WikipediaApp() { … … 201 204 } 202 205 206 static String getLabelForWikidata(String wikidataId, String preferredLanguage) { 207 try { 208 CheckParameterUtil.ensureThat(WIKIDATA_PATTERN.matcher(wikidataId).matches(), "Invalid Wikidata ID given"); 209 final String url = "https://www.wikidata.org/w/api.php" + 210 "?action=wbgetentities" + 211 "&props=labels" + 212 "&ids=" + wikidataId + 213 "&languages=" + preferredLanguage + 214 "&languagefallback=en" + 215 "&format=xml"; 216 Main.info("Wikipedia: GET " + url); 217 try (final InputStream in = Utils.openURL(new URL(url))) { 218 final Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in); 219 final Node label = (Node) XPathFactory.newInstance().newXPath().compile("//label").evaluate(xml, XPathConstants.NODE); 220 if (label == null) { 221 return null; 222 } else { 223 return (String) XPathFactory.newInstance().newXPath().compile("./@value").evaluate(label, XPathConstants.STRING); 224 } 225 } 226 } catch (Exception ex) { 227 throw new RuntimeException(ex); 228 } 229 } 230 203 231 static Collection<WikipediaLangArticle> getInterwikiArticles(String wikipediaLang, String article) { 204 232 try { -
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaPlugin.java
r31852 r31857 21 21 if (newFrame != null) { 22 22 newFrame.addToggleDialog(new WikipediaToggleDialog()); 23 newFrame.propertiesDialog.addCustomPropertiesCellRenderer(new WikidataTagCellRenderer()); 23 24 } 24 25 } -
applications/editors/josm/plugins/wikipedia/test/unit/org/wikipedia/WikipediaAppTest.java
r31852 r31857 143 143 assertThat(map.size(), is(4)); 144 144 } 145 146 @Test 147 public void testGetLabelForWikidata() throws Exception { 148 assertThat(WikipediaApp.getLabelForWikidata("Q1741", "de"), is("Wien")); 149 assertThat(WikipediaApp.getLabelForWikidata("Q1741", "en"), is("Vienna")); 150 assertThat(WikipediaApp.getLabelForWikidata("Q" + Long.MAX_VALUE, "en"), nullValue()); 151 } 152 153 @Test(expected = RuntimeException.class) 154 public void testGetLabelForWikidataInvalidId() throws Exception { 155 WikipediaApp.getLabelForWikidata("Qxyz", "en"); 156 } 145 157 }
Note:
See TracChangeset
for help on using the changeset viewer.