1.1 --- a/ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/ConvertTypes.java Mon Jun 24 17:49:27 2013 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,155 +0,0 @@
1.4 -/**
1.5 - * HTML via Java(tm) Language Bindings
1.6 - * Copyright (C) 2013 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. apidesign.org
1.16 - * designates this particular file as subject to the
1.17 - * "Classpath" exception as provided by apidesign.org
1.18 - * in the License file that accompanied this code.
1.19 - *
1.20 - * You should have received a copy of the GNU General Public License
1.21 - * along with this program. Look for COPYING file in the top folder.
1.22 - * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
1.23 - */
1.24 -package org.apidesign.html.ko2brwsr;
1.25 -
1.26 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.27 -
1.28 -/**
1.29 - *
1.30 - * @author Jaroslav Tulach <jtulach@netbeans.org>
1.31 - */
1.32 -final class ConvertTypes {
1.33 - ConvertTypes() {
1.34 - }
1.35 -
1.36 - public static String toString(Object object, String property) {
1.37 - Object ret = getProperty(object, property);
1.38 - return ret == null ? null : ret.toString();
1.39 - }
1.40 -
1.41 - public static double toDouble(Object object, String property) {
1.42 - Object ret = getProperty(object, property);
1.43 - return ret instanceof Number ? ((Number)ret).doubleValue() : Double.NaN;
1.44 - }
1.45 -
1.46 - public static int toInt(Object object, String property) {
1.47 - Object ret = getProperty(object, property);
1.48 - return ret instanceof Number ? ((Number)ret).intValue() : Integer.MIN_VALUE;
1.49 - }
1.50 -
1.51 - public static <T> T toModel(Class<T> modelClass, Object object, String property) {
1.52 - Object ret = getProperty(object, property);
1.53 - if (ret == null || modelClass.isInstance(ret)) {
1.54 - return modelClass.cast(ret);
1.55 - }
1.56 - throw new IllegalStateException("Value " + ret + " is not of type " + modelClass);
1.57 - }
1.58 -
1.59 - public static String toJSON(Object value) {
1.60 - if (value == null) {
1.61 - return "null";
1.62 - }
1.63 - if (value instanceof Enum) {
1.64 - value = value.toString();
1.65 - }
1.66 - if (value instanceof String) {
1.67 - return '"' +
1.68 - ((String)value).
1.69 - replace("\"", "\\\"").
1.70 - replace("\n", "\\n").
1.71 - replace("\r", "\\r").
1.72 - replace("\t", "\\t")
1.73 - + '"';
1.74 - }
1.75 - return value.toString();
1.76 - }
1.77 -
1.78 - @JavaScriptBody(args = { "object", "property" },
1.79 - body = "if (property === null) return object;\n"
1.80 - + "var p = object[property]; return p ? p : null;"
1.81 - )
1.82 - private static Object getProperty(Object object, String property) {
1.83 - return null;
1.84 - }
1.85 -
1.86 - public static String createJSONP(Object[] jsonResult, Runnable whenDone) {
1.87 - int h = whenDone.hashCode();
1.88 - String name;
1.89 - for (;;) {
1.90 - name = "jsonp" + Integer.toHexString(h);
1.91 - if (defineIfUnused(name, jsonResult, whenDone)) {
1.92 - return name;
1.93 - }
1.94 - h++;
1.95 - }
1.96 - }
1.97 -
1.98 - @JavaScriptBody(args = { "name", "arr", "run" }, body =
1.99 - "if (window[name]) return false;\n "
1.100 - + "window[name] = function(data) {\n "
1.101 - + " delete window[name];\n"
1.102 - + " var el = window.document.getElementById(name);\n"
1.103 - + " el.parentNode.removeChild(el);\n"
1.104 - + " arr[0] = data;\n"
1.105 - + " run.run__V();\n"
1.106 - + "};\n"
1.107 - + "return true;\n"
1.108 - )
1.109 - private static boolean defineIfUnused(String name, Object[] arr, Runnable run) {
1.110 - return true;
1.111 - }
1.112 -
1.113 - @JavaScriptBody(args = { "s" }, body = "return eval('(' + s + ')');")
1.114 - static Object parse(String s) {
1.115 - return s;
1.116 - }
1.117 -
1.118 - @JavaScriptBody(args = { "url", "arr", "callback", "method", "data" }, body = ""
1.119 - + "var request = new XMLHttpRequest();\n"
1.120 - + "if (!method) method = 'GET';\n"
1.121 - + "request.open(method, url, true);\n"
1.122 - + "request.setRequestHeader('Content-Type', 'application/json; charset=utf-8');\n"
1.123 - + "request.onreadystatechange = function() {\n"
1.124 - + " if (this.readyState!==4) return;\n"
1.125 - + " try {\n"
1.126 - + " arr[0] = eval('(' + this.response + ')');\n"
1.127 - + " } catch (error) {;\n"
1.128 - + " arr[0] = this.response;\n"
1.129 - + " }\n"
1.130 - + " callback.run__V();\n"
1.131 - + "};"
1.132 - + "if (data) request.send(data);"
1.133 - + "else request.send();"
1.134 - )
1.135 - static void loadJSON(
1.136 - String url, Object[] jsonResult, Runnable whenDone, String method, String data
1.137 - ) {
1.138 - }
1.139 -
1.140 - @JavaScriptBody(args = { "url", "jsonp" }, body =
1.141 - "var scrpt = window.document.createElement('script');\n "
1.142 - + "scrpt.setAttribute('src', url);\n "
1.143 - + "scrpt.setAttribute('id', jsonp);\n "
1.144 - + "scrpt.setAttribute('type', 'text/javascript');\n "
1.145 - + "var body = document.getElementsByTagName('body')[0];\n "
1.146 - + "body.appendChild(scrpt);\n"
1.147 - )
1.148 - static void loadJSONP(String url, String jsonp) {
1.149 -
1.150 - }
1.151 -
1.152 - public static void extractJSON(Object jsonObject, String[] props, Object[] values) {
1.153 - for (int i = 0; i < props.length; i++) {
1.154 - values[i] = getProperty(jsonObject, props[i]);
1.155 - }
1.156 - }
1.157 -
1.158 -}