source: josm/trunk/src/org/openstreetmap/josm/corrector/ReverseWayTagCorrector.java@ 1990

Last change on this file since 1990 was 1938, checked in by jttt, 15 years ago

Replace some occurrences of RelationMember.member with getters

File size: 5.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.corrector;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.ArrayList;
7import java.util.Collection;
8import java.util.HashMap;
9import java.util.List;
10import java.util.Map;
11import java.util.regex.Matcher;
12import java.util.regex.Pattern;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.command.Command;
16import org.openstreetmap.josm.data.osm.OsmPrimitive;
17import org.openstreetmap.josm.data.osm.OsmUtils;
18import org.openstreetmap.josm.data.osm.Relation;
19import org.openstreetmap.josm.data.osm.RelationMember;
20import org.openstreetmap.josm.data.osm.Way;
21
22public class ReverseWayTagCorrector extends TagCorrector<Way> {
23
24 private static class PrefixSuffixSwitcher {
25
26 private final String a;
27 private final String b;
28 private final Pattern startPattern;
29 private final Pattern endPattern;
30
31 private final String SEPARATOR = "[:_]?";
32
33 public PrefixSuffixSwitcher(String a, String b) {
34 this.a = a;
35 this.b = b;
36 startPattern = Pattern.compile(
37 "^(" + a + "|" + b + ")(" + SEPARATOR + "|$)",
38 Pattern.CASE_INSENSITIVE);
39 endPattern = Pattern.compile("^.*" +
40 SEPARATOR + "(" + a + "|" + b + ")$",
41 Pattern.CASE_INSENSITIVE);
42 }
43
44 public String apply(String text) {
45 Matcher m = startPattern.matcher(text);
46 if (!m.lookingAt()) {
47 m = endPattern.matcher(text);
48 }
49
50 if (m.lookingAt()) {
51 String leftRight = m.group(1).toLowerCase();
52
53 StringBuilder result = new StringBuilder();
54 result.append(text.substring(0, m.start(1)));
55 result.append(leftRight.equals(a) ? b : a);
56 result.append(text.substring(m.end(1)));
57
58 return result.toString();
59 }
60 return text;
61 }
62 }
63
64 private static PrefixSuffixSwitcher[] prefixSuffixSwitchers =
65 new PrefixSuffixSwitcher[] {
66 new PrefixSuffixSwitcher("left", "right"),
67 new PrefixSuffixSwitcher("forward", "backward"),
68 new PrefixSuffixSwitcher("forwards", "backwards")
69 };
70
71 @Override
72 public Collection<Command> execute(Way oldway, Way way) throws UserCancelException {
73 Map<OsmPrimitive, List<TagCorrection>> tagCorrectionsMap =
74 new HashMap<OsmPrimitive, List<TagCorrection>>();
75
76 ArrayList<OsmPrimitive> primitives = new ArrayList<OsmPrimitive>();
77 primitives.add(way);
78 primitives.addAll(way.getNodes());
79
80 for (OsmPrimitive primitive : primitives) {
81 tagCorrectionsMap.put(primitive, new ArrayList<TagCorrection>());
82
83 for (String key : primitive.keySet()) {
84 String newKey = key;
85 String value = primitive.get(key);
86 String newValue = value;
87
88 if (key.equals("oneway")) {
89 if (value.equals("-1")) {
90 newValue = OsmUtils.trueval;
91 } else {
92 Boolean boolValue = OsmUtils.getOsmBoolean(value);
93 if (boolValue != null && boolValue.booleanValue()) {
94 newValue = "-1";
95 }
96 }
97 } else {
98 for (PrefixSuffixSwitcher prefixSuffixSwitcher : prefixSuffixSwitchers) {
99 newKey = prefixSuffixSwitcher.apply(key);
100 if (!key.equals(newKey)) {
101 break;
102 }
103 }
104 }
105
106 if (!key.equals(newKey) || !value.equals(newValue)) {
107 tagCorrectionsMap.get(primitive).add(
108 new TagCorrection(key, value, newKey, newValue));
109 }
110 }
111 }
112
113 Map<OsmPrimitive, List<RoleCorrection>> roleCorrectionMap =
114 new HashMap<OsmPrimitive, List<RoleCorrection>>();
115 roleCorrectionMap.put(way, new ArrayList<RoleCorrection>());
116
117 for (Relation relation : Main.main.getCurrentDataSet().relations) {
118 int position = 0;
119 for (RelationMember member : relation.getMembers()) {
120 if (!member.getMember().hasEqualSemanticAttributes(oldway)
121 || !member.hasRole()) {
122 position++;
123 continue;
124 }
125
126 boolean found = false;
127 String newRole = null;
128 for (PrefixSuffixSwitcher prefixSuffixSwitcher : prefixSuffixSwitchers) {
129 newRole = prefixSuffixSwitcher.apply(member.getRole());
130 if (!newRole.equals(member.getRole())) {
131 found = true;
132 break;
133 }
134 }
135
136 if (found) {
137 roleCorrectionMap.get(way).add(
138 new RoleCorrection(relation, position, member, newRole));
139 }
140
141 position++;
142 }
143 }
144
145 return applyCorrections(tagCorrectionsMap, roleCorrectionMap,
146 tr("When reversing this way, the following changes to properties "
147 + "of the way and its nodes are suggested in order "
148 + "to maintain data consistency."));
149 }
150}
Note: See TracBrowser for help on using the repository browser.