source: josm/trunk/src/org/openstreetmap/josm/data/validation/tests/NameMismatch.java@ 8455

Last change on this file since 8455 was 8455, checked in by Don-vip, 9 years ago

fix #11484 - group "missing name" and "long segments" validation results

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