Commit c5561d27 authored by Florian Schäfer's avatar Florian Schäfer

Use the RegexUtil for checking the Wikidata Q-ID

parent 78435a6e
......@@ -39,11 +39,11 @@ import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.wikipedia.data.WikidataEntry;
import org.wikipedia.data.WikipediaEntry;
import org.wikipedia.tools.RegexUtil;
import org.wikipedia.tools.XPath;
public final class WikipediaApp {
public static final Pattern WIKIDATA_PATTERN = Pattern.compile("Q\\d+");
private static final XPath X_PATH = XPath.getInstance();
private final String wikipediaLang;
private final String siteId;
......@@ -311,7 +311,7 @@ public final class WikipediaApp {
X_PATH.evaluateNodes("//entity", xml).forEach(node -> {
final String wikidata = X_PATH.evaluateString("./@id", node);
final String wikipedia = X_PATH.evaluateString("./sitelinks/sitelink/@title", node);
if (WIKIDATA_PATTERN.matcher(wikidata).matches()) { // non existing entries result in negative integers
if (RegexUtil.isValidQId(wikidata)) { // non existing entries result in negative integers
r.put(wikipedia, wikidata);
}
});
......
......@@ -7,6 +7,7 @@ import org.openstreetmap.josm.tools.AlphanumComparator;
import org.openstreetmap.josm.tools.CheckParameterUtil;
import org.openstreetmap.josm.tools.Utils;
import org.wikipedia.WikipediaApp;
import org.wikipedia.tools.RegexUtil;
import java.util.Comparator;
import java.util.Optional;
......@@ -44,7 +45,7 @@ public class WikidataEntry extends WikipediaEntry {
}
private static void ensureValidWikidataId(String id) {
CheckParameterUtil.ensureThat(WikipediaApp.WIKIDATA_PATTERN.matcher(id).matches(), "Invalid Wikidata ID given: " + id);
CheckParameterUtil.ensureThat(RegexUtil.isValidQId(id), "Invalid Wikidata ID given: " + id);
}
@Override
......
......@@ -23,6 +23,7 @@ import org.openstreetmap.josm.tools.Logging;
import org.openstreetmap.josm.tools.Utils;
import org.wikipedia.WikipediaApp;
import org.wikipedia.data.WikidataEntry;
import org.wikipedia.tools.RegexUtil;
public class WikidataTagCellRenderer extends DefaultTableCellRenderer {
......@@ -41,11 +42,11 @@ public class WikidataTagCellRenderer extends DefaultTableCellRenderer {
final String id = ((Map<?, ?>) value).keySet().iterator().next().toString();
final JLabel component = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (WikipediaApp.WIKIDATA_PATTERN.matcher(id).matches()) {
if (RegexUtil.isValidQId(id)) {
return renderValues(Collections.singleton(id), table, component);
} else if (id.contains(";")) {
final List<String> ids = Arrays.asList(id.split("\\s*;\\s*"));
if (ids.stream().allMatch(i -> WikipediaApp.WIKIDATA_PATTERN.matcher(i).matches())) {
if (ids.stream().allMatch(RegexUtil::isValidQId)) {
return renderValues(ids, table, component);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment