Ignore:
Timestamp:
2015-12-25T20:03:27+01:00 (9 years ago)
Author:
simon04
Message:

JOSM/wikipedia: Demystify Wikidata tags - see #josm9775

Location:
applications/editors/josm/plugins/wikipedia
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/wikipedia/build.xml

    r31114 r31857  
    55    <property name="commit.message" value="Commit message"/>
    66    <!-- 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"/>
    88    <property name="plugin.canloadatruntime" value="true"/>
    99
  • applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaApp.java

    r31852 r31857  
    2929import org.openstreetmap.josm.data.osm.OsmPrimitive;
    3030import org.openstreetmap.josm.data.osm.Tag;
     31import org.openstreetmap.josm.tools.CheckParameterUtil;
    3132import org.openstreetmap.josm.tools.Predicate;
    3233import org.openstreetmap.josm.tools.Utils;
     
    3738
    3839public final class WikipediaApp {
     40
     41    public static Pattern WIKIDATA_PATTERN = Pattern.compile("Q\\d+");
    3942
    4043    private WikipediaApp() {
     
    201204    }
    202205
     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
    203231    static Collection<WikipediaLangArticle> getInterwikiArticles(String wikipediaLang, String article) {
    204232        try {
  • applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaPlugin.java

    r31852 r31857  
    2121        if (newFrame != null) {
    2222            newFrame.addToggleDialog(new WikipediaToggleDialog());
     23            newFrame.propertiesDialog.addCustomPropertiesCellRenderer(new WikidataTagCellRenderer());
    2324        }
    2425    }
  • applications/editors/josm/plugins/wikipedia/test/unit/org/wikipedia/WikipediaAppTest.java

    r31852 r31857  
    143143        assertThat(map.size(), is(4));
    144144    }
     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    }
    145157}
Note: See TracChangeset for help on using the changeset viewer.