2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.kofx;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23 import java.io.OutputStream;
24 import java.io.PushbackInputStream;
25 import java.io.Reader;
26 import java.net.HttpURLConnection;
27 import java.net.MalformedURLException;
29 import java.net.URLConnection;
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 net.java.html.js.JavaScriptBody;
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;
44 /** This is an implementation package - just
45 * include its JAR on classpath and use official {@link Context} API
46 * to access the functionality.
48 * @author Jaroslav Tulach <jtulach@netbeans.org>
50 final class LoadJSON implements Runnable {
51 private static final Logger LOG = FXContext.LOG;
52 private static final Executor REQ = Executors.newCachedThreadPool();
54 private final JSONCall call;
55 private final URL base;
56 private Throwable error;
60 private LoadJSON(JSONCall call) {
64 b = new URL(findBaseURL());
65 } catch (MalformedURLException ex) {
66 LOG.log(Level.SEVERE, "Can't find base url for " + call.composeURL("dummy"), ex);
72 public static void loadJSON(JSONCall call) {
73 REQ.execute(new LoadJSON((call)));
78 if (Platform.isFxApplicationThread()) {
80 call.notifyError(error);
82 call.notifySuccess(json);
88 url = call.composeURL("dummy");
90 url = call.composeURL(null);
93 final URL u = new URL(base, url.replace(" ", "%20"));
94 URLConnection conn = u.openConnection();
95 if (conn instanceof HttpURLConnection) {
96 HttpURLConnection huc = (HttpURLConnection) conn;
97 if (call.getMethod() != null) {
98 huc.setRequestMethod(call.getMethod());
100 if (call.isDoOutput()) {
101 huc.setDoOutput(true);
102 final OutputStream os = huc.getOutputStream();
107 final PushbackInputStream is = new PushbackInputStream(
108 conn.getInputStream(), 1
110 boolean array = false;
111 boolean string = false;
112 if (call.isJSONP()) {
135 if (!array && ch != '{') {
142 throw new JSONException("");
144 Reader r = new InputStreamReader(is, "UTF-8");
146 JSONTokener tok = new JSONTokener(r);
148 obj = array ? new JSONArray(tok) : new JSONObject(tok);
149 json = convertToArray(obj);
150 } catch (JSONException ex) {
151 Reader r = new InputStreamReader(is, "UTF-8");
152 StringBuilder sb = new StringBuilder();
160 json = sb.toString();
162 } catch (IOException ex) {
164 LOG.log(Level.WARNING, "Cannot connect to " + url, ex);
166 Platform.runLater(this);
170 private static Object convertToArray(Object o) throws JSONException {
171 if (o instanceof JSONArray) {
172 JSONArray ja = (JSONArray)o;
173 Object[] arr = new Object[ja.length()];
174 for (int i = 0; i < arr.length; i++) {
175 arr[i] = convertToArray(ja.get(i));
178 } else if (o instanceof JSONObject) {
179 JSONObject obj = (JSONObject)o;
180 Iterator it = obj.keys();
181 while (it.hasNext()) {
182 String key = (String)it.next();
183 obj.put(key, convertToArray(obj.get(key)));
191 public static void extractJSON(Object jsonObject, String[] props, Object[] values) {
192 if (jsonObject instanceof JSONObject) {
193 JSONObject obj = (JSONObject)jsonObject;
194 for (int i = 0; i < props.length; i++) {
196 values[i] = obj.has(props[i]) ? obj.get(props[i]) : null;
197 } catch (JSONException ex) {
198 LoadJSON.LOG.log(Level.SEVERE, "Can't read " + props[i] + " from " + jsonObject, ex);
202 if (jsonObject instanceof JSObject) {
203 JSObject obj = (JSObject)jsonObject;
204 for (int i = 0; i < props.length; i++) {
205 Object val = obj.getMember(props[i]);
206 values[i] = isDefined(val) ? val : null;
211 public static Object parse(InputStream is) throws IOException {
213 InputStreamReader r = new InputStreamReader(is, "UTF-8");
214 JSONTokener t = new JSONTokener(r);
215 return new JSONObject(t);
216 } catch (JSONException ex) {
217 throw new IOException(ex);
221 @JavaScriptBody(args = { }, body =
223 + "if (!!window && !!window.location && !!window.location.href)\n"
224 + " h = window.location.href;\n"
229 private static native String findBaseURL();
231 private static boolean isDefined(Object val) {
232 return !"undefined".equals(val);