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

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

see #14097, see #15783 - robustness to situations where JavaScript engine cannot be retrieved, for whatever reason

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