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

Last change on this file since 1706 was 1706, checked in by stoecker, 15 years ago

fixed some design issues

File size: 5.3 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 if (m.lookingAt()) {
50 String leftRight = m.group(1).toLowerCase();
51
52 StringBuilder result = new StringBuilder();
53 result.append(text.substring(0, m.start(1)));
54 result.append(leftRight.equals(a) ? b : a);
55 result.append(text.substring(m.end(1)));
56
57 return result.toString();
58 }
59 return text;
60 }
61 }
62
63 private static PrefixSuffixSwitcher[] prefixSuffixSwitchers =
64 new PrefixSuffixSwitcher[] {
65 new PrefixSuffixSwitcher("left", "right"),
66 new PrefixSuffixSwitcher("forward", "backward"),
67 new PrefixSuffixSwitcher("forwards", "backwards")
68 };
69
70 @Override
71 public Collection<Command> execute(Way oldway, Way way) throws UserCancelException {
72 Map<OsmPrimitive, List<TagCorrection>> tagCorrectionsMap =
73 new HashMap<OsmPrimitive, List<TagCorrection>>();
74
75 ArrayList<OsmPrimitive> primitives = new ArrayList<OsmPrimitive>();
76 primitives.add(way);
77 primitives.addAll(way.nodes);
78
79 for (OsmPrimitive primitive : primitives) {
80 tagCorrectionsMap.put(primitive, new ArrayList<TagCorrection>());
81
82 for (String key : primitive.keySet()) {
83 String newKey = key;
84 String value = primitive.get(key);
85 String newValue = value;
86
87 if (key.equals("oneway")) {
88 if (value.equals("-1"))
89 newValue = OsmUtils.trueval;
90 else {
91 Boolean boolValue = OsmUtils.getOsmBoolean(value);
92 if (boolValue != null && boolValue.booleanValue()) {
93 newValue = "-1";
94 }
95 }
96 } else {
97 for (PrefixSuffixSwitcher prefixSuffixSwitcher : prefixSuffixSwitchers) {
98 newKey = prefixSuffixSwitcher.apply(key);
99 if (!key.equals(newKey))
100 break;
101 }
102 }
103
104 if (!key.equals(newKey) || !value.equals(newValue))
105 tagCorrectionsMap.get(primitive).add(
106 new TagCorrection(key, value, newKey, newValue));
107 }
108 }
109
110 Map<OsmPrimitive, List<RoleCorrection>> roleCorrectionMap =
111 new HashMap<OsmPrimitive, List<RoleCorrection>>();
112 roleCorrectionMap.put(way, new ArrayList<RoleCorrection>());
113
114 for (Relation relation : Main.ds.relations) {
115 int position = 0;
116 for (RelationMember member : relation.members) {
117 if (!member.member.realEqual(oldway, true)
118 || member.role.length() == 0) {
119 position++;
120 continue;
121 }
122
123 boolean found = false;
124 String newRole = null;
125 for (PrefixSuffixSwitcher prefixSuffixSwitcher : prefixSuffixSwitchers) {
126 newRole = prefixSuffixSwitcher.apply(member.role);
127 if (!newRole.equals(member.role)) {
128 found = true;
129 break;
130 }
131 }
132
133 if (found)
134 roleCorrectionMap.get(way).add(
135 new RoleCorrection(relation, position, member, newRole));
136
137 position++;
138 }
139 }
140
141 return applyCorrections(tagCorrectionsMap, roleCorrectionMap,
142 tr("When reversing this way, the following changes to properties "
143 + "of the way and its nodes are suggested in order "
144 + "to maintain data consistency."));
145 }
146}
Note: See TracBrowser for help on using the repository browser.