1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions.search;
|
---|
3 |
|
---|
4 | import java.io.IOException;
|
---|
5 | import java.io.PushbackReader;
|
---|
6 | import java.io.StringReader;
|
---|
7 | import java.util.Map.Entry;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.data.osm.Node;
|
---|
10 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
11 | import org.openstreetmap.josm.data.osm.Segment;
|
---|
12 | import org.openstreetmap.josm.data.osm.Way;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Implements a google-like search.
|
---|
16 | * @author Imi
|
---|
17 | */
|
---|
18 | public class SearchCompiler {
|
---|
19 |
|
---|
20 | boolean caseSensitive = false;
|
---|
21 |
|
---|
22 | abstract public static class Match {
|
---|
23 | abstract public boolean match(OsmPrimitive osm);
|
---|
24 | }
|
---|
25 |
|
---|
26 | private static class Always extends Match {
|
---|
27 | @Override public boolean match(OsmPrimitive osm) {
|
---|
28 | return true;
|
---|
29 | }
|
---|
30 | }
|
---|
31 |
|
---|
32 | private static class Not extends Match {
|
---|
33 | private final Match match;
|
---|
34 | public Not(Match match) {this.match = match;}
|
---|
35 | @Override public boolean match(OsmPrimitive osm) {
|
---|
36 | return !match.match(osm);
|
---|
37 | }
|
---|
38 | @Override public String toString() {return "!"+match;}
|
---|
39 | }
|
---|
40 |
|
---|
41 | private static class And extends Match {
|
---|
42 | private Match lhs;
|
---|
43 | private Match rhs;
|
---|
44 | public And(Match lhs, Match rhs) {this.lhs = lhs; this.rhs = rhs;}
|
---|
45 | @Override public boolean match(OsmPrimitive osm) {
|
---|
46 | return lhs.match(osm) && rhs.match(osm);
|
---|
47 | }
|
---|
48 | @Override public String toString() {return lhs+" && "+rhs;}
|
---|
49 | }
|
---|
50 |
|
---|
51 | private static class Or extends Match {
|
---|
52 | private Match lhs;
|
---|
53 | private Match rhs;
|
---|
54 | public Or(Match lhs, Match rhs) {this.lhs = lhs; this.rhs = rhs;}
|
---|
55 | @Override public boolean match(OsmPrimitive osm) {
|
---|
56 | return lhs.match(osm) || rhs.match(osm);
|
---|
57 | }
|
---|
58 | @Override public String toString() {return lhs+" || "+rhs;}
|
---|
59 | }
|
---|
60 |
|
---|
61 | private static class Id extends Match {
|
---|
62 | private long id;
|
---|
63 | public Id(long id) {this.id = id;}
|
---|
64 | @Override public boolean match(OsmPrimitive osm) {
|
---|
65 | return osm.id == id;
|
---|
66 | }
|
---|
67 | @Override public String toString() {return "id="+id;}
|
---|
68 | }
|
---|
69 |
|
---|
70 | private class KeyValue extends Match {
|
---|
71 | private String key;
|
---|
72 | private String value;
|
---|
73 | boolean notValue;
|
---|
74 | public KeyValue(String key, String value, boolean notValue) {this.key = key; this.value = value; this.notValue = notValue;}
|
---|
75 | @Override public boolean match(OsmPrimitive osm) {
|
---|
76 | String value = null;
|
---|
77 | if (key.equals("timestamp"))
|
---|
78 | value = osm.getTimeStr();
|
---|
79 | else
|
---|
80 | value = osm.get(key);
|
---|
81 | if (value == null)
|
---|
82 | return notValue;
|
---|
83 | String v1 = caseSensitive ? value : value.toLowerCase();
|
---|
84 | String v2 = caseSensitive ? this.value : this.value.toLowerCase();
|
---|
85 | return (v1.indexOf(v2) != -1) != notValue;
|
---|
86 | }
|
---|
87 | @Override public String toString() {return key+"="+(notValue?"!":"")+value;}
|
---|
88 | }
|
---|
89 |
|
---|
90 | private class Any extends Match {
|
---|
91 | private String s;
|
---|
92 | public Any(String s) {this.s = s;}
|
---|
93 | @Override public boolean match(OsmPrimitive osm) {
|
---|
94 | if (osm.keys == null)
|
---|
95 | return s.equals("");
|
---|
96 | for (Entry<String, String> e : osm.keys.entrySet()) {
|
---|
97 | String key = caseSensitive ? e.getKey() : e.getKey().toLowerCase();
|
---|
98 | String value = caseSensitive ? e.getValue() : e.getValue().toLowerCase();
|
---|
99 | String search = caseSensitive ? s : s.toLowerCase();
|
---|
100 | if (key.indexOf(search) != -1 || value.indexOf(search) != -1)
|
---|
101 | return true;
|
---|
102 | }
|
---|
103 | return false;
|
---|
104 | }
|
---|
105 | @Override public String toString() {return s;}
|
---|
106 | }
|
---|
107 |
|
---|
108 | private static class ExactType extends Match {
|
---|
109 | private String type;
|
---|
110 | public ExactType(String type) {this.type = type;}
|
---|
111 | @Override public boolean match(OsmPrimitive osm) {
|
---|
112 | if (osm instanceof Node)
|
---|
113 | return type.equals("node");
|
---|
114 | if (osm instanceof Segment)
|
---|
115 | return type.equals("segment");
|
---|
116 | if (osm instanceof Way)
|
---|
117 | return type.equals("way");
|
---|
118 | throw new IllegalStateException("unknown class "+osm.getClass());
|
---|
119 | }
|
---|
120 | @Override public String toString() {return "type="+type;}
|
---|
121 | }
|
---|
122 |
|
---|
123 | private static class Modified extends Match {
|
---|
124 | @Override public boolean match(OsmPrimitive osm) {
|
---|
125 | return osm.modified;
|
---|
126 | }
|
---|
127 | @Override public String toString() {return "modified";}
|
---|
128 | }
|
---|
129 |
|
---|
130 | private static class Selected extends Match {
|
---|
131 | @Override public boolean match(OsmPrimitive osm) {
|
---|
132 | return osm.selected;
|
---|
133 | }
|
---|
134 | @Override public String toString() {return "selected";}
|
---|
135 | }
|
---|
136 |
|
---|
137 | private static class Incomplete extends Match {
|
---|
138 | @Override public boolean match(OsmPrimitive osm) {
|
---|
139 | return osm instanceof Way && ((Way)osm).isIncomplete();
|
---|
140 | }
|
---|
141 | @Override public String toString() {return "incomplete";}
|
---|
142 | }
|
---|
143 |
|
---|
144 | public static Match compile(String searchStr, boolean caseSensitive) {
|
---|
145 | SearchCompiler searchCompiler = new SearchCompiler();
|
---|
146 | searchCompiler.caseSensitive = caseSensitive;
|
---|
147 | return searchCompiler.parse(new PushbackReader(new StringReader(searchStr)));
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * The token returned is <code>null</code> or starts with an identifier character:
|
---|
153 | * - for an '-'. This will be the only character
|
---|
154 | * : for an key. The value is the next token
|
---|
155 | * | for "OR"
|
---|
156 | * ' ' for anything else.
|
---|
157 | * @return The next token in the stream.
|
---|
158 | */
|
---|
159 | private String nextToken(PushbackReader search) {
|
---|
160 | try {
|
---|
161 | int next;
|
---|
162 | char c = ' ';
|
---|
163 | while (c == ' ' || c == '\t' || c == '\n') {
|
---|
164 | next = search.read();
|
---|
165 | if (next == -1)
|
---|
166 | return null;
|
---|
167 | c = (char)next;
|
---|
168 | }
|
---|
169 | StringBuilder s;
|
---|
170 | switch (c) {
|
---|
171 | case '-':
|
---|
172 | return "-";
|
---|
173 | case '"':
|
---|
174 | s = new StringBuilder(" ");
|
---|
175 | for (int nc = search.read(); nc != -1 && nc != '"'; nc = search.read())
|
---|
176 | s.append((char)nc);
|
---|
177 | int nc = search.read();
|
---|
178 | if (nc != -1 && (char)nc == ':')
|
---|
179 | return ":"+s.toString();
|
---|
180 | if (nc != -1)
|
---|
181 | search.unread(nc);
|
---|
182 | return s.toString();
|
---|
183 | default:
|
---|
184 | s = new StringBuilder();
|
---|
185 | for (;;) {
|
---|
186 | s.append(c);
|
---|
187 | next = search.read();
|
---|
188 | if (next == -1) {
|
---|
189 | if (s.toString().equals("OR"))
|
---|
190 | return "|";
|
---|
191 | return " "+s.toString();
|
---|
192 | }
|
---|
193 | c = (char)next;
|
---|
194 | if (c == ' ' || c == '\t' || c == ':' || c == '"') {
|
---|
195 | if (c == ':')
|
---|
196 | return ":"+s.toString();
|
---|
197 | search.unread(next);
|
---|
198 | if (s.toString().equals("OR"))
|
---|
199 | return "|";
|
---|
200 | return " "+s.toString();
|
---|
201 | }
|
---|
202 | }
|
---|
203 | }
|
---|
204 | } catch (IOException e) {
|
---|
205 | throw new RuntimeException(e.getMessage(), e);
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|
210 | private boolean notKey = false;
|
---|
211 | private boolean notValue = false;
|
---|
212 | private boolean or = false;
|
---|
213 | private String key = null;
|
---|
214 | String token = null;
|
---|
215 | private Match build() {
|
---|
216 | String value = token.substring(1);
|
---|
217 | if (key == null) {
|
---|
218 | Match c = null;
|
---|
219 | if (value.equals("modified"))
|
---|
220 | c = new Modified();
|
---|
221 | else if (value.equals("incomplete"))
|
---|
222 | c = new Incomplete();
|
---|
223 | else if (value.equals("selected"))
|
---|
224 | c = new Selected();
|
---|
225 | else
|
---|
226 | c = new Any(value);
|
---|
227 | return notValue ? new Not(c) : c;
|
---|
228 | }
|
---|
229 | Match c;
|
---|
230 | if (key.equals("type"))
|
---|
231 | c = new ExactType(value);
|
---|
232 | else if (key.equals("property")) {
|
---|
233 | String realKey = "", realValue = value;
|
---|
234 | int eqPos = value.indexOf("=");
|
---|
235 | if (eqPos != -1) {
|
---|
236 | realKey = value.substring(0,eqPos);
|
---|
237 | realValue = value.substring(eqPos+1);
|
---|
238 | }
|
---|
239 | c = new KeyValue(realKey, realValue, notValue);
|
---|
240 | } else if (key.equals("id")) {
|
---|
241 | try {
|
---|
242 | c = new Id(Long.parseLong(value));
|
---|
243 | } catch (NumberFormatException x) {
|
---|
244 | c = new Id(0);
|
---|
245 | }
|
---|
246 | if (notValue)
|
---|
247 | c = new Not(c);
|
---|
248 | } else
|
---|
249 | c = new KeyValue(key, value, notValue);
|
---|
250 | if (notKey)
|
---|
251 | return new Not(c);
|
---|
252 | return c;
|
---|
253 | }
|
---|
254 |
|
---|
255 | private Match parse(PushbackReader search) {
|
---|
256 | Match result = null;
|
---|
257 | for (token = nextToken(search); token != null; token = nextToken(search)) {
|
---|
258 | if (token.equals("-"))
|
---|
259 | notValue = true;
|
---|
260 | else if (token.equals("|")) {
|
---|
261 | if (result == null)
|
---|
262 | continue;
|
---|
263 | or = true;
|
---|
264 | notValue = false;
|
---|
265 | } else if (token.startsWith(":")) {
|
---|
266 | if (key == null) {
|
---|
267 | key = token.substring(1);
|
---|
268 | notKey = notValue;
|
---|
269 | notValue = false;
|
---|
270 | } else
|
---|
271 | key += token.substring(1);
|
---|
272 | } else {
|
---|
273 | Match current = build();
|
---|
274 | if (result == null)
|
---|
275 | result = current;
|
---|
276 | else
|
---|
277 | result = or ? new Or(result, current) : new And(result, current);
|
---|
278 | key = null;
|
---|
279 | notKey = false;
|
---|
280 | notValue = false;
|
---|
281 | or = false;
|
---|
282 | }
|
---|
283 | }
|
---|
284 | // if "key:" was the last search
|
---|
285 | if (key != null) {
|
---|
286 | token = " ";
|
---|
287 | Match current = build();
|
---|
288 | result = (result == null) ? current : new And(result, current);
|
---|
289 | }
|
---|
290 | return result == null ? new Always() : result;
|
---|
291 | }
|
---|
292 | }
|
---|