1.1 --- a/ko/bck2brwsr/src/main/java/org/apidesign/bck2brwsr/ko2brwsr/BrwsrCtxImpl.java Thu Jan 09 21:19:44 2014 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,210 +0,0 @@
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.ko2brwsr;
1.22 -
1.23 -import java.io.ByteArrayOutputStream;
1.24 -import java.io.IOException;
1.25 -import java.io.InputStream;
1.26 -import java.io.InputStreamReader;
1.27 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.28 -import org.apidesign.html.json.spi.JSONCall;
1.29 -import org.apidesign.html.json.spi.Transfer;
1.30 -import org.apidesign.html.json.spi.WSTransfer;
1.31 -
1.32 -/**
1.33 - *
1.34 - * @author Jaroslav Tulach <jtulach@netbeans.org>
1.35 - */
1.36 -final class BrwsrCtxImpl implements Transfer, WSTransfer<LoadWS> {
1.37 - private BrwsrCtxImpl() {}
1.38 -
1.39 - public static final BrwsrCtxImpl DEFAULT = new BrwsrCtxImpl();
1.40 -
1.41 - @Override
1.42 - public void extract(Object obj, String[] props, Object[] values) {
1.43 - extractJSON(obj, props, values);
1.44 - }
1.45 -
1.46 - @Override
1.47 - public void loadJSON(final JSONCall call) {
1.48 - class R implements Runnable {
1.49 - final boolean success;
1.50 -
1.51 - public R(boolean success) {
1.52 - this.success = success;
1.53 - }
1.54 -
1.55 - Object[] arr = { null };
1.56 - @Override
1.57 - public void run() {
1.58 - if (success) {
1.59 - call.notifySuccess(arr[0]);
1.60 - } else {
1.61 - Throwable t;
1.62 - if (arr[0] instanceof Throwable) {
1.63 - t = (Throwable) arr[0];
1.64 - } else {
1.65 - if (arr[0] == null) {
1.66 - t = new IOException();
1.67 - } else {
1.68 - t = new IOException(arr[0].toString());
1.69 - }
1.70 - }
1.71 - call.notifyError(t);
1.72 - }
1.73 - }
1.74 - }
1.75 - R success = new R(true);
1.76 - R failure = new R(false);
1.77 - if (call.isJSONP()) {
1.78 - String me = createJSONP(success.arr, success);
1.79 - loadJSONP(call.composeURL(me), me);
1.80 - } else {
1.81 - String data = null;
1.82 - if (call.isDoOutput()) {
1.83 - try {
1.84 - ByteArrayOutputStream bos = new ByteArrayOutputStream();
1.85 - call.writeData(bos);
1.86 - data = new String(bos.toByteArray(), "UTF-8");
1.87 - } catch (IOException ex) {
1.88 - call.notifyError(ex);
1.89 - }
1.90 - }
1.91 - loadJSON(call.composeURL(null), success.arr, success, failure, call.getMethod(), data);
1.92 - }
1.93 - }
1.94 -
1.95 - @Override
1.96 - public Object toJSON(InputStream is) throws IOException {
1.97 - StringBuilder sb = new StringBuilder();
1.98 - InputStreamReader r = new InputStreamReader(is);
1.99 - for (;;) {
1.100 - int ch = r.read();
1.101 - if (ch == -1) {
1.102 - break;
1.103 - }
1.104 - sb.append((char)ch);
1.105 - }
1.106 - return parse(sb.toString());
1.107 - }
1.108 -
1.109 - @Override
1.110 - public LoadWS open(String url, JSONCall callback) {
1.111 - return new LoadWS(callback, url);
1.112 - }
1.113 -
1.114 - @Override
1.115 - public void send(LoadWS socket, JSONCall data) {
1.116 - socket.send(data);
1.117 - }
1.118 -
1.119 - @Override
1.120 - public void close(LoadWS socket) {
1.121 - socket.close();
1.122 - }
1.123 -
1.124 - //
1.125 - // implementations
1.126 - //
1.127 -
1.128 - @JavaScriptBody(args = {"object", "property"},
1.129 - body
1.130 - = "if (property === null) return object;\n"
1.131 - + "if (object === null) return null;\n"
1.132 - + "var p = object[property]; return p ? p : null;"
1.133 - )
1.134 - private static Object getProperty(Object object, String property) {
1.135 - return null;
1.136 - }
1.137 -
1.138 - public static String createJSONP(Object[] jsonResult, Runnable whenDone) {
1.139 - int h = whenDone.hashCode();
1.140 - String name;
1.141 - for (;;) {
1.142 - name = "jsonp" + Integer.toHexString(h);
1.143 - if (defineIfUnused(name, jsonResult, whenDone)) {
1.144 - return name;
1.145 - }
1.146 - h++;
1.147 - }
1.148 - }
1.149 -
1.150 - @JavaScriptBody(args = {"name", "arr", "run"}, body
1.151 - = "if (window[name]) return false;\n "
1.152 - + "window[name] = function(data) {\n "
1.153 - + " delete window[name];\n"
1.154 - + " var el = window.document.getElementById(name);\n"
1.155 - + " el.parentNode.removeChild(el);\n"
1.156 - + " arr[0] = data;\n"
1.157 - + " run.run__V();\n"
1.158 - + "};\n"
1.159 - + "return true;\n"
1.160 - )
1.161 - private static boolean defineIfUnused(String name, Object[] arr, Runnable run) {
1.162 - return true;
1.163 - }
1.164 -
1.165 - @JavaScriptBody(args = {"s"}, body = "return eval('(' + s + ')');")
1.166 - static Object parse(String s) {
1.167 - return s;
1.168 - }
1.169 -
1.170 - @JavaScriptBody(args = {"url", "arr", "callback", "onError", "method", "data"}, body = ""
1.171 - + "var request = new XMLHttpRequest();\n"
1.172 - + "if (!method) method = 'GET';\n"
1.173 - + "request.open(method, url, true);\n"
1.174 - + "request.setRequestHeader('Content-Type', 'application/json; charset=utf-8');\n"
1.175 - + "request.onreadystatechange = function() {\n"
1.176 - + " if (this.readyState!==4) return;\n"
1.177 - + " try {\n"
1.178 - + " arr[0] = eval('(' + this.response + ')');\n"
1.179 - + " } catch (error) {;\n"
1.180 - + " arr[0] = this.response;\n"
1.181 - + " }\n"
1.182 - + " callback.run__V();\n"
1.183 - + "};\n"
1.184 - + "request.onerror = function (e) {\n"
1.185 - + " arr[0] = e; onError.run__V();\n"
1.186 - + "}\n"
1.187 - + "if (data) request.send(data);"
1.188 - + "else request.send();"
1.189 - )
1.190 - static void loadJSON(
1.191 - String url, Object[] jsonResult, Runnable whenDone, Runnable whenErr, String method, String data
1.192 - ) {
1.193 - }
1.194 -
1.195 - @JavaScriptBody(args = {"url", "jsonp"}, body
1.196 - = "var scrpt = window.document.createElement('script');\n "
1.197 - + "scrpt.setAttribute('src', url);\n "
1.198 - + "scrpt.setAttribute('id', jsonp);\n "
1.199 - + "scrpt.setAttribute('type', 'text/javascript');\n "
1.200 - + "var body = document.getElementsByTagName('body')[0];\n "
1.201 - + "body.appendChild(scrpt);\n"
1.202 - )
1.203 - static void loadJSONP(String url, String jsonp) {
1.204 -
1.205 - }
1.206 -
1.207 - public static void extractJSON(Object jsonObject, String[] props, Object[] values) {
1.208 - for (int i = 0; i < props.length; i++) {
1.209 - values[i] = getProperty(jsonObject, props[i]);
1.210 - }
1.211 - }
1.212 -
1.213 -}