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

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

fix squid:S1319 - Declarations should use Java collection interfaces rather than specific implementation classes

  • Property svn:eol-style set to native
File size: 3.5 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.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:*={0}. Add tag with correct language key.", name), NAME_TRANSLATION_MISSING, p));
54 }
55
56 /**
57 * Check a primitive for a name mismatch.
58 *
59 * @param p The primitive to be tested
60 */
61 @Override
62 public void check(OsmPrimitive p) {
63 Set<String> names = new HashSet<>();
64
65 for (Entry<String, String> entry : p.getKeys().entrySet()) {
66 if (entry.getKey().startsWith("name:")) {
67 String n = entry.getValue();
68 if (n != null) {
69 names.add(n);
70 }
71 }
72 }
73
74 if (names.isEmpty()) return;
75
76 String name = p.get("name");
77
78 if (name == null) {
79 errors.add(new TestError(this, Severity.OTHER,
80 tr("A name is missing, even though name:* exists."),
81 NAME_MISSING, p));
82 return;
83 }
84
85 if (names.contains(name)) return;
86 /* If name is not equal to one of the name:*, it should be a
87 composition of some (not necessarily all) name:* labels.
88 Check if this is the case. */
89
90 String[] splitNames = NAME_SPLIT_PATTERN.split(name);
91 if (splitNames.length == 1) {
92 /* The name is not composed of multiple parts. Complain. */
93 missingTranslation(p, splitNames[0]);
94 return;
95 }
96
97 /* Check that each part corresponds to a translated name:*. */
98 for (String n : splitNames) {
99 if (!names.contains(n)) {
100 missingTranslation(p, n);
101 }
102 }
103 }
104}
Note: See TracBrowser for help on using the repository browser.