1 | // License: GPL. See LICENSE file for details.
|
---|
2 | package org.openstreetmap.josm.data.validation.tests;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.HashSet;
|
---|
7 | import java.util.Map.Entry;
|
---|
8 | import java.util.regex.Pattern;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
11 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
12 | import org.openstreetmap.josm.data.validation.Test;
|
---|
13 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Check for missing name:* translations.
|
---|
17 | * <p>
|
---|
18 | * This test finds multilingual objects whose 'name' attribute is not
|
---|
19 | * equal to any 'name:*' attribute and not a composition of some
|
---|
20 | * 'name:*' attributes separated by ' - '.
|
---|
21 | * <p>
|
---|
22 | * For example, a node with name=Europe, name:de=Europa should have
|
---|
23 | * name:en=Europe to avoid triggering this test. An object with
|
---|
24 | * name='Suomi - Finland' should have at least name:fi=Suomi and
|
---|
25 | * name:sv=Finland to avoid a warning (name:et=Soome would not
|
---|
26 | * matter). Also, complain if an object has some name:* attribute but
|
---|
27 | * no name.
|
---|
28 | *
|
---|
29 | * @author Skela
|
---|
30 | */
|
---|
31 | public class NameMismatch extends Test.TagTest {
|
---|
32 | protected static final int NAME_MISSING = 1501;
|
---|
33 | protected static final int NAME_TRANSLATION_MISSING = 1502;
|
---|
34 | private static final Pattern NAME_SPLIT_PATTERN = Pattern.compile(" - ");
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Constructs a new {@code NameMismatch} test.
|
---|
38 | */
|
---|
39 | public NameMismatch() {
|
---|
40 | super(tr("Missing name:* translation"),
|
---|
41 | tr("This test finds multilingual objects whose ''name'' attribute is not equal to some ''name:*'' attribute and not a composition of ''name:*'' attributes, e.g., Italia - Italien - Italy."));
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Report a missing translation.
|
---|
46 | *
|
---|
47 | * @param p The primitive whose translation is missing
|
---|
48 | */
|
---|
49 | private void missingTranslation(OsmPrimitive p) {
|
---|
50 | errors.add(new TestError(this, Severity.OTHER,
|
---|
51 | tr("A name:* translation is missing."),
|
---|
52 | NAME_TRANSLATION_MISSING, p));
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Check a primitive for a name mismatch.
|
---|
57 | *
|
---|
58 | * @param p The primitive to be tested
|
---|
59 | */
|
---|
60 | @Override
|
---|
61 | public void check(OsmPrimitive p) {
|
---|
62 | HashSet<String> names = new HashSet<>();
|
---|
63 |
|
---|
64 | for (Entry<String, String> entry : p.getKeys().entrySet()) {
|
---|
65 | if (entry.getKey().startsWith("name:")) {
|
---|
66 | String n = entry.getValue();
|
---|
67 | if (n != null) {
|
---|
68 | names.add(n);
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | if (names.isEmpty()) return;
|
---|
74 |
|
---|
75 | String name = p.get("name");
|
---|
76 |
|
---|
77 | if (name == null) {
|
---|
78 | errors.add(new TestError(this, Severity.OTHER,
|
---|
79 | tr("A name is missing, even though name:* exists."),
|
---|
80 | NAME_MISSING, p));
|
---|
81 | return;
|
---|
82 | }
|
---|
83 |
|
---|
84 | if (names.contains(name)) return;
|
---|
85 | /* If name is not equal to one of the name:*, it should be a
|
---|
86 | composition of some (not necessarily all) name:* labels.
|
---|
87 | Check if this is the case. */
|
---|
88 |
|
---|
89 | String[] splitNames = NAME_SPLIT_PATTERN.split(name);
|
---|
90 | if (splitNames.length == 1) {
|
---|
91 | /* The name is not composed of multiple parts. Complain. */
|
---|
92 | missingTranslation(p);
|
---|
93 | return;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /* Check that each part corresponds to a translated name:*. */
|
---|
97 | for (String n : splitNames) {
|
---|
98 | if (!names.contains(n)) {
|
---|
99 | missingTranslation(p);
|
---|
100 | return;
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|