# HG changeset patch # User Jaroslav Tulach # Date 1389281895 -3600 # Node ID 43bb0053f3e25a03c0c52eaca20ed81210e1c6b3 # Parent 095ce896e09420731d6c0f9eb561a83701540c5d Removing useless methods and classes diff -r 095ce896e094 -r 43bb0053f3e2 ko/bck2brwsr/src/main/java/org/apidesign/bck2brwsr/ko2brwsr/BrwsrCtxImpl.java --- a/ko/bck2brwsr/src/main/java/org/apidesign/bck2brwsr/ko2brwsr/BrwsrCtxImpl.java Thu Jan 09 15:57:28 2014 +0100 +++ b/ko/bck2brwsr/src/main/java/org/apidesign/bck2brwsr/ko2brwsr/BrwsrCtxImpl.java Thu Jan 09 16:38:15 2014 +0100 @@ -21,6 +21,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import org.apidesign.bck2brwsr.core.JavaScriptBody; import org.apidesign.html.json.spi.JSONCall; import org.apidesign.html.json.spi.Transfer; import org.apidesign.html.json.spi.WSTransfer; @@ -36,7 +37,7 @@ @Override public void extract(Object obj, String[] props, Object[] values) { - ConvertTypes.extractJSON(obj, props, values); + extractJSON(obj, props, values); } @Override @@ -71,8 +72,8 @@ R success = new R(true); R failure = new R(false); if (call.isJSONP()) { - String me = ConvertTypes.createJSONP(success.arr, success); - ConvertTypes.loadJSONP(call.composeURL(me), me); + String me = createJSONP(success.arr, success); + loadJSONP(call.composeURL(me), me); } else { String data = null; if (call.isDoOutput()) { @@ -84,7 +85,7 @@ call.notifyError(ex); } } - ConvertTypes.loadJSON(call.composeURL(null), success.arr, success, failure, call.getMethod(), data); + loadJSON(call.composeURL(null), success.arr, success, failure, call.getMethod(), data); } } @@ -99,7 +100,7 @@ } sb.append((char)ch); } - return ConvertTypes.parse(sb.toString()); + return parse(sb.toString()); } @Override @@ -116,4 +117,94 @@ public void close(LoadWS socket) { socket.close(); } + + // + // implementations + // + + @JavaScriptBody(args = {"object", "property"}, + body + = "if (property === null) return object;\n" + + "if (object === null) return null;\n" + + "var p = object[property]; return p ? p : null;" + ) + private static Object getProperty(Object object, String property) { + return null; + } + + public static String createJSONP(Object[] jsonResult, Runnable whenDone) { + int h = whenDone.hashCode(); + String name; + for (;;) { + name = "jsonp" + Integer.toHexString(h); + if (defineIfUnused(name, jsonResult, whenDone)) { + return name; + } + h++; + } + } + + @JavaScriptBody(args = {"name", "arr", "run"}, body + = "if (window[name]) return false;\n " + + "window[name] = function(data) {\n " + + " delete window[name];\n" + + " var el = window.document.getElementById(name);\n" + + " el.parentNode.removeChild(el);\n" + + " arr[0] = data;\n" + + " run.run__V();\n" + + "};\n" + + "return true;\n" + ) + private static boolean defineIfUnused(String name, Object[] arr, Runnable run) { + return true; + } + + @JavaScriptBody(args = {"s"}, body = "return eval('(' + s + ')');") + static Object parse(String s) { + return s; + } + + @JavaScriptBody(args = {"url", "arr", "callback", "onError", "method", "data"}, body = "" + + "var request = new XMLHttpRequest();\n" + + "if (!method) method = 'GET';\n" + + "request.open(method, url, true);\n" + + "request.setRequestHeader('Content-Type', 'application/json; charset=utf-8');\n" + + "request.onreadystatechange = function() {\n" + + " if (this.readyState!==4) return;\n" + + " try {\n" + + " arr[0] = eval('(' + this.response + ')');\n" + + " } catch (error) {;\n" + + " arr[0] = this.response;\n" + + " }\n" + + " callback.run__V();\n" + + "};\n" + + "request.onerror = function (e) {\n" + + " arr[0] = e; onError.run__V();\n" + + "}\n" + + "if (data) request.send(data);" + + "else request.send();" + ) + static void loadJSON( + String url, Object[] jsonResult, Runnable whenDone, Runnable whenErr, String method, String data + ) { + } + + @JavaScriptBody(args = {"url", "jsonp"}, body + = "var scrpt = window.document.createElement('script');\n " + + "scrpt.setAttribute('src', url);\n " + + "scrpt.setAttribute('id', jsonp);\n " + + "scrpt.setAttribute('type', 'text/javascript');\n " + + "var body = document.getElementsByTagName('body')[0];\n " + + "body.appendChild(scrpt);\n" + ) + static void loadJSONP(String url, String jsonp) { + + } + + public static void extractJSON(Object jsonObject, String[] props, Object[] values) { + for (int i = 0; i < props.length; i++) { + values[i] = getProperty(jsonObject, props[i]); + } + } + } diff -r 095ce896e094 -r 43bb0053f3e2 ko/bck2brwsr/src/main/java/org/apidesign/bck2brwsr/ko2brwsr/ConvertTypes.java --- a/ko/bck2brwsr/src/main/java/org/apidesign/bck2brwsr/ko2brwsr/ConvertTypes.java Thu Jan 09 15:57:28 2014 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,157 +0,0 @@ -/** - * Back 2 Browser Bytecode Translator - * Copyright (C) 2012 Jaroslav Tulach - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, version 2 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. Look for COPYING file in the top folder. - * If not, see http://opensource.org/licenses/GPL-2.0. - */ -package org.apidesign.bck2brwsr.ko2brwsr; - -import org.apidesign.bck2brwsr.core.JavaScriptBody; - -/** - * - * @author Jaroslav Tulach - */ -final class ConvertTypes { - ConvertTypes() { - } - - public static String toString(Object object, String property) { - Object ret = getProperty(object, property); - return ret == null ? null : ret.toString(); - } - - public static double toDouble(Object object, String property) { - Object ret = getProperty(object, property); - return ret instanceof Number ? ((Number)ret).doubleValue() : Double.NaN; - } - - public static int toInt(Object object, String property) { - Object ret = getProperty(object, property); - return ret instanceof Number ? ((Number)ret).intValue() : Integer.MIN_VALUE; - } - - public static T toModel(Class modelClass, Object object, String property) { - Object ret = getProperty(object, property); - if (ret == null || modelClass.isInstance(ret)) { - return modelClass.cast(ret); - } - throw new IllegalStateException("Value " + ret + " is not of type " + modelClass); - } - - public static String toJSON(Object value) { - if (value == null) { - return "null"; - } - if (value instanceof Enum) { - value = value.toString(); - } - if (value instanceof String) { - return '"' + - ((String)value). - replace("\"", "\\\""). - replace("\n", "\\n"). - replace("\r", "\\r"). - replace("\t", "\\t") - + '"'; - } - return value.toString(); - } - - @JavaScriptBody(args = { "object", "property" }, - body = - "if (property === null) return object;\n" - + "if (object === null) return null;\n" - + "var p = object[property]; return p ? p : null;" - ) - private static Object getProperty(Object object, String property) { - return null; - } - - public static String createJSONP(Object[] jsonResult, Runnable whenDone) { - int h = whenDone.hashCode(); - String name; - for (;;) { - name = "jsonp" + Integer.toHexString(h); - if (defineIfUnused(name, jsonResult, whenDone)) { - return name; - } - h++; - } - } - - @JavaScriptBody(args = { "name", "arr", "run" }, body = - "if (window[name]) return false;\n " - + "window[name] = function(data) {\n " - + " delete window[name];\n" - + " var el = window.document.getElementById(name);\n" - + " el.parentNode.removeChild(el);\n" - + " arr[0] = data;\n" - + " run.run__V();\n" - + "};\n" - + "return true;\n" - ) - private static boolean defineIfUnused(String name, Object[] arr, Runnable run) { - return true; - } - - @JavaScriptBody(args = { "s" }, body = "return eval('(' + s + ')');") - static Object parse(String s) { - return s; - } - - @JavaScriptBody(args = { "url", "arr", "callback", "onError", "method", "data" }, body = "" - + "var request = new XMLHttpRequest();\n" - + "if (!method) method = 'GET';\n" - + "request.open(method, url, true);\n" - + "request.setRequestHeader('Content-Type', 'application/json; charset=utf-8');\n" - + "request.onreadystatechange = function() {\n" - + " if (this.readyState!==4) return;\n" - + " try {\n" - + " arr[0] = eval('(' + this.response + ')');\n" - + " } catch (error) {;\n" - + " arr[0] = this.response;\n" - + " }\n" - + " callback.run__V();\n" - + "};\n" - + "request.onerror = function (e) {\n" - + " arr[0] = e; onError.run__V();\n" - + "}\n" - + "if (data) request.send(data);" - + "else request.send();" - ) - static void loadJSON( - String url, Object[] jsonResult, Runnable whenDone, Runnable whenErr, String method, String data - ) { - } - - @JavaScriptBody(args = { "url", "jsonp" }, body = - "var scrpt = window.document.createElement('script');\n " - + "scrpt.setAttribute('src', url);\n " - + "scrpt.setAttribute('id', jsonp);\n " - + "scrpt.setAttribute('type', 'text/javascript');\n " - + "var body = document.getElementsByTagName('body')[0];\n " - + "body.appendChild(scrpt);\n" - ) - static void loadJSONP(String url, String jsonp) { - - } - - public static void extractJSON(Object jsonObject, String[] props, Object[] values) { - for (int i = 0; i < props.length; i++) { - values[i] = getProperty(jsonObject, props[i]); - } - } - -}