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

Last change on this file since 10745 was 10733, checked in by Don-vip, 8 years ago

checkstyle

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.ScriptEngineManager;
10import javax.script.ScriptException;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.io.CachedFile;
14
15/**
16 * Uses <a href="https://github.com/tyrasd/overpass-wizard/">Overpass Turbo query wizard</a> code (MIT Licensed)
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
41 try (CachedFile file = new CachedFile("resource://data/overpass-wizard.js");
42 Reader reader = file.getContentReader()) {
43 engine.eval("var console = {error: " + Main.class.getCanonicalName() + ".warn};");
44 engine.eval("var global = {};");
45 engine.eval(reader);
46 engine.eval("var overpassWizard = function(query) {" +
47 " return global.overpassWizard(query, {" +
48 " comment: false," +
49 " outputFormat: 'xml'," +
50 " outputMode: 'recursive_meta'" +
51 " });" +
52 "}");
53 } catch (ScriptException | IOException ex) {
54 throw new IllegalStateException("Failed to initialize OverpassTurboQueryWizard", ex);
55 }
56 }
57
58 /**
59 * Builds an Overpass QL from a {@link org.openstreetmap.josm.actions.search.SearchAction} like query.
60 * @param search the {@link org.openstreetmap.josm.actions.search.SearchAction} like query
61 * @return an Overpass QL query
62 * @throws UncheckedParseException when the parsing fails
63 */
64 public String constructQuery(String search) {
65 try {
66 final Object result = ((Invocable) engine).invokeFunction("overpassWizard", search);
67 if (Boolean.FALSE.equals(result)) {
68 throw new UncheckedParseException();
69 }
70 String query = (String) result;
71 if (query != null) {
72 query = query.replace("[bbox:{{bbox}}]", "");
73 }
74 return query;
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.