Ticket #20037: 20037.2.patch

File 20037.2.patch, 2.5 KB (added by GerdP, 5 years ago)

like this? Fixes also a SonarLint issue

  • src/org/openstreetmap/josm/tools/SearchCompilerQueryWizard.java

     
    4646     * @return an Overpass QL query
    4747     * @throws UncheckedParseException when the parsing fails
    4848     */
    49     public String constructQuery(final String search) {
     49    public static String constructQuery(final String search) {
    5050        try {
    5151            Matcher matcher = Pattern.compile("\\s+GLOBAL\\s*$", Pattern.CASE_INSENSITIVE).matcher(search);
    5252            if (matcher.find()) {
     
    7373                    throw new IllegalStateException(mode);
    7474                }
    7575            }
    76            
     76
    7777            final Match match = SearchCompiler.compile(search);
    7878            return constructQuery(match, "[bbox:{{bbox}}];", "");
    7979        } catch (SearchParseError | UnsupportedOperationException e) {
     
    8181        }
    8282    }
    8383
    84     private String constructQuery(final Match match, final String bounds, final String queryLineSuffix) {
     84    private static String constructQuery(final Match match, final String bounds, final String queryLineSuffix) {
    8585        final List<Match> normalized = normalizeToDNF(match);
    8686        final List<String> queryLines = new ArrayList<>();
    8787        queryLines.add("[out:xml][timeout:90]" + bounds);
     
    135135                    return "[" + (negated ? "!" : "") + quote(key) + "]";
    136136                case EXACT:
    137137                    return "[" + quote(key) + (negated ? "!=" : "=") + quote(value) + "]";
     138                case ANY_KEY: // *=value
     139                    // fall through
    138140                case EXACT_REGEXP:
    139141                    final Matcher matcher = Pattern.compile("/(?<regex>.*)/(?<flags>i)?").matcher(value);
    140142                    final String valueQuery = matcher.matches()
    141143                            ? quote(matcher.group("regex")) + Optional.ofNullable(matcher.group("flags")).map(f -> "," + f).orElse("")
    142144                            : quote(value);
     145                    if (mode == SearchCompiler.ExactKeyValue.Mode.ANY_KEY)
     146                        return "[~\"^.*$\"" + (negated ? "!~" : "~") + valueQuery + "]";
    143147                    return "[" + quote(key) + (negated ? "!~" : "~") + valueQuery + "]";
    144148                case MISSING_KEY:
    145149                    // special case for empty values, see https://github.com/drolbr/Overpass-API/issues/53