1.1 --- a/ko/bck2brwsr/src/main/java/org/apidesign/bck2brwsr/ko2brwsr/ConvertTypes.java Thu Jan 09 15:57:28 2014 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,157 +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 org.apidesign.bck2brwsr.core.JavaScriptBody;
1.24 -
1.25 -/**
1.26 - *
1.27 - * @author Jaroslav Tulach <jtulach@netbeans.org>
1.28 - */
1.29 -final class ConvertTypes {
1.30 - ConvertTypes() {
1.31 - }
1.32 -
1.33 - public static String toString(Object object, String property) {
1.34 - Object ret = getProperty(object, property);
1.35 - return ret == null ? null : ret.toString();
1.36 - }
1.37 -
1.38 - public static double toDouble(Object object, String property) {
1.39 - Object ret = getProperty(object, property);
1.40 - return ret instanceof Number ? ((Number)ret).doubleValue() : Double.NaN;
1.41 - }
1.42 -
1.43 - public static int toInt(Object object, String property) {
1.44 - Object ret = getProperty(object, property);
1.45 - return ret instanceof Number ? ((Number)ret).intValue() : Integer.MIN_VALUE;
1.46 - }
1.47 -
1.48 - public static <T> T toModel(Class<T> modelClass, Object object, String property) {
1.49 - Object ret = getProperty(object, property);
1.50 - if (ret == null || modelClass.isInstance(ret)) {
1.51 - return modelClass.cast(ret);
1.52 - }
1.53 - throw new IllegalStateException("Value " + ret + " is not of type " + modelClass);
1.54 - }
1.55 -
1.56 - public static String toJSON(Object value) {
1.57 - if (value == null) {
1.58 - return "null";
1.59 - }
1.60 - if (value instanceof Enum) {
1.61 - value = value.toString();
1.62 - }
1.63 - if (value instanceof String) {
1.64 - return '"' +
1.65 - ((String)value).
1.66 - replace("\"", "\\\"").
1.67 - replace("\n", "\\n").
1.68 - replace("\r", "\\r").
1.69 - replace("\t", "\\t")
1.70 - + '"';
1.71 - }
1.72 - return value.toString();
1.73 - }
1.74 -
1.75 - @JavaScriptBody(args = { "object", "property" },
1.76 - body =
1.77 - "if (property === null) return object;\n"
1.78 - + "if (object === null) return null;\n"
1.79 - + "var p = object[property]; return p ? p : null;"
1.80 - )
1.81 - private static Object getProperty(Object object, String property) {
1.82 - return null;
1.83 - }
1.84 -
1.85 - public static String createJSONP(Object[] jsonResult, Runnable whenDone) {
1.86 - int h = whenDone.hashCode();
1.87 - String name;
1.88 - for (;;) {
1.89 - name = "jsonp" + Integer.toHexString(h);
1.90 - if (defineIfUnused(name, jsonResult, whenDone)) {
1.91 - return name;
1.92 - }
1.93 - h++;
1.94 - }
1.95 - }
1.96 -
1.97 - @JavaScriptBody(args = { "name", "arr", "run" }, body =
1.98 - "if (window[name]) return false;\n "
1.99 - + "window[name] = function(data) {\n "
1.100 - + " delete window[name];\n"
1.101 - + " var el = window.document.getElementById(name);\n"
1.102 - + " el.parentNode.removeChild(el);\n"
1.103 - + " arr[0] = data;\n"
1.104 - + " run.run__V();\n"
1.105 - + "};\n"
1.106 - + "return true;\n"
1.107 - )
1.108 - private static boolean defineIfUnused(String name, Object[] arr, Runnable run) {
1.109 - return true;
1.110 - }
1.111 -
1.112 - @JavaScriptBody(args = { "s" }, body = "return eval('(' + s + ')');")
1.113 - static Object parse(String s) {
1.114 - return s;
1.115 - }
1.116 -
1.117 - @JavaScriptBody(args = { "url", "arr", "callback", "onError", "method", "data" }, body = ""
1.118 - + "var request = new XMLHttpRequest();\n"
1.119 - + "if (!method) method = 'GET';\n"
1.120 - + "request.open(method, url, true);\n"
1.121 - + "request.setRequestHeader('Content-Type', 'application/json; charset=utf-8');\n"
1.122 - + "request.onreadystatechange = function() {\n"
1.123 - + " if (this.readyState!==4) return;\n"
1.124 - + " try {\n"
1.125 - + " arr[0] = eval('(' + this.response + ')');\n"
1.126 - + " } catch (error) {;\n"
1.127 - + " arr[0] = this.response;\n"
1.128 - + " }\n"
1.129 - + " callback.run__V();\n"
1.130 - + "};\n"
1.131 - + "request.onerror = function (e) {\n"
1.132 - + " arr[0] = e; onError.run__V();\n"
1.133 - + "}\n"
1.134 - + "if (data) request.send(data);"
1.135 - + "else request.send();"
1.136 - )
1.137 - static void loadJSON(
1.138 - String url, Object[] jsonResult, Runnable whenDone, Runnable whenErr, String method, String data
1.139 - ) {
1.140 - }
1.141 -
1.142 - @JavaScriptBody(args = { "url", "jsonp" }, body =
1.143 - "var scrpt = window.document.createElement('script');\n "
1.144 - + "scrpt.setAttribute('src', url);\n "
1.145 - + "scrpt.setAttribute('id', jsonp);\n "
1.146 - + "scrpt.setAttribute('type', 'text/javascript');\n "
1.147 - + "var body = document.getElementsByTagName('body')[0];\n "
1.148 - + "body.appendChild(scrpt);\n"
1.149 - )
1.150 - static void loadJSONP(String url, String jsonp) {
1.151 -
1.152 - }
1.153 -
1.154 - public static void extractJSON(Object jsonObject, String[] props, Object[] values) {
1.155 - for (int i = 0; i < props.length; i++) {
1.156 - values[i] = getProperty(jsonObject, props[i]);
1.157 - }
1.158 - }
1.159 -
1.160 -}