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

Last change on this file since 7489 was 7005, checked in by Don-vip, 10 years ago

see #8465 - use diamond operator where applicable

  • Property svn:eol-style set to native
File size: 3.4 KB
Line 
1// License: GPL. See LICENSE file for details.
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.regex.Pattern;
9
10import org.openstreetmap.josm.data.osm.OsmPrimitive;
11import org.openstreetmap.josm.data.validation.Severity;
12import org.openstreetmap.josm.data.validation.Test;
13import 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 */
31public 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}
Note: See TracBrowser for help on using the repository browser.