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

Last change on this file since 12921 was 12627, checked in by Don-vip, 7 years ago

see #15182 - remove unneeded imports to Main

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.Reader;
6
7import javax.script.Invocable;
8import javax.script.ScriptEngine;
9import javax.script.ScriptEngineManager;
10import javax.script.ScriptException;
11
12import org.openstreetmap.josm.io.CachedFile;
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 = new ScriptEngineManager().getEngineByName("JavaScript");
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 if (engine == null) {
40 throw new IllegalStateException("Failed to retrieve JavaScript engine");
41 }
42 try (CachedFile file = new CachedFile("resource://data/overpass-wizard.js");
43 Reader reader = file.getContentReader()) {
44 engine.eval("var console = {error: " + Logging.class.getCanonicalName() + ".warn};");
45 engine.eval("var global = {};");
46 engine.eval(reader);
47 engine.eval("var overpassWizard = function(query) {" +
48 " return global.overpassWizard(query, {" +
49 " comment: false," +
50 " outputFormat: 'xml'," +
51 " outputMode: 'recursive_meta'" +
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 try {
67 final Object result = ((Invocable) engine).invokeFunction("overpassWizard", search);
68 if (Boolean.FALSE.equals(result)) {
69 throw new UncheckedParseException();
70 }
71 return (String) result;
72 } catch (NoSuchMethodException e) {
73 throw new IllegalStateException(e);
74 } catch (ScriptException e) {
75 throw new UncheckedParseException("Failed to execute OverpassTurboQueryWizard", e);
76 }
77 }
78}
Note: See TracBrowser for help on using the repository browser.