2 * HTML via Java(tm) Language Bindings
3 * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. apidesign.org
13 * designates this particular file as subject to the
14 * "Classpath" exception as provided by apidesign.org
15 * in the License file that accompanied this code.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. Look for COPYING file in the top folder.
19 * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
21 package org.apidesign.html.kofx;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 import java.io.PushbackInputStream;
27 import java.io.Reader;
28 import java.net.MalformedURLException;
30 import java.util.Iterator;
31 import java.util.concurrent.Executor;
32 import java.util.concurrent.Executors;
33 import java.util.logging.Level;
34 import java.util.logging.Logger;
35 import javafx.application.Platform;
36 import javafx.scene.web.WebEngine;
37 import netscape.javascript.JSObject;
38 import org.apidesign.html.json.spi.JSONCall;
39 import org.json.JSONArray;
40 import org.json.JSONException;
41 import org.json.JSONObject;
42 import org.json.JSONTokener;
43 import org.openide.util.Exceptions;
45 /** This is an implementation package - just
46 * include its JAR on classpath and use official {@link Context} API
47 * to access the functionality.
49 * @author Jaroslav Tulach <jtulach@netbeans.org>
51 final class LoadJSON implements Runnable {
52 private static final Logger LOG = FXContext.LOG;
53 private static final Executor REQ = Executors.newCachedThreadPool();
55 private final JSONCall call;
56 private final URL base;
57 private Throwable error;
61 private LoadJSON(JSONCall call) {
65 b = new URL(findBaseURL());
66 } catch (MalformedURLException ex) {
67 LOG.log(Level.SEVERE, "Can't find base url for " + call.composeURL("dummy"), ex);
73 public static void loadJSON(JSONCall call) {
74 REQ.execute(new LoadJSON((call)));
79 if (Platform.isFxApplicationThread()) {
81 call.notifyError(error);
83 call.notifySuccess(json);
89 url = call.composeURL("dummy");
91 url = call.composeURL(null);
94 final URL u = new URL(base, url.replace(" ", "%20"));
95 final PushbackInputStream is = new PushbackInputStream(u.openStream(), 1);
96 boolean array = false;
118 Reader r = new InputStreamReader(is, "UTF-8");
120 JSONTokener tok = new JSONTokener(r);
121 Object obj = array ? new JSONArray(tok) : new JSONObject(tok);
122 json = convertToArray(obj);
123 } catch (JSONException | IOException ex) {
125 LOG.log(Level.WARNING, "Cannot connect to " + url, ex);
127 Platform.runLater(this);
131 private static Object convertToArray(Object o) throws JSONException {
132 if (o instanceof JSONArray) {
133 JSONArray ja = (JSONArray)o;
134 Object[] arr = new Object[ja.length()];
135 for (int i = 0; i < arr.length; i++) {
136 arr[i] = convertToArray(ja.get(i));
139 } else if (o instanceof JSONObject) {
140 JSONObject obj = (JSONObject)o;
141 Iterator it = obj.keys();
142 while (it.hasNext()) {
143 String key = (String)it.next();
144 obj.put(key, convertToArray(obj.get(key)));
152 public static void extractJSON(Object jsonObject, String[] props, Object[] values) {
153 if (jsonObject instanceof JSONObject) {
154 JSONObject obj = (JSONObject)jsonObject;
155 for (int i = 0; i < props.length; i++) {
157 values[i] = obj.has(props[i]) ? obj.get(props[i]) : null;
158 } catch (JSONException ex) {
159 LoadJSON.LOG.log(Level.SEVERE, "Can't read " + props[i] + " from " + jsonObject, ex);
163 if (jsonObject instanceof JSObject) {
164 JSObject obj = (JSObject)jsonObject;
165 for (int i = 0; i < props.length; i++) {
166 Object val = obj.getMember(props[i]);
167 values[i] = isDefined(val) ? val : null;
172 public static Object parse(InputStream is) throws IOException {
174 InputStreamReader r = new InputStreamReader(is, "UTF-8");
175 JSONTokener t = new JSONTokener(r);
176 return new JSONObject(t);
177 } catch (JSONException ex) {
178 throw new IOException(ex);
182 private static String findBaseURL() {
183 WebEngine eng = (WebEngine) System.getProperties().get("webEngine");
184 return (String) eng.executeScript(
186 + "if (!!window && !!window.location && !!window.location.href)\n"
187 + " h = window.location.href;\n"
193 private static boolean isDefined(Object val) {
194 return !"undefined".equals(val);