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

Last change on this file since 12318 was 11191, checked in by Don-vip, 7 years ago

sonar - fix some recent warnings

  • 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.marktr;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.util.HashSet;
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 " +
43 "and not a composition of ''name:*'' attributes, e.g., Italia - Italien - Italy."));
44 }
45
46 /**
47 * Report a missing translation.
48 *
49 * @param p The primitive whose translation is missing
50 * @param name The name whose translation is missing
51 */
52 private void missingTranslation(OsmPrimitive p, String name) {
53 errors.add(TestError.builder(this, Severity.OTHER, NAME_TRANSLATION_MISSING)
54 .message(tr("Missing name:* translation"), marktr("Missing name:*={0}. Add tag with correct language key."), name)
55 .primitives(p)
56 .build());
57 }
58
59 /**
60 * Check a primitive for a name mismatch.
61 *
62 * @param p The primitive to be tested
63 */
64 @Override
65 public void check(OsmPrimitive p) {
66 Set<String> names = new HashSet<>();
67
68 p.getKeys().forEach((key, n) -> {
69 if (n != null && key.startsWith("name:") && !"name:etymology:wikidata".equals(key)) {
70 names.add(n);
71 }
72 });
73
74 if (names.isEmpty()) return;
75
76 String name = p.get("name");
77
78 if (name == null) {
79 errors.add(TestError.builder(this, Severity.OTHER, NAME_MISSING)
80 .message(tr("A name is missing, even though name:* exists."))
81 .primitives(p)
82 .build());
83 return;
84 }
85
86 if (names.contains(name)) return;
87 /* If name is not equal to one of the name:*, it should be a
88 composition of some (not necessarily all) name:* labels.
89 Check if this is the case. */
90
91 String[] splitNames = NAME_SPLIT_PATTERN.split(name);
92 if (splitNames.length == 1) {
93 /* The name is not composed of multiple parts. Complain. */
94 missingTranslation(p, splitNames[0]);
95 return;
96 }
97
98 /* Check that each part corresponds to a translated name:*. */
99 for (String n : splitNames) {
100 if (!names.contains(n)) {
101 missingTranslation(p, n);
102 }
103 }
104 }
105}
Note: See TracBrowser for help on using the repository browser.