Changeset 32001 in osm for applications/editors/josm/plugins/wikipedia
- Timestamp:
- 2016-01-18T12:43:17+01:00 (9 years ago)
- Location:
- applications/editors/josm/plugins/wikipedia
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikidataTagCellRenderer.java
r31915 r32001 7 7 import java.util.Collections; 8 8 import java.util.List; 9 import java.util.Locale; 9 10 import java.util.Map; 10 11 import java.util.concurrent.Callable; … … 37 38 @Override 38 39 public String call() throws Exception { 39 final String label = WikipediaApp.getLabelForWikidata(id, L anguageInfo.getJOSMLocaleCode());40 final String label = WikipediaApp.getLabelForWikidata(id, Locale.getDefault()); 40 41 table.repaint(); 41 42 table = null; -
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaApp.java
r31907 r32001 12 12 import java.util.HashMap; 13 13 import java.util.List; 14 import java.util.Locale; 14 15 import java.util.Map; 15 16 import java.util.Objects; … … 47 48 48 49 private WikipediaApp() { 50 } 51 52 static String getMediawikiLocale(Locale locale) { 53 if (!locale.getCountry().isEmpty()) { 54 return locale.getLanguage() + "-" + locale.getCountry().toLowerCase(); 55 } else { 56 return locale.getLanguage(); 57 } 49 58 } 50 59 … … 206 215 } 207 216 208 static String getLabelForWikidata(String wikidataId, StringpreferredLanguage) {217 static String getLabelForWikidata(String wikidataId, Locale locale, String ... preferredLanguage) { 209 218 try { 210 219 CheckParameterUtil.ensureThat(WIKIDATA_PATTERN.matcher(wikidataId).matches(), "Invalid Wikidata ID given"); … … 213 222 "&props=labels" + 214 223 "&ids=" + wikidataId + 215 "&format=xml" + 216 (preferredLanguage != null ? "&languages=" + preferredLanguage + "&languagefallback=en" : ""); 224 "&format=xml"; 225 final Collection<String> languages = new ArrayList<>(); 226 if (locale != null) { 227 languages.add(getMediawikiLocale(locale)); 228 languages.add(getMediawikiLocale(new Locale(locale.getLanguage()))); 229 } 230 languages.addAll(Arrays.asList(preferredLanguage)); 231 languages.add("en"); 217 232 try (final InputStream in = HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContent()) { 218 233 final Document xml = DOCUMENT_BUILDER.parse(in); 219 f inal Node label = (Node) X_PATH.compile("//label").evaluate(xml, XPathConstants.NODE);220 if (label == null && preferredLanguage != null) {221 return getLabelForWikidata(wikidataId, null);222 } else if (label == null) {223 return null;224 } else {225 return (String) X_PATH.compile("./@value").evaluate(label, XPathConstants.STRING);226 }234 for (String language : languages) { 235 final String label = (String) X_PATH.compile("//label[@language='" + language + "']/@value") 236 .evaluate(xml, XPathConstants.STRING); 237 if (label != null && !label.isEmpty()) { 238 return label; 239 } 240 } 241 return null; 227 242 } 228 243 } catch (Exception ex) { -
applications/editors/josm/plugins/wikipedia/test/unit/org/wikipedia/WikipediaAppTest.java
r31907 r32001 13 13 import java.util.Collection; 14 14 import java.util.List; 15 import java.util.Locale; 15 16 import java.util.Map; 16 17 … … 25 26 public void setUp() throws Exception { 26 27 Main.initApplicationPreferences(); 28 } 29 30 @Test 31 public void testMediawikiLocale() throws Exception { 32 assertThat(WikipediaApp.getMediawikiLocale(Locale.GERMANY), is("de-de")); 33 assertThat(WikipediaApp.getMediawikiLocale(Locale.GERMAN), is("de")); 34 assertThat(WikipediaApp.getMediawikiLocale(Locale.UK), is("en-gb")); 35 assertThat(WikipediaApp.getMediawikiLocale(Locale.CANADA), is("en-ca")); 27 36 } 28 37 … … 146 155 @Test 147 156 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());157 assertThat(WikipediaApp.getLabelForWikidata("Q1741", Locale.GERMAN), is("Wien")); 158 assertThat(WikipediaApp.getLabelForWikidata("Q1741", Locale.ENGLISH), is("Vienna")); 159 assertThat(WikipediaApp.getLabelForWikidata("Q" + Long.MAX_VALUE, Locale.ENGLISH), nullValue()); 151 160 } 152 161 153 162 @Test(expected = RuntimeException.class) 154 163 public void testGetLabelForWikidataInvalidId() throws Exception { 155 WikipediaApp.getLabelForWikidata("Qxyz", "en");164 WikipediaApp.getLabelForWikidata("Qxyz", Locale.ENGLISH); 156 165 } 157 166
Note:
See TracChangeset
for help on using the changeset viewer.