Ticket #20037: 20037-fix.patch
File 20037-fix.patch, 5.8 KB (added by , 3 years ago) |
---|
-
src/org/openstreetmap/josm/gui/download/OverpassQueryWizardDialog.java
90 90 */ 91 91 private Optional<String> tryParseSearchTerm(String searchTerm) { 92 92 try { 93 return Optional.of(SearchCompilerQueryWizard. getInstance().constructQuery(searchTerm));93 return Optional.of(SearchCompilerQueryWizard.constructQuery(searchTerm)); 94 94 } catch (UncheckedParseException | IllegalStateException ex) { 95 95 Logging.error(ex); 96 96 JOptionPane.showMessageDialog( -
src/org/openstreetmap/josm/tools/SearchCompilerQueryWizard.java
25 25 */ 26 26 public final class SearchCompilerQueryWizard { 27 27 28 private static final SearchCompilerQueryWizard instance = new SearchCompilerQueryWizard();29 30 /**31 * Replies the unique instance of this class.32 *33 * @return the unique instance of this class34 */35 public static SearchCompilerQueryWizard getInstance() {36 return instance;37 }38 39 28 private SearchCompilerQueryWizard() { 40 29 // private constructor for utility class 41 30 } … … 46 35 * @return an Overpass QL query 47 36 * @throws UncheckedParseException when the parsing fails 48 37 */ 49 public String constructQuery(final String search) {38 public static String constructQuery(final String search) { 50 39 try { 51 40 Matcher matcher = Pattern.compile("\\s+GLOBAL\\s*$", Pattern.CASE_INSENSITIVE).matcher(search); 52 41 if (matcher.find()) { … … 73 62 throw new IllegalStateException(mode); 74 63 } 75 64 } 76 65 77 66 final Match match = SearchCompiler.compile(search); 78 67 return constructQuery(match, "[bbox:{{bbox}}];", ""); 79 68 } catch (SearchParseError | UnsupportedOperationException e) { … … 81 70 } 82 71 } 83 72 84 private String constructQuery(final Match match, final String bounds, final String queryLineSuffix) {73 private static String constructQuery(final Match match, final String bounds, final String queryLineSuffix) { 85 74 final List<Match> normalized = normalizeToDNF(match); 86 75 final List<String> queryLines = new ArrayList<>(); 87 76 queryLines.add("[out:xml][timeout:90]" + bounds); … … 135 124 return "[" + (negated ? "!" : "") + quote(key) + "]"; 136 125 case EXACT: 137 126 return "[" + quote(key) + (negated ? "!=" : "=") + quote(value) + "]"; 127 case ANY_KEY: // *=value 128 // fall through 138 129 case EXACT_REGEXP: 139 130 final Matcher matcher = Pattern.compile("/(?<regex>.*)/(?<flags>i)?").matcher(value); 140 131 final String valueQuery = matcher.matches() 141 132 ? quote(matcher.group("regex")) + Optional.ofNullable(matcher.group("flags")).map(f -> "," + f).orElse("") 142 133 : quote(value); 134 if (mode == SearchCompiler.ExactKeyValue.Mode.ANY_KEY) 135 return "[~\"^.*$\"" + (negated ? "!~" : "~") + valueQuery + "]"; 143 136 return "[" + quote(key) + (negated ? "!~" : "~") + valueQuery + "]"; 144 137 case MISSING_KEY: 145 138 // special case for empty values, see https://github.com/drolbr/Overpass-API/issues/53 -
test/unit/org/openstreetmap/josm/io/OverpassDownloadReaderTest.java
58 58 } 59 59 60 60 private String getExpandedQuery(String search) { 61 final String query = SearchCompilerQueryWizard. getInstance().constructQuery(search);61 final String query = SearchCompilerQueryWizard.constructQuery(search); 62 62 final String request = new OverpassDownloadReader(new Bounds(1, 2, 3, 4), null, query) 63 63 .getRequestForBbox(1, 2, 3, 4) 64 64 .substring("interpreter?data=".length()); -
test/unit/org/openstreetmap/josm/tools/SearchCompilerQueryWizardTest.java
23 23 public JOSMTestRules test = new JOSMTestRules().i18n("de"); 24 24 25 25 private static String constructQuery(String s) { 26 return SearchCompilerQueryWizard. getInstance().constructQuery(s);26 return SearchCompilerQueryWizard.constructQuery(s); 27 27 } 28 28 29 29 private void assertQueryEquals(String expectedQueryPart, String input) { … … 240 240 assertQueryEquals(" relation[\"type\"=\"multipolygon\"][!\"landuse\"][!\"area:highway\"];\n", 241 241 "type:relation and type=multipolygon and -landuse=* and -\"area:highway\"=*"); 242 242 } 243 244 /** 245 * Test for ticket <a href="https://josm.openstreetmap.de/ticket/20037>#20037</a> 246 */ 247 @Test 248 void testTicket20037() { 249 assertQueryEquals( 250 " node[~\"^.*$\"~\"forward\"];\n" + 251 " node[~\"^.*$\"~\"backward\"];\n", 252 "type:node AND (*=forward OR *=backward)"); 253 } 243 254 }