ko/bck2brwsr/src/main/java/org/apidesign/bck2brwsr/ko2brwsr/ConvertTypes.java
branchclassloader
changeset 1228 462dd9238173
parent 1227 5a907f38608d
child 1234 697da4d02f93
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ko/bck2brwsr/src/main/java/org/apidesign/bck2brwsr/ko2brwsr/ConvertTypes.java	Wed Jun 26 19:29:54 2013 +0200
     1.3 @@ -0,0 +1,152 @@
     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 = "if (property === null) return object;\n"
    1.77 +        + "var p = object[property]; return p ? p : null;"
    1.78 +    )
    1.79 +    private static Object getProperty(Object object, String property) {
    1.80 +        return null;
    1.81 +    }
    1.82 +    
    1.83 +    public static String createJSONP(Object[] jsonResult, Runnable whenDone) {
    1.84 +        int h = whenDone.hashCode();
    1.85 +        String name;
    1.86 +        for (;;) {
    1.87 +            name = "jsonp" + Integer.toHexString(h);
    1.88 +            if (defineIfUnused(name, jsonResult, whenDone)) {
    1.89 +                return name;
    1.90 +            }
    1.91 +            h++;
    1.92 +        }
    1.93 +    }
    1.94 +
    1.95 +    @JavaScriptBody(args = { "name", "arr", "run" }, body = 
    1.96 +        "if (window[name]) return false;\n "
    1.97 +      + "window[name] = function(data) {\n "
    1.98 +      + "  delete window[name];\n"
    1.99 +      + "  var el = window.document.getElementById(name);\n"
   1.100 +      + "  el.parentNode.removeChild(el);\n"
   1.101 +      + "  arr[0] = data;\n"
   1.102 +      + "  run.run__V();\n"
   1.103 +      + "};\n"
   1.104 +      + "return true;\n"
   1.105 +    )
   1.106 +    private static boolean defineIfUnused(String name, Object[] arr, Runnable run) {
   1.107 +        return true;
   1.108 +    }
   1.109 +    
   1.110 +    @JavaScriptBody(args = { "s" }, body = "return eval('(' + s + ')');")
   1.111 +    static Object parse(String s) {
   1.112 +        return s;
   1.113 +    }
   1.114 +    
   1.115 +    @JavaScriptBody(args = { "url", "arr", "callback", "method", "data" }, body = ""
   1.116 +        + "var request = new XMLHttpRequest();\n"
   1.117 +        + "if (!method) method = 'GET';\n"
   1.118 +        + "request.open(method, url, true);\n"
   1.119 +        + "request.setRequestHeader('Content-Type', 'application/json; charset=utf-8');\n"
   1.120 +        + "request.onreadystatechange = function() {\n"
   1.121 +        + "  if (this.readyState!==4) return;\n"
   1.122 +        + "  try {\n"
   1.123 +        + "    arr[0] = eval('(' + this.response + ')');\n"
   1.124 +        + "  } catch (error) {;\n"
   1.125 +        + "    arr[0] = this.response;\n"
   1.126 +        + "  }\n"
   1.127 +        + "  callback.run__V();\n"
   1.128 +        + "};"
   1.129 +        + "if (data) request.send(data);"
   1.130 +        + "else request.send();"
   1.131 +    )
   1.132 +    static void loadJSON(
   1.133 +        String url, Object[] jsonResult, Runnable whenDone, String method, String data
   1.134 +    ) {
   1.135 +    }
   1.136 +    
   1.137 +    @JavaScriptBody(args = { "url", "jsonp" }, body = 
   1.138 +        "var scrpt = window.document.createElement('script');\n "
   1.139 +        + "scrpt.setAttribute('src', url);\n "
   1.140 +        + "scrpt.setAttribute('id', jsonp);\n "
   1.141 +        + "scrpt.setAttribute('type', 'text/javascript');\n "
   1.142 +        + "var body = document.getElementsByTagName('body')[0];\n "
   1.143 +        + "body.appendChild(scrpt);\n"
   1.144 +    )
   1.145 +    static void loadJSONP(String url, String jsonp) {
   1.146 +        
   1.147 +    }
   1.148 +    
   1.149 +    public static void extractJSON(Object jsonObject, String[] props, Object[] values) {
   1.150 +        for (int i = 0; i < props.length; i++) {
   1.151 +            values[i] = getProperty(jsonObject, props[i]);
   1.152 +        }
   1.153 +    }
   1.154 +    
   1.155 +}