1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/ko/fx/src/main/java/org/apidesign/bck2brwsr/kofx/LoadJSON.java Wed Jun 26 19:57:38 2013 +0200
1.3 @@ -0,0 +1,234 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.bck2brwsr.kofx;
1.22 +
1.23 +import java.io.IOException;
1.24 +import java.io.InputStream;
1.25 +import java.io.InputStreamReader;
1.26 +import java.io.OutputStream;
1.27 +import java.io.PushbackInputStream;
1.28 +import java.io.Reader;
1.29 +import java.net.HttpURLConnection;
1.30 +import java.net.MalformedURLException;
1.31 +import java.net.URL;
1.32 +import java.net.URLConnection;
1.33 +import java.util.Iterator;
1.34 +import java.util.concurrent.Executor;
1.35 +import java.util.concurrent.Executors;
1.36 +import java.util.logging.Level;
1.37 +import java.util.logging.Logger;
1.38 +import javafx.application.Platform;
1.39 +import net.java.html.js.JavaScriptBody;
1.40 +import netscape.javascript.JSObject;
1.41 +import org.apidesign.html.json.spi.JSONCall;
1.42 +import org.json.JSONArray;
1.43 +import org.json.JSONException;
1.44 +import org.json.JSONObject;
1.45 +import org.json.JSONTokener;
1.46 +
1.47 +/** This is an implementation package - just
1.48 + * include its JAR on classpath and use official {@link Context} API
1.49 + * to access the functionality.
1.50 + *
1.51 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.52 + */
1.53 +final class LoadJSON implements Runnable {
1.54 + private static final Logger LOG = FXContext.LOG;
1.55 + private static final Executor REQ = Executors.newCachedThreadPool();
1.56 +
1.57 + private final JSONCall call;
1.58 + private final URL base;
1.59 + private Throwable error;
1.60 + private Object json;
1.61 +
1.62 +
1.63 + private LoadJSON(JSONCall call) {
1.64 + this.call = call;
1.65 + URL b;
1.66 + try {
1.67 + b = new URL(findBaseURL());
1.68 + } catch (MalformedURLException ex) {
1.69 + LOG.log(Level.SEVERE, "Can't find base url for " + call.composeURL("dummy"), ex);
1.70 + b = null;
1.71 + }
1.72 + this.base = b;
1.73 + }
1.74 +
1.75 + public static void loadJSON(JSONCall call) {
1.76 + REQ.execute(new LoadJSON((call)));
1.77 + }
1.78 +
1.79 + @Override
1.80 + public void run() {
1.81 + if (Platform.isFxApplicationThread()) {
1.82 + if (error != null) {
1.83 + call.notifyError(error);
1.84 + } else {
1.85 + call.notifySuccess(json);
1.86 + }
1.87 + return;
1.88 + }
1.89 + final String url;
1.90 + if (call.isJSONP()) {
1.91 + url = call.composeURL("dummy");
1.92 + } else {
1.93 + url = call.composeURL(null);
1.94 + }
1.95 + try {
1.96 + final URL u = new URL(base, url.replace(" ", "%20"));
1.97 + URLConnection conn = u.openConnection();
1.98 + if (conn instanceof HttpURLConnection) {
1.99 + HttpURLConnection huc = (HttpURLConnection) conn;
1.100 + if (call.getMethod() != null) {
1.101 + huc.setRequestMethod(call.getMethod());
1.102 + }
1.103 + if (call.isDoOutput()) {
1.104 + huc.setDoOutput(true);
1.105 + final OutputStream os = huc.getOutputStream();
1.106 + call.writeData(os);
1.107 + os.flush();
1.108 + }
1.109 + }
1.110 + final PushbackInputStream is = new PushbackInputStream(
1.111 + conn.getInputStream(), 1
1.112 + );
1.113 + boolean array = false;
1.114 + boolean string = false;
1.115 + if (call.isJSONP()) {
1.116 + for (;;) {
1.117 + int ch = is.read();
1.118 + if (ch == -1) {
1.119 + break;
1.120 + }
1.121 + if (ch == '[') {
1.122 + is.unread(ch);
1.123 + array = true;
1.124 + break;
1.125 + }
1.126 + if (ch == '{') {
1.127 + is.unread(ch);
1.128 + break;
1.129 + }
1.130 + }
1.131 + } else {
1.132 + int ch = is.read();
1.133 + if (ch == -1) {
1.134 + string = true;
1.135 + } else {
1.136 + array = ch == '[';
1.137 + is.unread(ch);
1.138 + if (!array && ch != '{') {
1.139 + string = true;
1.140 + }
1.141 + }
1.142 + }
1.143 + try {
1.144 + if (string) {
1.145 + throw new JSONException("");
1.146 + }
1.147 + Reader r = new InputStreamReader(is, "UTF-8");
1.148 +
1.149 + JSONTokener tok = new JSONTokener(r);
1.150 + Object obj;
1.151 + obj = array ? new JSONArray(tok) : new JSONObject(tok);
1.152 + json = convertToArray(obj);
1.153 + } catch (JSONException ex) {
1.154 + Reader r = new InputStreamReader(is, "UTF-8");
1.155 + StringBuilder sb = new StringBuilder();
1.156 + for (;;) {
1.157 + int ch = r.read();
1.158 + if (ch == -1) {
1.159 + break;
1.160 + }
1.161 + sb.append((char)ch);
1.162 + }
1.163 + json = sb.toString();
1.164 + }
1.165 + } catch (IOException ex) {
1.166 + error = ex;
1.167 + LOG.log(Level.WARNING, "Cannot connect to " + url, ex);
1.168 + } finally {
1.169 + Platform.runLater(this);
1.170 + }
1.171 + }
1.172 +
1.173 + private static Object convertToArray(Object o) throws JSONException {
1.174 + if (o instanceof JSONArray) {
1.175 + JSONArray ja = (JSONArray)o;
1.176 + Object[] arr = new Object[ja.length()];
1.177 + for (int i = 0; i < arr.length; i++) {
1.178 + arr[i] = convertToArray(ja.get(i));
1.179 + }
1.180 + return arr;
1.181 + } else if (o instanceof JSONObject) {
1.182 + JSONObject obj = (JSONObject)o;
1.183 + Iterator it = obj.keys();
1.184 + while (it.hasNext()) {
1.185 + String key = (String)it.next();
1.186 + obj.put(key, convertToArray(obj.get(key)));
1.187 + }
1.188 + return obj;
1.189 + } else {
1.190 + return o;
1.191 + }
1.192 + }
1.193 +
1.194 + public static void extractJSON(Object jsonObject, String[] props, Object[] values) {
1.195 + if (jsonObject instanceof JSONObject) {
1.196 + JSONObject obj = (JSONObject)jsonObject;
1.197 + for (int i = 0; i < props.length; i++) {
1.198 + try {
1.199 + values[i] = obj.has(props[i]) ? obj.get(props[i]) : null;
1.200 + } catch (JSONException ex) {
1.201 + LoadJSON.LOG.log(Level.SEVERE, "Can't read " + props[i] + " from " + jsonObject, ex);
1.202 + }
1.203 + }
1.204 + }
1.205 + if (jsonObject instanceof JSObject) {
1.206 + JSObject obj = (JSObject)jsonObject;
1.207 + for (int i = 0; i < props.length; i++) {
1.208 + Object val = obj.getMember(props[i]);
1.209 + values[i] = isDefined(val) ? val : null;
1.210 + }
1.211 + }
1.212 + }
1.213 +
1.214 + public static Object parse(InputStream is) throws IOException {
1.215 + try {
1.216 + InputStreamReader r = new InputStreamReader(is, "UTF-8");
1.217 + JSONTokener t = new JSONTokener(r);
1.218 + return new JSONObject(t);
1.219 + } catch (JSONException ex) {
1.220 + throw new IOException(ex);
1.221 + }
1.222 + }
1.223 +
1.224 + @JavaScriptBody(args = { }, body =
1.225 + "var h;"
1.226 + + "if (!!window && !!window.location && !!window.location.href)\n"
1.227 + + " h = window.location.href;\n"
1.228 + + "else "
1.229 + + " h = null;"
1.230 + + "return h;\n"
1.231 + )
1.232 + private static native String findBaseURL();
1.233 +
1.234 + private static boolean isDefined(Object val) {
1.235 + return !"undefined".equals(val);
1.236 + }
1.237 +}