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.Collection;
|
---|
7 | import java.util.HashSet;
|
---|
8 | import java.util.Map.Entry;
|
---|
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 {
|
---|
32 | protected static final int NAME_MISSING = 1501;
|
---|
33 | protected static final int NAME_TRANSLATION_MISSING = 1502;
|
---|
34 |
|
---|
35 | public NameMismatch() {
|
---|
36 | super(tr("Missing name:* translation."),
|
---|
37 | 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."));
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Report a missing translation.
|
---|
42 | *
|
---|
43 | * @param p The primitive whose translation is missing
|
---|
44 | */
|
---|
45 | private void missingTranslation(OsmPrimitive p) {
|
---|
46 | errors.add(new TestError(this, Severity.OTHER,
|
---|
47 | tr("A name:* translation is missing."),
|
---|
48 | NAME_TRANSLATION_MISSING, p));
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Check a primitive for a name mismatch.
|
---|
53 | *
|
---|
54 | * @param p The primitive to be tested
|
---|
55 | */
|
---|
56 | public void check(OsmPrimitive p) {
|
---|
57 | HashSet<String> names = new HashSet<String>();
|
---|
58 |
|
---|
59 | for (Entry<String, String> entry : p.getKeys().entrySet()) {
|
---|
60 | if (entry.getKey().startsWith("name:")) {
|
---|
61 | String name_s = entry.getValue();
|
---|
62 | if (name_s != null) {
|
---|
63 | names.add(name_s);
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | if (names.isEmpty()) return;
|
---|
69 |
|
---|
70 | String name = p.get("name");
|
---|
71 |
|
---|
72 | if (name == null) {
|
---|
73 | errors.add(new TestError(this, Severity.OTHER,
|
---|
74 | tr("A name is missing, even though name:* exists."),
|
---|
75 | NAME_MISSING, p));
|
---|
76 | return;
|
---|
77 | }
|
---|
78 |
|
---|
79 | if (names.contains(name)) return;
|
---|
80 | /* If name is not equal to one of the name:*, it should be a
|
---|
81 | composition of some (not necessarily all) name:* labels.
|
---|
82 | Check if this is the case. */
|
---|
83 |
|
---|
84 | String split_names[] = name.split(" - ");
|
---|
85 | if (split_names.length == 1) {
|
---|
86 | /* The name is not composed of multiple parts. Complain. */
|
---|
87 | missingTranslation(p);
|
---|
88 | return;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /* Check that each part corresponds to a translated name:*. */
|
---|
92 | for (String n : split_names) {
|
---|
93 | if (!names.contains(n)) {
|
---|
94 | missingTranslation(p);
|
---|
95 | return;
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Checks a name mismatch in all primitives.
|
---|
102 | *
|
---|
103 | * @param selection The primitives to be tested
|
---|
104 | */
|
---|
105 | @Override public void visit(Collection<OsmPrimitive> selection) {
|
---|
106 | for (OsmPrimitive p : selection)
|
---|
107 | if (!p.isDeleted() && !p.isIncomplete())
|
---|
108 | check(p);
|
---|
109 | }
|
---|
110 | }
|
---|