jaroslav@813
|
1 |
/**
|
jaroslav@813
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@813
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@813
|
4 |
*
|
jaroslav@813
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@813
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@813
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@813
|
8 |
*
|
jaroslav@813
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@813
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@813
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@813
|
12 |
* GNU General Public License for more details.
|
jaroslav@813
|
13 |
*
|
jaroslav@813
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@813
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@813
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@813
|
17 |
*/
|
jaroslav@813
|
18 |
package org.apidesign.bck2brwsr.htmlpage;
|
jaroslav@813
|
19 |
|
jaroslav@992
|
20 |
import java.io.IOException;
|
jaroslav@992
|
21 |
import java.io.InputStreamReader;
|
jaroslav@992
|
22 |
import java.io.PushbackInputStream;
|
jaroslav@992
|
23 |
import java.io.Reader;
|
jaroslav@1011
|
24 |
import java.net.MalformedURLException;
|
jaroslav@992
|
25 |
import java.net.URL;
|
jaroslav@992
|
26 |
import java.util.Iterator;
|
jaroslav@992
|
27 |
import java.util.concurrent.Executor;
|
jaroslav@992
|
28 |
import java.util.concurrent.Executors;
|
jaroslav@992
|
29 |
import java.util.logging.Level;
|
jaroslav@992
|
30 |
import java.util.logging.Logger;
|
jaroslav@992
|
31 |
import javafx.application.Platform;
|
jaroslav@1011
|
32 |
import javafx.scene.web.WebEngine;
|
jaroslav@990
|
33 |
import netscape.javascript.JSObject;
|
jaroslav@813
|
34 |
import org.apidesign.bck2brwsr.core.JavaScriptBody;
|
jaroslav@992
|
35 |
import org.json.JSONArray;
|
jaroslav@992
|
36 |
import org.json.JSONException;
|
jaroslav@992
|
37 |
import org.json.JSONObject;
|
jaroslav@992
|
38 |
import org.json.JSONTokener;
|
jaroslav@813
|
39 |
|
jaroslav@813
|
40 |
/**
|
jaroslav@813
|
41 |
*
|
jaroslav@813
|
42 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@813
|
43 |
*/
|
jaroslav@813
|
44 |
public final class ConvertTypes {
|
jaroslav@992
|
45 |
private static final Logger LOG = Logger.getLogger(ConvertTypes.class.getName());
|
jaroslav@813
|
46 |
ConvertTypes() {
|
jaroslav@813
|
47 |
}
|
jaroslav@813
|
48 |
|
jaroslav@813
|
49 |
public static String toString(Object object, String property) {
|
jaroslav@813
|
50 |
Object ret = getProperty(object, property);
|
jaroslav@813
|
51 |
return ret == null ? null : ret.toString();
|
jaroslav@813
|
52 |
}
|
jaroslav@813
|
53 |
|
jaroslav@813
|
54 |
public static double toDouble(Object object, String property) {
|
jaroslav@813
|
55 |
Object ret = getProperty(object, property);
|
jaroslav@813
|
56 |
return ret instanceof Number ? ((Number)ret).doubleValue() : Double.NaN;
|
jaroslav@813
|
57 |
}
|
jaroslav@813
|
58 |
|
jaroslav@813
|
59 |
public static int toInt(Object object, String property) {
|
jaroslav@813
|
60 |
Object ret = getProperty(object, property);
|
jaroslav@813
|
61 |
return ret instanceof Number ? ((Number)ret).intValue() : Integer.MIN_VALUE;
|
jaroslav@813
|
62 |
}
|
jaroslav@906
|
63 |
|
jaroslav@906
|
64 |
public static <T> T toModel(Class<T> modelClass, Object object, String property) {
|
jaroslav@906
|
65 |
Object ret = getProperty(object, property);
|
jaroslav@990
|
66 |
if (ret instanceof JSObject) {
|
jaroslav@990
|
67 |
Object real = ((JSObject)ret).getMember("koModel");
|
jaroslav@990
|
68 |
if (real != null) {
|
jaroslav@990
|
69 |
ret = real;
|
jaroslav@990
|
70 |
}
|
jaroslav@990
|
71 |
}
|
jaroslav@906
|
72 |
if (ret == null || modelClass.isInstance(ret)) {
|
jaroslav@906
|
73 |
return modelClass.cast(ret);
|
jaroslav@906
|
74 |
}
|
jaroslav@906
|
75 |
throw new IllegalStateException("Value " + ret + " is not of type " + modelClass);
|
jaroslav@906
|
76 |
}
|
jaroslav@813
|
77 |
|
jaroslav@920
|
78 |
public static String toJSON(Object value) {
|
jaroslav@920
|
79 |
if (value == null) {
|
jaroslav@920
|
80 |
return "null";
|
jaroslav@920
|
81 |
}
|
jaroslav@920
|
82 |
if (value instanceof String) {
|
jaroslav@920
|
83 |
return '"' +
|
jaroslav@920
|
84 |
((String)value).
|
jaroslav@920
|
85 |
replace("\"", "\\\"").
|
jaroslav@920
|
86 |
replace("\n", "\\n").
|
jaroslav@920
|
87 |
replace("\r", "\\r").
|
jaroslav@920
|
88 |
replace("\t", "\\t")
|
jaroslav@920
|
89 |
+ '"';
|
jaroslav@920
|
90 |
}
|
jaroslav@920
|
91 |
return value.toString();
|
jaroslav@920
|
92 |
}
|
jaroslav@920
|
93 |
|
jaroslav@813
|
94 |
@JavaScriptBody(args = { "object", "property" },
|
jaroslav@879
|
95 |
body = "if (property === null) return object;\n"
|
jaroslav@879
|
96 |
+ "var p = object[property]; return p ? p : null;"
|
jaroslav@813
|
97 |
)
|
jaroslav@813
|
98 |
private static Object getProperty(Object object, String property) {
|
jaroslav@990
|
99 |
if (property == null) {
|
jaroslav@990
|
100 |
return object;
|
jaroslav@990
|
101 |
} else {
|
jaroslav@990
|
102 |
if (object instanceof JSObject) {
|
jaroslav@990
|
103 |
JSObject jo = (JSObject)object;
|
jaroslav@990
|
104 |
return jo.getMember(property);
|
jaroslav@990
|
105 |
}
|
jaroslav@990
|
106 |
return null;
|
jaroslav@990
|
107 |
}
|
jaroslav@813
|
108 |
}
|
jaroslav@954
|
109 |
|
jaroslav@1011
|
110 |
private static String findBaseURL() {
|
jaroslav@1011
|
111 |
WebEngine eng = (WebEngine) System.getProperties().get("webEngine");
|
jaroslav@1011
|
112 |
return (String)eng.executeScript(
|
jaroslav@1011
|
113 |
"var h;"
|
jaroslav@1011
|
114 |
+ "if (!!window && !!window.location && !!window.location.href)\n"
|
jaroslav@1011
|
115 |
+ " h = window.location.href;\n"
|
jaroslav@1011
|
116 |
+ "else "
|
jaroslav@1011
|
117 |
+ " h = null;"
|
jaroslav@1011
|
118 |
+ "h\n"
|
jaroslav@1011
|
119 |
);
|
jaroslav@1011
|
120 |
}
|
jaroslav@1011
|
121 |
|
jaroslav@954
|
122 |
public static String createJSONP(Object[] jsonResult, Runnable whenDone) {
|
jaroslav@992
|
123 |
return "json" + Integer.toHexString(whenDone.hashCode());
|
jaroslav@954
|
124 |
}
|
jaroslav@934
|
125 |
|
jaroslav@954
|
126 |
public static void loadJSON(
|
jaroslav@954
|
127 |
String url, Object[] jsonResult, Runnable whenDone, String jsonp
|
jaroslav@954
|
128 |
) {
|
jaroslav@992
|
129 |
REQ.execute(new LoadJSON(url, jsonResult, whenDone, jsonp));
|
jaroslav@992
|
130 |
}
|
jaroslav@992
|
131 |
|
jaroslav@992
|
132 |
private static final Executor REQ = Executors.newCachedThreadPool();
|
jaroslav@992
|
133 |
private static final class LoadJSON implements Runnable {
|
jaroslav@992
|
134 |
|
jaroslav@992
|
135 |
private final String url;
|
jaroslav@992
|
136 |
private final Object[] jsonResult;
|
jaroslav@992
|
137 |
private final Runnable whenDone;
|
jaroslav@992
|
138 |
private final String jsonp;
|
jaroslav@1011
|
139 |
private final URL base;
|
jaroslav@992
|
140 |
|
jaroslav@992
|
141 |
LoadJSON(String url, Object[] jsonResult, Runnable whenDone, String jsonp) {
|
jaroslav@992
|
142 |
this.url = url;
|
jaroslav@992
|
143 |
this.jsonResult = jsonResult;
|
jaroslav@992
|
144 |
this.whenDone = whenDone;
|
jaroslav@992
|
145 |
this.jsonp = jsonp;
|
jaroslav@1011
|
146 |
URL b;
|
jaroslav@1011
|
147 |
try {
|
jaroslav@1011
|
148 |
b = new URL(findBaseURL());
|
jaroslav@1011
|
149 |
} catch (MalformedURLException ex) {
|
jaroslav@1011
|
150 |
LOG.log(Level.SEVERE, "Can't find base url for " + url, ex);
|
jaroslav@1011
|
151 |
b = null;
|
jaroslav@1011
|
152 |
}
|
jaroslav@1011
|
153 |
this.base = b;
|
jaroslav@992
|
154 |
}
|
jaroslav@992
|
155 |
|
jaroslav@992
|
156 |
@Override
|
jaroslav@992
|
157 |
public void run() {
|
jaroslav@992
|
158 |
if (Platform.isFxApplicationThread()) {
|
jaroslav@992
|
159 |
whenDone.run();
|
jaroslav@992
|
160 |
return;
|
jaroslav@992
|
161 |
}
|
jaroslav@992
|
162 |
try {
|
jaroslav@1011
|
163 |
URL u = new URL(base, url.replace(" ", "%20"));
|
jaroslav@1012
|
164 |
PushbackInputStream is = new PushbackInputStream(u.openStream(), 1);
|
jaroslav@1012
|
165 |
boolean array = false;
|
jaroslav@992
|
166 |
if (jsonp != null) {
|
jaroslav@992
|
167 |
for (;;) {
|
jaroslav@1012
|
168 |
int ch = is.read();
|
jaroslav@992
|
169 |
if (ch == -1) {
|
jaroslav@992
|
170 |
break;
|
jaroslav@992
|
171 |
}
|
jaroslav@1012
|
172 |
if (ch == '[') {
|
jaroslav@1012
|
173 |
is.unread(ch);
|
jaroslav@1012
|
174 |
array = true;
|
jaroslav@1012
|
175 |
break;
|
jaroslav@1012
|
176 |
}
|
jaroslav@992
|
177 |
if (ch == '{') {
|
jaroslav@1012
|
178 |
is.unread(ch);
|
jaroslav@992
|
179 |
break;
|
jaroslav@992
|
180 |
}
|
jaroslav@992
|
181 |
}
|
jaroslav@1012
|
182 |
} else {
|
jaroslav@1012
|
183 |
int ch = is.read();
|
jaroslav@1012
|
184 |
array = ch == '[';
|
jaroslav@1012
|
185 |
is.unread(ch);
|
jaroslav@992
|
186 |
}
|
jaroslav@992
|
187 |
Reader r = new InputStreamReader(is, "UTF-8");
|
jaroslav@992
|
188 |
|
jaroslav@992
|
189 |
JSONTokener tok = new JSONTokener(r);
|
jaroslav@1012
|
190 |
Object obj = array ? new JSONArray(tok) : new JSONObject(tok);
|
jaroslav@992
|
191 |
jsonResult[0] = convertToArray(obj);
|
jaroslav@992
|
192 |
} catch (JSONException | IOException ex) {
|
jaroslav@992
|
193 |
jsonResult[0] = ex;
|
jaroslav@997
|
194 |
LOG.log(Level.WARNING, "Cannot connect to " + url, ex);
|
jaroslav@992
|
195 |
} finally {
|
jaroslav@992
|
196 |
Platform.runLater(this);
|
jaroslav@992
|
197 |
}
|
jaroslav@992
|
198 |
}
|
jaroslav@992
|
199 |
|
jaroslav@992
|
200 |
private static Object convertToArray(Object o) throws JSONException {
|
jaroslav@992
|
201 |
if (o instanceof JSONArray) {
|
jaroslav@992
|
202 |
JSONArray ja = (JSONArray)o;
|
jaroslav@992
|
203 |
Object[] arr = new Object[ja.length()];
|
jaroslav@992
|
204 |
for (int i = 0; i < arr.length; i++) {
|
jaroslav@992
|
205 |
arr[i] = convertToArray(ja.get(i));
|
jaroslav@992
|
206 |
}
|
jaroslav@992
|
207 |
return arr;
|
jaroslav@992
|
208 |
} else if (o instanceof JSONObject) {
|
jaroslav@992
|
209 |
JSONObject obj = (JSONObject)o;
|
jaroslav@992
|
210 |
Iterator it = obj.keys();
|
jaroslav@992
|
211 |
while (it.hasNext()) {
|
jaroslav@992
|
212 |
String key = (String)it.next();
|
jaroslav@992
|
213 |
obj.put(key, convertToArray(obj.get(key)));
|
jaroslav@992
|
214 |
}
|
jaroslav@992
|
215 |
return obj;
|
jaroslav@992
|
216 |
} else {
|
jaroslav@992
|
217 |
return o;
|
jaroslav@992
|
218 |
}
|
jaroslav@954
|
219 |
}
|
jaroslav@954
|
220 |
}
|
jaroslav@954
|
221 |
|
jaroslav@954
|
222 |
@JavaScriptBody(args = { "url", "jsonp" }, body =
|
jaroslav@954
|
223 |
"var scrpt = window.document.createElement('script');\n "
|
jaroslav@954
|
224 |
+ "scrpt.setAttribute('src', url);\n "
|
jaroslav@954
|
225 |
+ "scrpt.setAttribute('id', jsonp);\n "
|
jaroslav@954
|
226 |
+ "scrpt.setAttribute('type', 'text/javascript');\n "
|
jaroslav@954
|
227 |
+ "var body = document.getElementsByTagName('body')[0];\n "
|
jaroslav@954
|
228 |
+ "body.appendChild(scrpt);\n"
|
jaroslav@954
|
229 |
)
|
jaroslav@954
|
230 |
private static void loadJSONP(String url, String jsonp) {
|
jaroslav@954
|
231 |
|
jaroslav@954
|
232 |
}
|
jaroslav@954
|
233 |
|
jaroslav@934
|
234 |
public static void extractJSON(Object jsonObject, String[] props, Object[] values) {
|
jaroslav@992
|
235 |
if (jsonObject instanceof JSONObject) {
|
jaroslav@992
|
236 |
JSONObject obj = (JSONObject)jsonObject;
|
jaroslav@992
|
237 |
for (int i = 0; i < props.length; i++) {
|
jaroslav@992
|
238 |
try {
|
jaroslav@1012
|
239 |
values[i] = obj.has(props[i]) ? obj.get(props[i]) : null;
|
jaroslav@992
|
240 |
} catch (JSONException ex) {
|
jaroslav@992
|
241 |
LOG.log(Level.SEVERE, "Can't read " + props[i] + " from " + jsonObject, ex);
|
jaroslav@992
|
242 |
}
|
jaroslav@992
|
243 |
}
|
jaroslav@992
|
244 |
|
jaroslav@934
|
245 |
}
|
jaroslav@992
|
246 |
|
jaroslav@934
|
247 |
}
|
jaroslav@934
|
248 |
|
jaroslav@813
|
249 |
}
|