2 * HTML via Java(tm) Language Bindings
3 * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. apidesign.org
13 * designates this particular file as subject to the
14 * "Classpath" exception as provided by apidesign.org
15 * in the License file that accompanied this code.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. Look for COPYING file in the top folder.
19 * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
21 package org.apidesign.html.ko2brwsr;
23 import org.apidesign.bck2brwsr.core.JavaScriptBody;
27 * @author Jaroslav Tulach <jtulach@netbeans.org>
29 final class ConvertTypes {
33 public static String toString(Object object, String property) {
34 Object ret = getProperty(object, property);
35 return ret == null ? null : ret.toString();
38 public static double toDouble(Object object, String property) {
39 Object ret = getProperty(object, property);
40 return ret instanceof Number ? ((Number)ret).doubleValue() : Double.NaN;
43 public static int toInt(Object object, String property) {
44 Object ret = getProperty(object, property);
45 return ret instanceof Number ? ((Number)ret).intValue() : Integer.MIN_VALUE;
48 public static <T> T toModel(Class<T> modelClass, Object object, String property) {
49 Object ret = getProperty(object, property);
50 if (ret == null || modelClass.isInstance(ret)) {
51 return modelClass.cast(ret);
53 throw new IllegalStateException("Value " + ret + " is not of type " + modelClass);
56 public static String toJSON(Object value) {
60 if (value instanceof Enum) {
61 value = value.toString();
63 if (value instanceof String) {
66 replace("\"", "\\\"").
72 return value.toString();
75 @JavaScriptBody(args = { "object", "property" },
76 body = "if (property === null) return object;\n"
77 + "var p = object[property]; return p ? p : null;"
79 private static Object getProperty(Object object, String property) {
83 public static String createJSONP(Object[] jsonResult, Runnable whenDone) {
84 int h = whenDone.hashCode();
87 name = "jsonp" + Integer.toHexString(h);
88 if (defineIfUnused(name, jsonResult, whenDone)) {
95 @JavaScriptBody(args = { "name", "arr", "run" }, body =
96 "if (window[name]) return false;\n "
97 + "window[name] = function(data) {\n "
98 + " delete window[name];\n"
99 + " var el = window.document.getElementById(name);\n"
100 + " el.parentNode.removeChild(el);\n"
101 + " arr[0] = data;\n"
106 private static boolean defineIfUnused(String name, Object[] arr, Runnable run) {
110 @JavaScriptBody(args = { "s" }, body = "return eval('(' + s + ')');")
111 static Object parse(String s) {
115 @JavaScriptBody(args = { "url", "arr", "callback", "method", "data" }, body = ""
116 + "var request = new XMLHttpRequest();\n"
117 + "if (!method) method = 'GET';\n"
118 + "request.open(method, url, true);\n"
119 + "request.setRequestHeader('Content-Type', 'application/json; charset=utf-8');\n"
120 + "request.onreadystatechange = function() {\n"
121 + " if (this.readyState!==4) return;\n"
123 + " arr[0] = eval('(' + this.response + ')');\n"
124 + " } catch (error) {;\n"
125 + " arr[0] = this.response;\n"
127 + " callback.run__V();\n"
129 + "if (data) request.send(data);"
130 + "else request.send();"
132 static void loadJSON(
133 String url, Object[] jsonResult, Runnable whenDone, String method, String data
137 @JavaScriptBody(args = { "url", "jsonp" }, body =
138 "var scrpt = window.document.createElement('script');\n "
139 + "scrpt.setAttribute('src', url);\n "
140 + "scrpt.setAttribute('id', jsonp);\n "
141 + "scrpt.setAttribute('type', 'text/javascript');\n "
142 + "var body = document.getElementsByTagName('body')[0];\n "
143 + "body.appendChild(scrpt);\n"
145 static void loadJSONP(String url, String jsonp) {
149 public static void extractJSON(Object jsonObject, String[] props, Object[] values) {
150 for (int i = 0; i < props.length; i++) {
151 values[i] = getProperty(jsonObject, props[i]);