1.1 --- a/ko/bck2brwsr/src/main/java/org/apidesign/bck2brwsr/ko2brwsr/BrwsrCtxImpl.java Thu Jan 09 15:57:28 2014 +0100
1.2 +++ b/ko/bck2brwsr/src/main/java/org/apidesign/bck2brwsr/ko2brwsr/BrwsrCtxImpl.java Thu Jan 09 16:38:15 2014 +0100
1.3 @@ -21,6 +21,7 @@
1.4 import java.io.IOException;
1.5 import java.io.InputStream;
1.6 import java.io.InputStreamReader;
1.7 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.8 import org.apidesign.html.json.spi.JSONCall;
1.9 import org.apidesign.html.json.spi.Transfer;
1.10 import org.apidesign.html.json.spi.WSTransfer;
1.11 @@ -36,7 +37,7 @@
1.12
1.13 @Override
1.14 public void extract(Object obj, String[] props, Object[] values) {
1.15 - ConvertTypes.extractJSON(obj, props, values);
1.16 + extractJSON(obj, props, values);
1.17 }
1.18
1.19 @Override
1.20 @@ -71,8 +72,8 @@
1.21 R success = new R(true);
1.22 R failure = new R(false);
1.23 if (call.isJSONP()) {
1.24 - String me = ConvertTypes.createJSONP(success.arr, success);
1.25 - ConvertTypes.loadJSONP(call.composeURL(me), me);
1.26 + String me = createJSONP(success.arr, success);
1.27 + loadJSONP(call.composeURL(me), me);
1.28 } else {
1.29 String data = null;
1.30 if (call.isDoOutput()) {
1.31 @@ -84,7 +85,7 @@
1.32 call.notifyError(ex);
1.33 }
1.34 }
1.35 - ConvertTypes.loadJSON(call.composeURL(null), success.arr, success, failure, call.getMethod(), data);
1.36 + loadJSON(call.composeURL(null), success.arr, success, failure, call.getMethod(), data);
1.37 }
1.38 }
1.39
1.40 @@ -99,7 +100,7 @@
1.41 }
1.42 sb.append((char)ch);
1.43 }
1.44 - return ConvertTypes.parse(sb.toString());
1.45 + return parse(sb.toString());
1.46 }
1.47
1.48 @Override
1.49 @@ -116,4 +117,94 @@
1.50 public void close(LoadWS socket) {
1.51 socket.close();
1.52 }
1.53 +
1.54 + //
1.55 + // implementations
1.56 + //
1.57 +
1.58 + @JavaScriptBody(args = {"object", "property"},
1.59 + body
1.60 + = "if (property === null) return object;\n"
1.61 + + "if (object === null) return null;\n"
1.62 + + "var p = object[property]; return p ? p : null;"
1.63 + )
1.64 + private static Object getProperty(Object object, String property) {
1.65 + return null;
1.66 + }
1.67 +
1.68 + public static String createJSONP(Object[] jsonResult, Runnable whenDone) {
1.69 + int h = whenDone.hashCode();
1.70 + String name;
1.71 + for (;;) {
1.72 + name = "jsonp" + Integer.toHexString(h);
1.73 + if (defineIfUnused(name, jsonResult, whenDone)) {
1.74 + return name;
1.75 + }
1.76 + h++;
1.77 + }
1.78 + }
1.79 +
1.80 + @JavaScriptBody(args = {"name", "arr", "run"}, body
1.81 + = "if (window[name]) return false;\n "
1.82 + + "window[name] = function(data) {\n "
1.83 + + " delete window[name];\n"
1.84 + + " var el = window.document.getElementById(name);\n"
1.85 + + " el.parentNode.removeChild(el);\n"
1.86 + + " arr[0] = data;\n"
1.87 + + " run.run__V();\n"
1.88 + + "};\n"
1.89 + + "return true;\n"
1.90 + )
1.91 + private static boolean defineIfUnused(String name, Object[] arr, Runnable run) {
1.92 + return true;
1.93 + }
1.94 +
1.95 + @JavaScriptBody(args = {"s"}, body = "return eval('(' + s + ')');")
1.96 + static Object parse(String s) {
1.97 + return s;
1.98 + }
1.99 +
1.100 + @JavaScriptBody(args = {"url", "arr", "callback", "onError", "method", "data"}, body = ""
1.101 + + "var request = new XMLHttpRequest();\n"
1.102 + + "if (!method) method = 'GET';\n"
1.103 + + "request.open(method, url, true);\n"
1.104 + + "request.setRequestHeader('Content-Type', 'application/json; charset=utf-8');\n"
1.105 + + "request.onreadystatechange = function() {\n"
1.106 + + " if (this.readyState!==4) return;\n"
1.107 + + " try {\n"
1.108 + + " arr[0] = eval('(' + this.response + ')');\n"
1.109 + + " } catch (error) {;\n"
1.110 + + " arr[0] = this.response;\n"
1.111 + + " }\n"
1.112 + + " callback.run__V();\n"
1.113 + + "};\n"
1.114 + + "request.onerror = function (e) {\n"
1.115 + + " arr[0] = e; onError.run__V();\n"
1.116 + + "}\n"
1.117 + + "if (data) request.send(data);"
1.118 + + "else request.send();"
1.119 + )
1.120 + static void loadJSON(
1.121 + String url, Object[] jsonResult, Runnable whenDone, Runnable whenErr, String method, String data
1.122 + ) {
1.123 + }
1.124 +
1.125 + @JavaScriptBody(args = {"url", "jsonp"}, body
1.126 + = "var scrpt = window.document.createElement('script');\n "
1.127 + + "scrpt.setAttribute('src', url);\n "
1.128 + + "scrpt.setAttribute('id', jsonp);\n "
1.129 + + "scrpt.setAttribute('type', 'text/javascript');\n "
1.130 + + "var body = document.getElementsByTagName('body')[0];\n "
1.131 + + "body.appendChild(scrpt);\n"
1.132 + )
1.133 + static void loadJSONP(String url, String jsonp) {
1.134 +
1.135 + }
1.136 +
1.137 + public static void extractJSON(Object jsonObject, String[] props, Object[] values) {
1.138 + for (int i = 0; i < props.length; i++) {
1.139 + values[i] = getProperty(jsonObject, props[i]);
1.140 + }
1.141 + }
1.142 +
1.143 }
2.1 --- a/ko/bck2brwsr/src/main/java/org/apidesign/bck2brwsr/ko2brwsr/ConvertTypes.java Thu Jan 09 15:57:28 2014 +0100
2.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
2.3 @@ -1,157 +0,0 @@
2.4 -/**
2.5 - * Back 2 Browser Bytecode Translator
2.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
2.7 - *
2.8 - * This program is free software: you can redistribute it and/or modify
2.9 - * it under the terms of the GNU General Public License as published by
2.10 - * the Free Software Foundation, version 2 of the License.
2.11 - *
2.12 - * This program is distributed in the hope that it will be useful,
2.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
2.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2.15 - * GNU General Public License for more details.
2.16 - *
2.17 - * You should have received a copy of the GNU General Public License
2.18 - * along with this program. Look for COPYING file in the top folder.
2.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
2.20 - */
2.21 -package org.apidesign.bck2brwsr.ko2brwsr;
2.22 -
2.23 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
2.24 -
2.25 -/**
2.26 - *
2.27 - * @author Jaroslav Tulach <jtulach@netbeans.org>
2.28 - */
2.29 -final class ConvertTypes {
2.30 - ConvertTypes() {
2.31 - }
2.32 -
2.33 - public static String toString(Object object, String property) {
2.34 - Object ret = getProperty(object, property);
2.35 - return ret == null ? null : ret.toString();
2.36 - }
2.37 -
2.38 - public static double toDouble(Object object, String property) {
2.39 - Object ret = getProperty(object, property);
2.40 - return ret instanceof Number ? ((Number)ret).doubleValue() : Double.NaN;
2.41 - }
2.42 -
2.43 - public static int toInt(Object object, String property) {
2.44 - Object ret = getProperty(object, property);
2.45 - return ret instanceof Number ? ((Number)ret).intValue() : Integer.MIN_VALUE;
2.46 - }
2.47 -
2.48 - public static <T> T toModel(Class<T> modelClass, Object object, String property) {
2.49 - Object ret = getProperty(object, property);
2.50 - if (ret == null || modelClass.isInstance(ret)) {
2.51 - return modelClass.cast(ret);
2.52 - }
2.53 - throw new IllegalStateException("Value " + ret + " is not of type " + modelClass);
2.54 - }
2.55 -
2.56 - public static String toJSON(Object value) {
2.57 - if (value == null) {
2.58 - return "null";
2.59 - }
2.60 - if (value instanceof Enum) {
2.61 - value = value.toString();
2.62 - }
2.63 - if (value instanceof String) {
2.64 - return '"' +
2.65 - ((String)value).
2.66 - replace("\"", "\\\"").
2.67 - replace("\n", "\\n").
2.68 - replace("\r", "\\r").
2.69 - replace("\t", "\\t")
2.70 - + '"';
2.71 - }
2.72 - return value.toString();
2.73 - }
2.74 -
2.75 - @JavaScriptBody(args = { "object", "property" },
2.76 - body =
2.77 - "if (property === null) return object;\n"
2.78 - + "if (object === null) return null;\n"
2.79 - + "var p = object[property]; return p ? p : null;"
2.80 - )
2.81 - private static Object getProperty(Object object, String property) {
2.82 - return null;
2.83 - }
2.84 -
2.85 - public static String createJSONP(Object[] jsonResult, Runnable whenDone) {
2.86 - int h = whenDone.hashCode();
2.87 - String name;
2.88 - for (;;) {
2.89 - name = "jsonp" + Integer.toHexString(h);
2.90 - if (defineIfUnused(name, jsonResult, whenDone)) {
2.91 - return name;
2.92 - }
2.93 - h++;
2.94 - }
2.95 - }
2.96 -
2.97 - @JavaScriptBody(args = { "name", "arr", "run" }, body =
2.98 - "if (window[name]) return false;\n "
2.99 - + "window[name] = function(data) {\n "
2.100 - + " delete window[name];\n"
2.101 - + " var el = window.document.getElementById(name);\n"
2.102 - + " el.parentNode.removeChild(el);\n"
2.103 - + " arr[0] = data;\n"
2.104 - + " run.run__V();\n"
2.105 - + "};\n"
2.106 - + "return true;\n"
2.107 - )
2.108 - private static boolean defineIfUnused(String name, Object[] arr, Runnable run) {
2.109 - return true;
2.110 - }
2.111 -
2.112 - @JavaScriptBody(args = { "s" }, body = "return eval('(' + s + ')');")
2.113 - static Object parse(String s) {
2.114 - return s;
2.115 - }
2.116 -
2.117 - @JavaScriptBody(args = { "url", "arr", "callback", "onError", "method", "data" }, body = ""
2.118 - + "var request = new XMLHttpRequest();\n"
2.119 - + "if (!method) method = 'GET';\n"
2.120 - + "request.open(method, url, true);\n"
2.121 - + "request.setRequestHeader('Content-Type', 'application/json; charset=utf-8');\n"
2.122 - + "request.onreadystatechange = function() {\n"
2.123 - + " if (this.readyState!==4) return;\n"
2.124 - + " try {\n"
2.125 - + " arr[0] = eval('(' + this.response + ')');\n"
2.126 - + " } catch (error) {;\n"
2.127 - + " arr[0] = this.response;\n"
2.128 - + " }\n"
2.129 - + " callback.run__V();\n"
2.130 - + "};\n"
2.131 - + "request.onerror = function (e) {\n"
2.132 - + " arr[0] = e; onError.run__V();\n"
2.133 - + "}\n"
2.134 - + "if (data) request.send(data);"
2.135 - + "else request.send();"
2.136 - )
2.137 - static void loadJSON(
2.138 - String url, Object[] jsonResult, Runnable whenDone, Runnable whenErr, String method, String data
2.139 - ) {
2.140 - }
2.141 -
2.142 - @JavaScriptBody(args = { "url", "jsonp" }, body =
2.143 - "var scrpt = window.document.createElement('script');\n "
2.144 - + "scrpt.setAttribute('src', url);\n "
2.145 - + "scrpt.setAttribute('id', jsonp);\n "
2.146 - + "scrpt.setAttribute('type', 'text/javascript');\n "
2.147 - + "var body = document.getElementsByTagName('body')[0];\n "
2.148 - + "body.appendChild(scrpt);\n"
2.149 - )
2.150 - static void loadJSONP(String url, String jsonp) {
2.151 -
2.152 - }
2.153 -
2.154 - public static void extractJSON(Object jsonObject, String[] props, Object[] values) {
2.155 - for (int i = 0; i < props.length; i++) {
2.156 - values[i] = getProperty(jsonObject, props[i]);
2.157 - }
2.158 - }
2.159 -
2.160 -}