source: josm/trunk/src/org/openstreetmap/josm/tools/OverpassTurboQueryWizard.java@ 9419

Last change on this file since 9419 was 9385, checked in by simon04, 8 years ago

Refactoring: introduce UncheckedParseException

In addition, DateUtils#fromString does no longer return "now" when
date cannot be parsed, but throws an UncheckedParseException instead.

File size: 2.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.io.IOException;
5import java.io.InputStreamReader;
6import java.io.Reader;
7import java.nio.charset.StandardCharsets;
8import java.util.regex.Pattern;
9
10import javax.script.Invocable;
11import javax.script.ScriptEngine;
12import javax.script.ScriptEngineManager;
13import javax.script.ScriptException;
14
15/**
16 * Uses <a href="https://github.com/tyrasd/overpass-turbo/">Overpass Turbo</a> query wizard code
17 * to build an Overpass QL from a {@link org.openstreetmap.josm.actions.search.SearchAction} like query.
18 *
19 * Requires a JavaScript {@link ScriptEngine}.
20 * @since 8744
21 */
22public final class OverpassTurboQueryWizard {
23
24 private static OverpassTurboQueryWizard instance;
25 private final ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
26
27 /**
28 * Replies the unique instance of this class.
29 *
30 * @return the unique instance of this class
31 */
32 public static synchronized OverpassTurboQueryWizard getInstance() {
33 if (instance == null) {
34 instance = new OverpassTurboQueryWizard();
35 }
36 return instance;
37 }
38
39 private OverpassTurboQueryWizard() {
40 // overpass-turbo is MIT Licensed
41
42 try (final Reader reader = new InputStreamReader(
43 getClass().getResourceAsStream("/data/overpass-turbo-ffs.js"), StandardCharsets.UTF_8)) {
44 engine.eval("var console = {log: function(){}};");
45 engine.eval(reader);
46 engine.eval("var construct_query = turbo.ffs().construct_query;");
47 } catch (ScriptException | IOException ex) {
48 throw new RuntimeException("Failed to initialize OverpassTurboQueryWizard", ex);
49 }
50 }
51
52 /**
53 * Builds an Overpass QL from a {@link org.openstreetmap.josm.actions.search.SearchAction} like query.
54 * @param search the {@link org.openstreetmap.josm.actions.search.SearchAction} like query
55 * @return an Overpass QL query
56 * @throws UncheckedParseException when the parsing fails
57 */
58 public String constructQuery(String search) throws UncheckedParseException {
59 try {
60 final Object result = ((Invocable) engine).invokeFunction("construct_query", search);
61 if (result == Boolean.FALSE) {
62 throw new UncheckedParseException();
63 }
64 String query = (String) result;
65 query = Pattern.compile("^.*\\[out:json\\]", Pattern.DOTALL).matcher(query).replaceFirst("");
66 query = Pattern.compile("^out.*", Pattern.MULTILINE).matcher(query).replaceAll("out meta;");
67 query = query.replace("({{bbox}})", "");
68 return query;
69 } catch (NoSuchMethodException e) {
70 throw new IllegalStateException();
71 } catch (ScriptException e) {
72 throw new RuntimeException("Failed to execute OverpassTurboQueryWizard", e);
73 }
74 }
75
76}
Note: See TracBrowser for help on using the repository browser.