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

Last change on this file since 14397 was 14324, checked in by Don-vip, 6 years ago

fix #16839 - increase default overpass wizard timeout from 25 to 90 seconds (can be configured with overpass.wizard.timeout)

File size: 3.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.io.IOException;
5import java.io.Reader;
6
7import javax.script.Invocable;
8import javax.script.ScriptEngine;
9import javax.script.ScriptException;
10
11import org.openstreetmap.josm.io.CachedFile;
12import org.openstreetmap.josm.spi.preferences.Config;
13
14/**
15 * Uses <a href="https://github.com/tyrasd/overpass-wizard/">Overpass Turbo query wizard</a> code (MIT Licensed)
16 * to build an Overpass QL from a {@link org.openstreetmap.josm.actions.search.SearchAction} like query.
17 *
18 * Requires a JavaScript {@link ScriptEngine}.
19 * @since 8744
20 */
21public final class OverpassTurboQueryWizard {
22
23 private static OverpassTurboQueryWizard instance;
24 private final ScriptEngine engine = Utils.getJavaScriptEngine();
25
26 /**
27 * Replies the unique instance of this class.
28 *
29 * @return the unique instance of this class
30 */
31 public static synchronized OverpassTurboQueryWizard getInstance() {
32 if (instance == null) {
33 instance = new OverpassTurboQueryWizard();
34 }
35 return instance;
36 }
37
38 private OverpassTurboQueryWizard() {
39 try (CachedFile file = new CachedFile("resource://data/overpass-wizard.js");
40 Reader reader = file.getContentReader()) {
41 if (engine != null) {
42 engine.eval("var console = {error: " + Logging.class.getCanonicalName() + ".warn};");
43 engine.eval("var global = {};");
44 engine.eval(reader);
45 engine.eval("var overpassWizard = function(query) {" +
46 " return global.overpassWizard(query, {" +
47 " comment: false," +
48 " timeout: " + Config.getPref().getInt("overpass.wizard.timeout", 90) + "," +
49 " outputFormat: 'xml'," +
50 " outputMode: 'recursive_meta'" +
51 " });" +
52 "}");
53 }
54 } catch (ScriptException | IOException ex) {
55 throw new IllegalStateException("Failed to initialize OverpassTurboQueryWizard", ex);
56 }
57 }
58
59 /**
60 * Builds an Overpass QL from a {@link org.openstreetmap.josm.actions.search.SearchAction} like query.
61 * @param search the {@link org.openstreetmap.josm.actions.search.SearchAction} like query
62 * @return an Overpass QL query
63 * @throws UncheckedParseException when the parsing fails
64 */
65 public String constructQuery(String search) {
66 if (engine == null) {
67 throw new IllegalStateException("Failed to retrieve JavaScript engine");
68 }
69 try {
70 final Object result = ((Invocable) engine).invokeFunction("overpassWizard", search);
71 if (Boolean.FALSE.equals(result)) {
72 throw new UncheckedParseException();
73 }
74 return (String) result;
75 } catch (NoSuchMethodException e) {
76 throw new IllegalStateException(e);
77 } catch (ScriptException e) {
78 throw new UncheckedParseException("Failed to execute OverpassTurboQueryWizard", e);
79 }
80 }
81}
Note: See TracBrowser for help on using the repository browser.