- Timestamp:
- 2009-11-23T20:55:56+01:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/search/SearchCompiler.java
r2407 r2510 31 31 private boolean caseSensitive = false; 32 32 private boolean regexSearch = false; 33 private String rxErrorMsg = marktr("The regex \"{0}\" had a parse error at offset {1}, full error:\n\n{2}");33 private static String rxErrorMsg = marktr("The regex \"{0}\" had a parse error at offset {1}, full error:\n\n{2}"); 34 34 private PushbackTokenizer tokenizer; 35 35 private CollectBackReferencesVisitor childBackRefs; … … 43 43 44 44 abstract public static class Match { 45 abstract public boolean match(OsmPrimitive osm) throws ParseError;45 abstract public boolean match(OsmPrimitive osm); 46 46 } 47 47 … … 55 55 private final Match match; 56 56 public Not(Match match) {this.match = match;} 57 @Override public boolean match(OsmPrimitive osm) throws ParseError{57 @Override public boolean match(OsmPrimitive osm) { 58 58 return !match.match(osm); 59 59 } … … 65 65 private Match rhs; 66 66 public And(Match lhs, Match rhs) {this.lhs = lhs; this.rhs = rhs;} 67 @Override public boolean match(OsmPrimitive osm) throws ParseError{67 @Override public boolean match(OsmPrimitive osm) { 68 68 return lhs.match(osm) && rhs.match(osm); 69 69 } … … 75 75 private Match rhs; 76 76 public Or(Match lhs, Match rhs) {this.lhs = lhs; this.rhs = rhs;} 77 @Override public boolean match(OsmPrimitive osm) throws ParseError{77 @Override public boolean match(OsmPrimitive osm) { 78 78 return lhs.match(osm) || rhs.match(osm); 79 79 } … … 90 90 } 91 91 92 private class KeyValue extends Match { 93 private String key; 94 private String value; 95 public KeyValue(String key, String value) {this.key = key; this.value = value; } 96 @Override public boolean match(OsmPrimitive osm) throws ParseError { 97 92 private static class KeyValue extends Match { 93 private final String key; 94 private final Pattern keyPattern; 95 private final String value; 96 private final Pattern valuePattern; 97 private final boolean caseSensitive; 98 99 public KeyValue(String key, String value, boolean regexSearch, boolean caseSensitive) throws ParseError { 100 this.caseSensitive = caseSensitive; 98 101 if (regexSearch) { 102 int searchFlags = regexFlags(caseSensitive); 103 104 try { 105 this.keyPattern = Pattern.compile(key, searchFlags); 106 this.valuePattern = Pattern.compile(value, searchFlags); 107 } catch (PatternSyntaxException e) { 108 throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage())); 109 } 110 this.key = key; 111 this.value = value; 112 113 } else if (caseSensitive) { 114 this.key = key; 115 this.value = value; 116 this.keyPattern = null; 117 this.valuePattern = null; 118 } else { 119 this.key = key.toLowerCase(); 120 this.value = value; 121 this.keyPattern = null; 122 this.valuePattern = null; 123 } 124 } 125 126 @Override public boolean match(OsmPrimitive osm) { 127 128 if (keyPattern != null) { 99 129 if (!osm.hasKeys()) 100 130 return false; … … 107 137 */ 108 138 109 Pattern searchKey = null;110 Pattern searchValue = null;111 int searchFlags = regexFlags();112 113 try {114 searchKey = Pattern.compile(key, searchFlags);115 searchValue = Pattern.compile(value, searchFlags);116 } catch (PatternSyntaxException e) {117 throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));118 }119 120 139 for (Entry<String, String> e : osm.entrySet()) { 121 140 String k = e.getKey(); 122 141 String v = e.getValue(); 123 142 124 Matcher matcherKey = searchKey.matcher(k);143 Matcher matcherKey = keyPattern.matcher(k); 125 144 boolean matchedKey = matcherKey.find(); 126 145 127 146 if (matchedKey) { 128 Matcher matcherValue = searchValue.matcher(v);147 Matcher matcherValue = valuePattern.matcher(v); 129 148 boolean matchedValue = matcherValue.find(); 130 149 … … 146 165 147 166 String v1 = caseSensitive ? value : value.toLowerCase(); 148 String v2 = caseSensitive ? this.value : this.value.toLowerCase();149 167 150 168 // is not Java 1.5 151 169 //v1 = java.text.Normalizer.normalize(v1, java.text.Normalizer.Form.NFC); 152 170 //v2 = java.text.Normalizer.normalize(v2, java.text.Normalizer.Form.NFC); 153 return v1.indexOf(v 2) != -1;171 return v1.indexOf(value) != -1; 154 172 } 155 173 … … 224 242 225 243 @Override 226 public boolean match(OsmPrimitive osm) throws ParseError{244 public boolean match(OsmPrimitive osm) { 227 245 228 246 if (!osm.hasKeys()) … … 279 297 } 280 298 281 private class Any extends Match { 282 private String s; 283 public Any(String s) {this.s = s;} 284 @Override public boolean match(OsmPrimitive osm) throws ParseError { 285 if (!osm.hasKeys()) 286 return s.equals(""); 287 288 String search; 289 Pattern searchRegex = null; 290 299 private static class Any extends Match { 300 private final String search; 301 private final Pattern searchRegex; 302 private final boolean caseSensitive; 303 304 public Any(String s, boolean regexSearch, boolean caseSensitive) throws ParseError { 305 this.caseSensitive = caseSensitive; 291 306 if (regexSearch) { 292 search = s;293 int searchFlags = regexFlags();294 295 307 try { 296 searchRegex = Pattern.compile(search, searchFlags);308 this.searchRegex = Pattern.compile(s, regexFlags(caseSensitive)); 297 309 } catch (PatternSyntaxException e) { 298 310 throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage())); 299 311 } 300 } else { 301 search = caseSensitive ? s : s.toLowerCase(); 302 } 312 this.search = s; 313 } else if (caseSensitive) { 314 this.search = s; 315 this.searchRegex = null; 316 } else { 317 this.search = s.toLowerCase(); 318 this.searchRegex = null; 319 } 320 } 321 322 @Override public boolean match(OsmPrimitive osm) { 323 if (!osm.hasKeys()) 324 return search.equals(""); 303 325 304 326 // is not Java 1.5 305 327 //search = java.text.Normalizer.normalize(search, java.text.Normalizer.Form.NFC); 306 328 for (Entry<String, String> e : osm.entrySet()) { 307 if ( regexSearch) {329 if (searchRegex != null) { 308 330 String key = e.getKey(); 309 331 String value = e.getValue(); … … 343 365 return false; 344 366 } 345 @Override public String toString() {return s;} 367 @Override public String toString() { 368 return search; 369 } 346 370 } 347 371 … … 475 499 private Match child; 476 500 public Parent(Match m) { child = m; } 477 @Override public boolean match(OsmPrimitive osm) throws ParseError{501 @Override public boolean match(OsmPrimitive osm) { 478 502 boolean isParent = false; 479 503 … … 516 540 } 517 541 518 @Override public boolean match(OsmPrimitive osm) throws ParseError{542 @Override public boolean match(OsmPrimitive osm) { 519 543 boolean isChild = false; 520 544 childBackRefs.initialize(); … … 631 655 return new Parent(parseParens()); 632 656 else 633 return new Any(tok );657 return new Any(tok, regexSearch, caseSensitive); 634 658 } 635 659 … … 639 663 else if (key.equals("user")) 640 664 return new UserMatch(value); 641 else if (key.equals("tags")) 665 else if (key.equals("tags")) { 642 666 try { 643 667 String[] range = value.split("-"); … … 651 675 throw new ParseError(tr("Incorrect value of tags operator: {0}. Tags operator expects number of tags or range, for example tags:1 or tags:2-5", value)); 652 676 } 653 else if (key.equals("nodes")) {677 } else if (key.equals("nodes")) { 654 678 try { 655 679 String[] range = value.split("-"); … … 671 695 } 672 696 } else 673 return new KeyValue(key, value );674 } 675 676 private int regexFlags() {697 return new KeyValue(key, value, regexSearch, caseSensitive); 698 } 699 700 private static int regexFlags(boolean caseSensitive) { 677 701 int searchFlags = 0; 678 702
Note:
See TracChangeset
for help on using the changeset viewer.