- Timestamp:
- 2015-10-01T21:06:10+02:00 (9 years ago)
- Location:
- trunk
- Files:
-
- 15 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/search/SearchAction.java
r8540 r8811 286 286 if (buttonIndex == 0) { 287 287 try { 288 SearchCompiler.compile(hcbSearchString.getText(), caseSensitive.isSelected(), regexSearch.isSelected()); 288 SearchSetting ss = new SearchSetting(); 289 ss.text = hcbSearchString.getText(); 290 ss.caseSensitive = caseSensitive.isSelected(); 291 ss.regexSearch = regexSearch.isSelected(); 292 SearchCompiler.compile(ss); 289 293 super.buttonAction(buttonIndex, evt); 290 294 } catch (ParseError e) { … … 454 458 int foundMatches = 0; 455 459 try { 456 String searchText = s.text; 457 SearchCompiler.Match matcher = SearchCompiler.compile(searchText, s.caseSensitive, s.regexSearch); 460 SearchCompiler.Match matcher = SearchCompiler.compile(s); 458 461 459 462 if (s.mode == SearchMode.replace) { … … 508 511 public static void getSelection(SearchSetting s, Collection<OsmPrimitive> all, Property<OsmPrimitive, Boolean> p) { 509 512 try { 510 String searchText = s.text;511 513 if (s instanceof Filter && ((Filter) s).inverted) { 512 searchText = String.format("-(%s)", searchText); 513 } 514 SearchCompiler.Match matcher = SearchCompiler.compile(searchText, s.caseSensitive, s.regexSearch); 514 s = new SearchSetting(s); 515 s.text = String.format("-(%s)", s.text); 516 } 517 SearchCompiler.Match matcher = SearchCompiler.compile(s); 515 518 516 519 for (OsmPrimitive osm : all) { … … 540 543 541 544 public static void search(String search, SearchMode mode) { 542 search(new SearchSetting(search, mode, false, false, false)); 545 final SearchSetting searchSetting = new SearchSetting(); 546 searchSetting.text = search; 547 searchSetting.mode = mode; 548 search(searchSetting); 543 549 } 544 550 … … 579 585 580 586 public static class SearchSetting { 581 public String text; 582 public SearchMode mode; 587 public String text = ""; 588 public SearchMode mode = SearchMode.replace; 583 589 public boolean caseSensitive; 584 590 public boolean regexSearch; … … 589 595 */ 590 596 public SearchSetting() { 591 this("", SearchMode.replace, false /* case insensitive */,592 false /* no regexp */, false /* only useful primitives */);593 }594 595 public SearchSetting(String text, SearchMode mode, boolean caseSensitive,596 boolean regexSearch, boolean allElements) {597 this.caseSensitive = caseSensitive;598 this.regexSearch = regexSearch;599 this.allElements = allElements;600 this.mode = mode;601 this.text = text;602 597 } 603 598 604 599 public SearchSetting(SearchSetting original) { 605 this(original.text, original.mode, original.caseSensitive, 606 original.regexSearch, original.allElements); 600 text = original.text; 601 mode = original.mode; 602 caseSensitive = original.caseSensitive; 603 regexSearch = original.regexSearch; 604 allElements = original.allElements; 607 605 } 608 606 … … 612 610 /*case sensitive*/ trc("search", "CS") : 613 611 /*case insensitive*/ trc("search", "CI"); 614 612 String rx = regexSearch ? ", " + 615 613 /*regex search*/ trc("search", "RX") : ""; 616 614 String all = allElements ? ", " + 617 615 /*all elements*/ trc("search", "A") : ""; 618 616 return "\"" + text + "\" (" + cs + rx + all + ", " + mode + ")"; 619 617 } 620 618 -
trunk/src/org/openstreetmap/josm/actions/search/SearchCompiler.java
r8781 r8811 1402 1402 } 1403 1403 1404 public static Match compile(String searchStr, boolean caseSensitive, boolean regexSearch) throws ParseError { 1405 return new SearchCompiler(caseSensitive, regexSearch, 1404 /** 1405 * Compiles the search expression. 1406 * @param searchStr the search expression 1407 * @return a {@link Match} object for the expression 1408 * @throws ParseError if an error has been encountered while compiling 1409 * @see #compile(org.openstreetmap.josm.actions.search.SearchAction.SearchSetting) 1410 */ 1411 public static Match compile(String searchStr) throws ParseError { 1412 return new SearchCompiler(false, false, 1406 1413 new PushbackTokenizer( 1407 1414 new PushbackReader(new StringReader(searchStr)))) 1408 .parse(); 1415 .parse(); 1416 } 1417 1418 /** 1419 * Compiles the search expression. 1420 * @param setting the settings to use 1421 * @return a {@link Match} object for the expression 1422 * @throws ParseError if an error has been encountered while compiling 1423 * @see #compile(String) 1424 */ 1425 public static Match compile(SearchAction.SearchSetting setting) throws ParseError { 1426 return new SearchCompiler(setting.caseSensitive, setting.regexSearch, 1427 new PushbackTokenizer( 1428 new PushbackReader(new StringReader(setting.text)))) 1429 .parse(); 1409 1430 } 1410 1431 -
trunk/src/org/openstreetmap/josm/data/osm/Filter.java
r7083 r8811 22 22 */ 23 23 public Filter() { 24 super("", SearchMode.add, false, false, false); 25 } 26 27 public Filter(String text, SearchMode mode, boolean caseSensitive, 28 boolean regexSearch, boolean allElements) { 29 super(text, mode, caseSensitive, regexSearch, allElements); 24 super(); 25 mode = SearchMode.add; 30 26 } 31 27 32 28 public Filter(FilterPreferenceEntry e) { 33 super(e.text, SearchMode.add, false, false, false); 29 this(); 30 text = e.text; 34 31 if ("replace".equals(e.mode)) { 35 32 mode = SearchMode.replace; -
trunk/src/org/openstreetmap/josm/data/osm/FilterMatcher.java
r8510 r8811 84 84 } 85 85 86 Match compiled = SearchCompiler.compile(filter .text, filter.caseSensitive, filter.regexSearch);86 Match compiled = SearchCompiler.compile(filter); 87 87 this.match = filter.inverted ? new Not(compiled) : compiled; 88 88 this.isInverted = filter.inverted; -
trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitive.java
r8767 r8811 795 795 796 796 try { 797 reversedDirectionKeys = SearchCompiler.compile(Main.pref.get("tags.reversed_direction", reversedDirectionDefault) , false, false);797 reversedDirectionKeys = SearchCompiler.compile(Main.pref.get("tags.reversed_direction", reversedDirectionDefault)); 798 798 } catch (ParseError e) { 799 799 Main.error("Unable to compile pattern for tags.reversed_direction, trying default pattern: " + e.getMessage()); 800 800 801 801 try { 802 reversedDirectionKeys = SearchCompiler.compile(reversedDirectionDefault , false, false);802 reversedDirectionKeys = SearchCompiler.compile(reversedDirectionDefault); 803 803 } catch (ParseError e2) { 804 804 throw new AssertionError("Unable to compile default pattern for direction keys: " + e2.getMessage(), e2); … … 806 806 } 807 807 try { 808 directionKeys = SearchCompiler.compile(Main.pref.get("tags.direction", directionDefault) , false, false);808 directionKeys = SearchCompiler.compile(Main.pref.get("tags.direction", directionDefault)); 809 809 } catch (ParseError e) { 810 810 Main.error("Unable to compile pattern for tags.direction, trying default pattern: " + e.getMessage()); 811 811 812 812 try { 813 directionKeys = SearchCompiler.compile(directionDefault , false, false);813 directionKeys = SearchCompiler.compile(directionDefault); 814 814 } catch (ParseError e2) { 815 815 throw new AssertionError("Unable to compile default pattern for direction keys: " + e2.getMessage(), e2); -
trunk/src/org/openstreetmap/josm/gui/dialogs/RelationListDialog.java
r8540 r8811 289 289 f.setBackground(UIManager.getColor("TextField.background")); 290 290 f.setToolTipText(tr("Relation list filter")); 291 model.setFilter(SearchCompiler.compile(filter.getText() , false, false));291 model.setFilter(SearchCompiler.compile(filter.getText())); 292 292 } catch (SearchCompiler.ParseError ex) { 293 293 f.setBackground(new Color(255, 224, 224)); -
trunk/src/org/openstreetmap/josm/gui/dialogs/properties/PropertiesDialog.java
r8693 r8811 1323 1323 } 1324 1324 1325 SearchSetting ss = new SearchSetting(s.toString(), SearchMode.replace, true, false, false); 1325 final SearchSetting ss = new SearchSetting(); 1326 ss.text = s.toString(); 1327 ss.caseSensitive = true; 1326 1328 org.openstreetmap.josm.actions.search.SearchAction.searchWithoutHistory(ss); 1327 1329 } -
trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/ExpressionFactory.java
r8795 r8811 664 664 Match m; 665 665 try { 666 m = SearchCompiler.compile(searchStr , false, false);666 m = SearchCompiler.compile(searchStr); 667 667 } catch (ParseError ex) { 668 668 return null; -
trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPreset.java
r8710 r8811 202 202 public void setName_template_filter(String filter) throws SAXException { 203 203 try { 204 this.nameTemplateFilter = SearchCompiler.compile(filter , false, false);204 this.nameTemplateFilter = SearchCompiler.compile(filter); 205 205 } catch (org.openstreetmap.josm.actions.search.SearchCompiler.ParseError e) { 206 206 Main.error("Error while parsing" + filter + ": " + e.getMessage()); -
trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPresetItems.java
r8786 r8811 52 52 53 53 import org.openstreetmap.josm.Main; 54 import org.openstreetmap.josm.actions.search.SearchAction; 54 55 import org.openstreetmap.josm.actions.search.SearchCompiler; 55 56 import org.openstreetmap.josm.data.osm.OsmPrimitive; … … 204 205 public void setMember_expression(String member_expression) throws SAXException { 205 206 try { 206 this.memberExpression = SearchCompiler.compile(member_expression, true, true); 207 final SearchAction.SearchSetting searchSetting = new SearchAction.SearchSetting(); 208 searchSetting.text = member_expression; 209 searchSetting.caseSensitive = true; 210 searchSetting.regexSearch = true; 211 this.memberExpression = SearchCompiler.compile(searchSetting); 207 212 } catch (SearchCompiler.ParseError ex) { 208 213 throw new SAXException(tr("Illegal member expression: {0}", ex.getMessage()), ex); -
trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/LoadAndZoomHandler.java
r8510 r8811 196 196 } else if (args.containsKey("search") && PermissionPrefWithDefault.CHANGE_SELECTION.isAllowed()) { 197 197 try { 198 final SearchCompiler.Match search = SearchCompiler.compile(args.get("search") , false, false);198 final SearchCompiler.Match search = SearchCompiler.compile(args.get("search")); 199 199 Main.worker.submit(new Runnable() { 200 200 @Override -
trunk/src/org/openstreetmap/josm/tools/template_engine/TemplateParser.java
r8509 r8811 90 90 try { 91 91 result.getEntries().add(new SearchExpressionCondition( 92 SearchCompiler.compile(searchExpression.getText() , false, false), condition));92 SearchCompiler.compile(searchExpression.getText()), condition)); 93 93 } catch (org.openstreetmap.josm.actions.search.SearchCompiler.ParseError e) { 94 94 throw new ParseError(searchExpression.getPosition(), e); … … 119 119 else { 120 120 try { 121 Match match = SearchCompiler.compile(searchExpression.getText() , false, false);121 Match match = SearchCompiler.compile(searchExpression.getText()); 122 122 result = new ContextSwitchTemplate(match, template, searchExpression.getPosition()); 123 123 } catch (org.openstreetmap.josm.actions.search.SearchCompiler.ParseError e) { -
trunk/test/unit/org/openstreetmap/josm/actions/CreateMultipolygonActionTest.groovy
r7938 r8811 5 5 import org.openstreetmap.josm.JOSMFixture 6 6 import org.openstreetmap.josm.TestUtils 7 import org.openstreetmap.josm.actions.search.SearchAction 7 8 import org.openstreetmap.josm.actions.search.SearchCompiler 8 9 import org.openstreetmap.josm.data.osm.Relation … … 26 27 } 27 28 29 static def regexpSearch(String search) { 30 def setting = new SearchAction.SearchSetting() 31 setting.text = search 32 setting.regexSearch = true 33 return setting 34 } 35 28 36 @Test 29 37 public void testCreate1() { … … 37 45 public void testCreate2() { 38 46 def ds = OsmReader.parseDataSet(new FileInputStream(TestUtils.getTestDataRoot() + "create_multipolygon.osm"), null); 39 def ways = Utils.filter(ds.getWays(), SearchCompiler.compile("ref=1 OR ref:1.1." , false, false))47 def ways = Utils.filter(ds.getWays(), SearchCompiler.compile("ref=1 OR ref:1.1.")) 40 48 def mp = CreateMultipolygonAction.createMultipolygonCommand(ways as Collection<Way>, null) 41 49 assert getRefToRoleMap(mp.b).toString() == "[1:outer, 1.1.1:inner, 1.1.2:inner]" … … 45 53 public void testUpdate1() { 46 54 def ds = OsmReader.parseDataSet(new FileInputStream(TestUtils.getTestDataRoot() + "create_multipolygon.osm"), null); 47 def ways = Utils.filter(ds.getWays(), SearchCompiler.compile("ref=\".*1\$\"" , false, true))55 def ways = Utils.filter(ds.getWays(), SearchCompiler.compile(regexpSearch("ref=\".*1\$\""))) 48 56 def mp = CreateMultipolygonAction.createMultipolygonCommand(ways as Collection<Way>, null) 49 57 assert mp.b.getMembersCount() == 3 50 58 assert getRefToRoleMap(mp.b).toString() == "[1:outer, 1.1:inner, 1.1.1:outer]" 51 def ways2 = Utils.filter(ds.getWays(), SearchCompiler.compile("ref=1.2" , false, true))59 def ways2 = Utils.filter(ds.getWays(), SearchCompiler.compile(regexpSearch("ref=1.2"))) 52 60 def mp2 = CreateMultipolygonAction.createMultipolygonCommand(ways2 as Collection<Way>, mp.b) 53 61 assert mp2.b.getMembersCount() == 4 … … 58 66 public void testUpdate2() { 59 67 def ds = OsmReader.parseDataSet(new FileInputStream(TestUtils.getTestDataRoot() + "create_multipolygon.osm"), null); 60 def ways = Utils.filter(ds.getWays(), SearchCompiler.compile("ref=1 OR ref:1.1.1" , false, false))68 def ways = Utils.filter(ds.getWays(), SearchCompiler.compile("ref=1 OR ref:1.1.1")) 61 69 def mp = CreateMultipolygonAction.createMultipolygonCommand(ways as Collection<Way>, null) 62 70 assert getRefToRoleMap(mp.b).toString() == "[1:outer, 1.1.1:inner]" 63 def ways2 = Utils.filter(ds.getWays(), SearchCompiler.compile("ref=1.1 OR ref=1.2 OR ref=1.1.2" , false, true))71 def ways2 = Utils.filter(ds.getWays(), SearchCompiler.compile(regexpSearch("ref=1.1 OR ref=1.2 OR ref=1.1.2"))) 64 72 def mp2 = CreateMultipolygonAction.createMultipolygonCommand(ways2 as Collection<Way>, mp.b) 65 73 assert getRefToRoleMap(mp2.b).toString() == "[1:outer, 1.1:inner, 1.1.1:outer, 1.1.2:outer, 1.2:inner]" -
trunk/test/unit/org/openstreetmap/josm/actions/search/SearchCompilerTest.java
r8509 r8811 31 31 @Test 32 32 public void testAny() throws Exception { 33 final SearchCompiler.Match c = SearchCompiler.compile("foo" , false, false);33 final SearchCompiler.Match c = SearchCompiler.compile("foo"); 34 34 Assert.assertTrue(c.match(newPrimitive("foobar", "true"))); 35 35 Assert.assertTrue(c.match(newPrimitive("name", "hello-foo-xy"))); … … 39 39 @Test 40 40 public void testEquals() throws Exception { 41 final SearchCompiler.Match c = SearchCompiler.compile("foo=bar" , false, false);41 final SearchCompiler.Match c = SearchCompiler.compile("foo=bar"); 42 42 Assert.assertFalse(c.match(newPrimitive("foobar", "true"))); 43 43 Assert.assertTrue(c.match(newPrimitive("foo", "bar"))); … … 48 48 @Test 49 49 public void testCompare() throws Exception { 50 final SearchCompiler.Match c1 = SearchCompiler.compile("start_date>1950" , false, false);50 final SearchCompiler.Match c1 = SearchCompiler.compile("start_date>1950"); 51 51 Assert.assertTrue(c1.match(newPrimitive("start_date", "1950-01-01"))); 52 52 Assert.assertTrue(c1.match(newPrimitive("start_date", "1960"))); … … 54 54 Assert.assertFalse(c1.match(newPrimitive("start_date", "1000"))); 55 55 Assert.assertTrue(c1.match(newPrimitive("start_date", "101010"))); 56 final SearchCompiler.Match c2 = SearchCompiler.compile("start_date<1960" , false, false);56 final SearchCompiler.Match c2 = SearchCompiler.compile("start_date<1960"); 57 57 Assert.assertTrue(c2.match(newPrimitive("start_date", "1950-01-01"))); 58 58 Assert.assertFalse(c2.match(newPrimitive("start_date", "1960"))); … … 60 60 Assert.assertTrue(c2.match(newPrimitive("start_date", "1000"))); 61 61 Assert.assertTrue(c2.match(newPrimitive("start_date", "200"))); 62 final SearchCompiler.Match c3 = SearchCompiler.compile("name<I" , false, false);62 final SearchCompiler.Match c3 = SearchCompiler.compile("name<I"); 63 63 Assert.assertTrue(c3.match(newPrimitive("name", "Alpha"))); 64 64 Assert.assertFalse(c3.match(newPrimitive("name", "Sigma"))); 65 final SearchCompiler.Match c4 = SearchCompiler.compile("\"start_date\"<1960" , false, false);65 final SearchCompiler.Match c4 = SearchCompiler.compile("\"start_date\"<1960"); 66 66 Assert.assertTrue(c4.match(newPrimitive("start_date", "1950-01-01"))); 67 67 Assert.assertFalse(c4.match(newPrimitive("start_date", "2000"))); … … 83 83 way.addNode(node1); 84 84 way.addNode(node2); 85 Assert.assertFalse(SearchCompiler.compile("nth:2" , false, false).match(node1));86 Assert.assertTrue(SearchCompiler.compile("nth:1" , false, false).match(node1));87 Assert.assertFalse(SearchCompiler.compile("nth:0" , false, false).match(node1));88 Assert.assertTrue(SearchCompiler.compile("nth:0" , false, false).match(node0));89 Assert.assertTrue(SearchCompiler.compile("nth:2" , false, false).match(node2));90 Assert.assertTrue(SearchCompiler.compile("nth:-1" , false, false).match(node2));91 Assert.assertTrue(SearchCompiler.compile("nth:-2" , false, false).match(node1));92 Assert.assertTrue(SearchCompiler.compile("nth:-3" , false, false).match(node0));85 Assert.assertFalse(SearchCompiler.compile("nth:2").match(node1)); 86 Assert.assertTrue(SearchCompiler.compile("nth:1").match(node1)); 87 Assert.assertFalse(SearchCompiler.compile("nth:0").match(node1)); 88 Assert.assertTrue(SearchCompiler.compile("nth:0").match(node0)); 89 Assert.assertTrue(SearchCompiler.compile("nth:2").match(node2)); 90 Assert.assertTrue(SearchCompiler.compile("nth:-1").match(node2)); 91 Assert.assertTrue(SearchCompiler.compile("nth:-2").match(node1)); 92 Assert.assertTrue(SearchCompiler.compile("nth:-3").match(node0)); 93 93 } 94 94 95 95 @Test 96 96 public void testNthParseNegative() throws Exception { 97 Assert.assertThat(SearchCompiler.compile("nth:-1" , false, false).toString(), CoreMatchers.is("Nth{nth=-1, modulo=false}"));97 Assert.assertThat(SearchCompiler.compile("nth:-1").toString(), CoreMatchers.is("Nth{nth=-1, modulo=false}")); 98 98 99 99 } -
trunk/test/unit/org/openstreetmap/josm/tools/template_engine/TemplateEngineTest.java
r8540 r8811 61 61 62 62 private static Match compile(String expression) throws org.openstreetmap.josm.actions.search.SearchCompiler.ParseError { 63 return SearchCompiler.compile(expression , false, false);63 return SearchCompiler.compile(expression); 64 64 } 65 65
Note:
See TracChangeset
for help on using the changeset viewer.