/
ConvertTypes.java
155 lines (139 loc) · 5.22 KB
/
ConvertTypes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* Back 2 Browser Bytecode Translator
* Copyright (C) 2012-2018 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
*
* 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.htmlpage;
import org.apidesign.bck2brwsr.core.JavaScriptBody;
/**
*
* @author Jaroslav Tulach <jtulach@netbeans.org>
*/
public 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> T toModel(Class<T> 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"
+ "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 = { "url", "arr", "callback" }, body = ""
+ "var request = new XMLHttpRequest();\n"
+ "request.open('GET', 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"
+ " throw 'Cannot parse' + error + ':' + this.response;\n"
+ " };\n"
+ " callback['run__V']();\n"
+ "};"
+ "request.send();"
)
private static void loadJSON(
String url, Object[] jsonResult, Runnable whenDone
) {
}
public static void loadJSON(
String url, Object[] jsonResult, Runnable whenDone, String jsonp
) {
if (jsonp == null) {
loadJSON(url, jsonResult, whenDone);
} else {
loadJSONP(url, jsonp);
}
}
@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"
)
private 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]);
}
}
}