1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.corrector;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.regex.Matcher;
|
---|
8 | import java.util.regex.Pattern;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
11 | import org.openstreetmap.josm.data.osm.Way;
|
---|
12 |
|
---|
13 | public class ReverseWayTagCorrector extends TagCorrector<Way> {
|
---|
14 |
|
---|
15 | private static final Pattern leftRightStartRegex = Pattern.compile(
|
---|
16 | "^(left|right):.*", Pattern.CASE_INSENSITIVE);
|
---|
17 |
|
---|
18 | private static final Pattern leftRightEndRegex = Pattern.compile(
|
---|
19 | ".*:(left|right)$", Pattern.CASE_INSENSITIVE);
|
---|
20 |
|
---|
21 | @Override public boolean execute(Way way) {
|
---|
22 |
|
---|
23 | ArrayList<TagCorrection> tagCorrections = new ArrayList<TagCorrection>();
|
---|
24 |
|
---|
25 | for (String key : way.keySet()) {
|
---|
26 | String newKey = key;
|
---|
27 | String value = way.get(key);
|
---|
28 | String newValue = value;
|
---|
29 |
|
---|
30 | if (key.equals("oneway")) {
|
---|
31 | if (value.equals("-1"))
|
---|
32 | newValue = OsmUtils.trueval;
|
---|
33 | else {
|
---|
34 | Boolean boolValue = OsmUtils.getOsmBoolean(value);
|
---|
35 | if (boolValue != null && boolValue.booleanValue()) {
|
---|
36 | newValue = "-1";
|
---|
37 | }
|
---|
38 | }
|
---|
39 | } else {
|
---|
40 | Matcher m = leftRightStartRegex.matcher(key);
|
---|
41 | if (!m.matches())
|
---|
42 | m = leftRightEndRegex.matcher(key);
|
---|
43 |
|
---|
44 | if (m.matches()) {
|
---|
45 | String leftRight = m.group(1).toLowerCase();
|
---|
46 |
|
---|
47 | newKey = key.substring(0, m.start(1)).concat(
|
---|
48 | leftRight.equals("left") ? "right" : "left")
|
---|
49 | .concat(key.substring(m.end(1)));
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | if (key != newKey || value != newValue)
|
---|
54 | tagCorrections.add(new TagCorrection(key, value, newKey,
|
---|
55 | newValue));
|
---|
56 | }
|
---|
57 |
|
---|
58 | return applyCorrections(tagCorrections, way,
|
---|
59 | tr("When reverting this way, following changes to the "
|
---|
60 | + "properties are suggested in order to maintain "
|
---|
61 | + "data consistency."));
|
---|
62 | }
|
---|
63 | }
|
---|