jtulach@2
|
1 |
/**
|
jaroslav@358
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
jtulach@2
|
3 |
*
|
jaroslav@551
|
4 |
* Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
|
jtulach@2
|
5 |
*
|
jaroslav@358
|
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
|
jaroslav@358
|
7 |
* Other names may be trademarks of their respective owners.
|
jtulach@2
|
8 |
*
|
jaroslav@358
|
9 |
* The contents of this file are subject to the terms of either the GNU
|
jaroslav@358
|
10 |
* General Public License Version 2 only ("GPL") or the Common
|
jaroslav@358
|
11 |
* Development and Distribution License("CDDL") (collectively, the
|
jaroslav@358
|
12 |
* "License"). You may not use this file except in compliance with the
|
jaroslav@358
|
13 |
* License. You can obtain a copy of the License at
|
jaroslav@358
|
14 |
* http://www.netbeans.org/cddl-gplv2.html
|
jaroslav@358
|
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
|
jaroslav@358
|
16 |
* specific language governing permissions and limitations under the
|
jaroslav@358
|
17 |
* License. When distributing the software, include this License Header
|
jaroslav@358
|
18 |
* Notice in each file and include the License file at
|
jaroslav@358
|
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
|
jaroslav@358
|
20 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@358
|
21 |
* by Oracle in the GPL Version 2 section of the License file that
|
jaroslav@358
|
22 |
* accompanied this code. If applicable, add the following below the
|
jaroslav@358
|
23 |
* License Header, with the fields enclosed by brackets [] replaced by
|
jaroslav@358
|
24 |
* your own identifying information:
|
jaroslav@358
|
25 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
jaroslav@358
|
26 |
*
|
jaroslav@358
|
27 |
* Contributor(s):
|
jaroslav@358
|
28 |
*
|
jaroslav@358
|
29 |
* The Original Software is NetBeans. The Initial Developer of the Original
|
jaroslav@551
|
30 |
* Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
|
jaroslav@358
|
31 |
*
|
jaroslav@358
|
32 |
* If you wish your version of this file to be governed by only the CDDL
|
jaroslav@358
|
33 |
* or only the GPL Version 2, indicate your decision by adding
|
jaroslav@358
|
34 |
* "[Contributor] elects to include this software in this distribution
|
jaroslav@358
|
35 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a
|
jaroslav@358
|
36 |
* single choice of license, a recipient has the option to distribute
|
jaroslav@358
|
37 |
* your version of this file under either the CDDL, the GPL Version 2 or
|
jaroslav@358
|
38 |
* to extend the choice of license to its licensees as provided above.
|
jaroslav@358
|
39 |
* However, if you add GPL Version 2 code and therefore, elected the GPL
|
jaroslav@358
|
40 |
* Version 2 license, then the option applies only if the new code is
|
jaroslav@358
|
41 |
* made subject to such option by the copyright holder.
|
jtulach@2
|
42 |
*/
|
jaroslav@362
|
43 |
package org.netbeans.html.json.impl;
|
jtulach@2
|
44 |
|
jaroslav@60
|
45 |
import java.io.IOException;
|
jaroslav@60
|
46 |
import java.io.InputStream;
|
jaroslav@414
|
47 |
import java.util.Collection;
|
jaroslav@27
|
48 |
import java.util.HashMap;
|
jaroslav@27
|
49 |
import java.util.Map;
|
jaroslav@110
|
50 |
import net.java.html.BrwsrCtx;
|
jaroslav@110
|
51 |
import org.apidesign.html.context.spi.Contexts;
|
jaroslav@110
|
52 |
import org.apidesign.html.json.spi.FunctionBinding;
|
jaroslav@24
|
53 |
import org.apidesign.html.json.spi.JSONCall;
|
jaroslav@110
|
54 |
import org.apidesign.html.json.spi.PropertyBinding;
|
jaroslav@374
|
55 |
import org.apidesign.html.json.spi.Proto;
|
jaroslav@38
|
56 |
import org.apidesign.html.json.spi.Technology;
|
jaroslav@23
|
57 |
import org.apidesign.html.json.spi.Transfer;
|
jaroslav@258
|
58 |
import org.apidesign.html.json.spi.WSTransfer;
|
jaroslav@22
|
59 |
|
jtulach@2
|
60 |
/**
|
jtulach@2
|
61 |
*
|
jtulach@2
|
62 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jtulach@2
|
63 |
*/
|
jtulach@2
|
64 |
public final class JSON {
|
jtulach@2
|
65 |
private JSON() {
|
jtulach@2
|
66 |
}
|
jtulach@2
|
67 |
|
jaroslav@110
|
68 |
static Technology<?> findTechnology(BrwsrCtx c) {
|
jaroslav@110
|
69 |
Technology<?> t = Contexts.find(c, Technology.class);
|
jaroslav@110
|
70 |
return t == null ? EmptyTech.EMPTY : t;
|
jaroslav@110
|
71 |
}
|
jaroslav@110
|
72 |
|
jaroslav@110
|
73 |
static Transfer findTransfer(BrwsrCtx c) {
|
jaroslav@110
|
74 |
Transfer t = Contexts.find(c, Transfer.class);
|
jaroslav@110
|
75 |
return t == null ? EmptyTech.EMPTY : t;
|
jaroslav@110
|
76 |
}
|
jaroslav@255
|
77 |
|
jaroslav@258
|
78 |
static WSTransfer<?> findWSTransfer(BrwsrCtx c) {
|
jaroslav@258
|
79 |
WSTransfer<?> t = Contexts.find(c, WSTransfer.class);
|
jaroslav@258
|
80 |
return t == null ? EmptyTech.EMPTY : t;
|
jaroslav@258
|
81 |
}
|
jaroslav@258
|
82 |
|
jaroslav@240
|
83 |
public static void runInBrowser(BrwsrCtx c, Runnable runnable) {
|
jaroslav@240
|
84 |
findTechnology(c).runSafe(runnable);
|
jaroslav@240
|
85 |
}
|
jaroslav@240
|
86 |
|
jaroslav@110
|
87 |
public static void extract(BrwsrCtx c, Object value, String[] props, Object[] values) {
|
jaroslav@110
|
88 |
Transfer t = findTransfer(c);
|
jaroslav@22
|
89 |
t.extract(value, props, values);
|
jtulach@2
|
90 |
}
|
jaroslav@21
|
91 |
|
jaroslav@110
|
92 |
private static Object getProperty(BrwsrCtx c, Object obj, String prop) {
|
jaroslav@21
|
93 |
if (prop == null) return obj;
|
jaroslav@21
|
94 |
|
jaroslav@21
|
95 |
String[] arr = { prop };
|
jaroslav@21
|
96 |
Object[] val = { null };
|
jaroslav@22
|
97 |
extract(c, obj, arr, val);
|
jaroslav@21
|
98 |
return val[0];
|
jaroslav@21
|
99 |
}
|
jtulach@2
|
100 |
|
jaroslav@380
|
101 |
public static String toJSON(Object value) {
|
jaroslav@18
|
102 |
if (value == null) {
|
jaroslav@18
|
103 |
return "null";
|
jaroslav@18
|
104 |
}
|
jaroslav@18
|
105 |
if (value instanceof Enum) {
|
jaroslav@18
|
106 |
value = value.toString();
|
jaroslav@18
|
107 |
}
|
jaroslav@18
|
108 |
if (value instanceof String) {
|
jaroslav@329
|
109 |
String s = (String)value;
|
jaroslav@329
|
110 |
int len = s.length();
|
jaroslav@329
|
111 |
StringBuilder sb = new StringBuilder(len + 10);
|
jaroslav@329
|
112 |
sb.append('"');
|
jaroslav@329
|
113 |
for (int i = 0; i < len; i++) {
|
jaroslav@329
|
114 |
char ch = s.charAt(i);
|
jaroslav@329
|
115 |
switch (ch) {
|
jaroslav@329
|
116 |
case '\"': sb.append("\\\""); break;
|
jaroslav@329
|
117 |
case '\n': sb.append("\\n"); break;
|
jaroslav@329
|
118 |
case '\r': sb.append("\\r"); break;
|
jaroslav@329
|
119 |
case '\t': sb.append("\\t"); break;
|
jaroslav@329
|
120 |
case '\\': sb.append("\\\\"); break;
|
jaroslav@329
|
121 |
default: sb.append(ch);
|
jaroslav@329
|
122 |
}
|
jaroslav@329
|
123 |
}
|
jaroslav@329
|
124 |
sb.append('"');
|
jaroslav@329
|
125 |
return sb.toString();
|
jaroslav@18
|
126 |
}
|
jaroslav@18
|
127 |
return value.toString();
|
jtulach@2
|
128 |
}
|
jaroslav@12
|
129 |
|
jaroslav@110
|
130 |
public static String toString(BrwsrCtx c, Object obj, String prop) {
|
jaroslav@22
|
131 |
obj = getProperty(c, obj, prop);
|
jaroslav@21
|
132 |
return obj instanceof String ? (String)obj : null;
|
jaroslav@13
|
133 |
}
|
jaroslav@110
|
134 |
public static Number toNumber(BrwsrCtx c, Object obj, String prop) {
|
jaroslav@22
|
135 |
obj = getProperty(c, obj, prop);
|
jaroslav@22
|
136 |
if (!(obj instanceof Number)) {
|
jaroslav@22
|
137 |
obj = Double.NaN;
|
jaroslav@22
|
138 |
}
|
jaroslav@22
|
139 |
return (Number)obj;
|
jaroslav@22
|
140 |
}
|
jaroslav@110
|
141 |
public static <M> M toModel(BrwsrCtx c, Class<M> aClass, Object data, Object object) {
|
jaroslav@38
|
142 |
Technology<?> t = findTechnology(c);
|
jaroslav@38
|
143 |
Object o = t.toModel(aClass, data);
|
jaroslav@38
|
144 |
return aClass.cast(o);
|
jaroslav@30
|
145 |
}
|
jaroslav@280
|
146 |
|
jaroslav@280
|
147 |
public static boolean isSame(int a, int b) {
|
jaroslav@280
|
148 |
return a == b;
|
jaroslav@280
|
149 |
}
|
jaroslav@280
|
150 |
|
jaroslav@280
|
151 |
public static boolean isSame(double a, double b) {
|
jaroslav@280
|
152 |
return a == b;
|
jaroslav@280
|
153 |
}
|
jaroslav@280
|
154 |
|
jaroslav@280
|
155 |
public static boolean isSame(Object a, Object b) {
|
jaroslav@280
|
156 |
if (a == b) {
|
jaroslav@280
|
157 |
return true;
|
jaroslav@280
|
158 |
}
|
jaroslav@280
|
159 |
if (a == null || b == null) {
|
jaroslav@280
|
160 |
return false;
|
jaroslav@280
|
161 |
}
|
jaroslav@280
|
162 |
return a.equals(b);
|
jaroslav@280
|
163 |
}
|
jaroslav@281
|
164 |
|
jaroslav@281
|
165 |
public static int hashPlus(Object o, int h) {
|
jaroslav@281
|
166 |
return o == null ? h : h ^ o.hashCode();
|
jaroslav@281
|
167 |
}
|
jaroslav@22
|
168 |
|
jaroslav@197
|
169 |
public static <T> T extractValue(Class<T> type, Object val) {
|
jaroslav@197
|
170 |
if (Number.class.isAssignableFrom(type)) {
|
jaroslav@197
|
171 |
val = numberValue(val);
|
jaroslav@197
|
172 |
}
|
jaroslav@197
|
173 |
if (Boolean.class == type) {
|
jaroslav@197
|
174 |
val = boolValue(val);
|
jaroslav@197
|
175 |
}
|
jaroslav@307
|
176 |
if (String.class == type) {
|
jaroslav@307
|
177 |
val = stringValue(val);
|
jaroslav@307
|
178 |
}
|
jaroslav@307
|
179 |
if (Character.class == type) {
|
jaroslav@307
|
180 |
val = charValue(val);
|
jaroslav@307
|
181 |
}
|
jaroslav@307
|
182 |
if (Integer.class == type) {
|
jaroslav@307
|
183 |
val = val instanceof Number ? ((Number)val).intValue() : 0;
|
jaroslav@307
|
184 |
}
|
jaroslav@307
|
185 |
if (Long.class == type) {
|
jaroslav@307
|
186 |
val = val instanceof Number ? ((Number)val).longValue() : 0;
|
jaroslav@307
|
187 |
}
|
jaroslav@307
|
188 |
if (Short.class == type) {
|
jaroslav@307
|
189 |
val = val instanceof Number ? ((Number)val).shortValue() : 0;
|
jaroslav@307
|
190 |
}
|
jaroslav@307
|
191 |
if (Byte.class == type) {
|
jaroslav@307
|
192 |
val = val instanceof Number ? ((Number)val).byteValue() : 0;
|
jaroslav@307
|
193 |
}
|
jaroslav@307
|
194 |
if (Double.class == type) {
|
jaroslav@307
|
195 |
val = val instanceof Number ? ((Number)val).doubleValue() : Double.NaN;
|
jaroslav@307
|
196 |
}
|
jaroslav@307
|
197 |
if (Float.class == type) {
|
jaroslav@307
|
198 |
val = val instanceof Number ? ((Number)val).floatValue() : Float.NaN;
|
jaroslav@307
|
199 |
}
|
jaroslav@197
|
200 |
return type.cast(val);
|
jaroslav@197
|
201 |
}
|
jaroslav@197
|
202 |
|
jaroslav@380
|
203 |
static boolean isNumeric(Object val) {
|
jaroslav@307
|
204 |
return ((val instanceof Integer) || (val instanceof Long) || (val instanceof Short) || (val instanceof Byte));
|
jaroslav@307
|
205 |
}
|
jaroslav@307
|
206 |
|
jaroslav@197
|
207 |
public static String stringValue(Object val) {
|
jaroslav@307
|
208 |
if (val instanceof Boolean) {
|
jaroslav@307
|
209 |
return ((Boolean)val ? "true" : "false");
|
jaroslav@307
|
210 |
}
|
jaroslav@307
|
211 |
if (isNumeric(val)) {
|
jaroslav@307
|
212 |
return Long.toString(((Number)val).longValue());
|
jaroslav@307
|
213 |
}
|
jaroslav@307
|
214 |
if (val instanceof Float) {
|
jaroslav@307
|
215 |
return Float.toString((Float)val);
|
jaroslav@307
|
216 |
}
|
jaroslav@307
|
217 |
if (val instanceof Double) {
|
jaroslav@307
|
218 |
return Double.toString((Double)val);
|
jaroslav@307
|
219 |
}
|
jaroslav@197
|
220 |
return (String)val;
|
jaroslav@197
|
221 |
}
|
jaroslav@380
|
222 |
|
jaroslav@197
|
223 |
public static Number numberValue(Object val) {
|
jaroslav@197
|
224 |
if (val instanceof String) {
|
jaroslav@197
|
225 |
try {
|
jaroslav@197
|
226 |
return Double.valueOf((String)val);
|
jaroslav@197
|
227 |
} catch (NumberFormatException ex) {
|
jaroslav@197
|
228 |
return Double.NaN;
|
jaroslav@197
|
229 |
}
|
jaroslav@197
|
230 |
}
|
jaroslav@307
|
231 |
if (val instanceof Boolean) {
|
jaroslav@307
|
232 |
return (Boolean)val ? 1 : 0;
|
jaroslav@307
|
233 |
}
|
jaroslav@197
|
234 |
return (Number)val;
|
jaroslav@197
|
235 |
}
|
jaroslav@197
|
236 |
|
jaroslav@197
|
237 |
public static Character charValue(Object val) {
|
jaroslav@307
|
238 |
if (val instanceof Number) {
|
jaroslav@307
|
239 |
return Character.toChars(numberValue(val).intValue())[0];
|
jaroslav@307
|
240 |
}
|
jaroslav@307
|
241 |
if (val instanceof Boolean) {
|
jaroslav@307
|
242 |
return (Boolean)val ? (char)1 : (char)0;
|
jaroslav@307
|
243 |
}
|
jaroslav@307
|
244 |
if (val instanceof String) {
|
jaroslav@307
|
245 |
String s = (String)val;
|
jaroslav@307
|
246 |
return s.isEmpty() ? (char)0 : s.charAt(0);
|
jaroslav@307
|
247 |
}
|
jaroslav@197
|
248 |
return (Character)val;
|
jaroslav@197
|
249 |
}
|
jaroslav@250
|
250 |
|
jaroslav@197
|
251 |
public static Boolean boolValue(Object val) {
|
jaroslav@197
|
252 |
if (val instanceof String) {
|
jaroslav@197
|
253 |
return Boolean.parseBoolean((String)val);
|
jaroslav@197
|
254 |
}
|
jaroslav@307
|
255 |
if (val instanceof Number) {
|
jaroslav@307
|
256 |
return numberValue(val).doubleValue() != 0.0;
|
jaroslav@307
|
257 |
}
|
jaroslav@307
|
258 |
|
jaroslav@197
|
259 |
return Boolean.TRUE.equals(val);
|
jaroslav@197
|
260 |
}
|
jaroslav@13
|
261 |
|
jaroslav@414
|
262 |
public static Object find(Object object, Bindings model) {
|
jaroslav@414
|
263 |
if (object == null) {
|
jaroslav@414
|
264 |
return null;
|
jaroslav@414
|
265 |
}
|
jaroslav@414
|
266 |
if (object instanceof JSONList) {
|
jaroslav@414
|
267 |
return ((JSONList<?>) object).koData();
|
jaroslav@414
|
268 |
}
|
jaroslav@414
|
269 |
if (object instanceof Collection) {
|
jaroslav@414
|
270 |
return JSONList.koData((Collection<?>) object, model);
|
jaroslav@414
|
271 |
}
|
jaroslav@453
|
272 |
Proto proto = findProto(object);
|
jaroslav@414
|
273 |
if (proto == null) {
|
jaroslav@414
|
274 |
return null;
|
jaroslav@414
|
275 |
}
|
jaroslav@414
|
276 |
final Bindings b = PropertyBindingAccessor.getBindings(proto, true);
|
jaroslav@414
|
277 |
return b == null ? null : b.koData();
|
jaroslav@414
|
278 |
}
|
jaroslav@453
|
279 |
|
jaroslav@453
|
280 |
private static Proto findProto(Object object) {
|
jaroslav@453
|
281 |
Proto.Type<?> type = JSON.findType(object.getClass());
|
jaroslav@453
|
282 |
if (type == null) {
|
jaroslav@453
|
283 |
return null;
|
jaroslav@453
|
284 |
}
|
jaroslav@453
|
285 |
final Proto proto = PropertyBindingAccessor.protoFor(type, object);
|
jaroslav@453
|
286 |
return proto;
|
jaroslav@453
|
287 |
}
|
jaroslav@414
|
288 |
|
jaroslav@414
|
289 |
public static Object find(Object object) {
|
jaroslav@414
|
290 |
return find(object, null);
|
jaroslav@414
|
291 |
}
|
jaroslav@414
|
292 |
|
jaroslav@453
|
293 |
public static void applyBindings(Object object) {
|
jaroslav@453
|
294 |
final Proto proto = findProto(object);
|
jaroslav@453
|
295 |
if (proto == null) {
|
jaroslav@453
|
296 |
throw new IllegalArgumentException("Not a model: " + object.getClass());
|
jaroslav@453
|
297 |
}
|
jaroslav@453
|
298 |
proto.applyBindings();
|
jaroslav@453
|
299 |
}
|
jaroslav@453
|
300 |
|
jaroslav@24
|
301 |
public static void loadJSON(
|
jaroslav@255
|
302 |
BrwsrCtx c, RcvrJSON callback,
|
jaroslav@75
|
303 |
String urlBefore, String urlAfter, String method,
|
jaroslav@75
|
304 |
Object data
|
jaroslav@75
|
305 |
) {
|
jaroslav@262
|
306 |
JSONCall call = PropertyBindingAccessor.createCall(c, callback, urlBefore, urlAfter, method, data);
|
jaroslav@110
|
307 |
Transfer t = findTransfer(c);
|
jaroslav@24
|
308 |
t.loadJSON(call);
|
jaroslav@12
|
309 |
}
|
jaroslav@258
|
310 |
public static WS openWS(
|
jaroslav@258
|
311 |
BrwsrCtx c, RcvrJSON r, String url, Object data
|
jaroslav@258
|
312 |
) {
|
jaroslav@258
|
313 |
WS ws = WSImpl.create(findWSTransfer(c), r);
|
jaroslav@262
|
314 |
ws.send(c, url, data);
|
jaroslav@258
|
315 |
return ws;
|
jaroslav@258
|
316 |
}
|
jaroslav@258
|
317 |
|
jaroslav@258
|
318 |
public static abstract class WS {
|
jaroslav@258
|
319 |
private WS() {
|
jaroslav@258
|
320 |
}
|
jaroslav@258
|
321 |
|
jaroslav@262
|
322 |
public abstract void send(BrwsrCtx ctx, String url, Object model);
|
jaroslav@258
|
323 |
}
|
jaroslav@258
|
324 |
|
jaroslav@258
|
325 |
private static final class WSImpl<Socket> extends WS {
|
jaroslav@258
|
326 |
|
jaroslav@258
|
327 |
private final WSTransfer<Socket> trans;
|
jaroslav@258
|
328 |
private final RcvrJSON rcvr;
|
jaroslav@258
|
329 |
private Socket socket;
|
jaroslav@258
|
330 |
private String prevURL;
|
jaroslav@258
|
331 |
|
jaroslav@258
|
332 |
private WSImpl(WSTransfer<Socket> trans, RcvrJSON rcvr) {
|
jaroslav@258
|
333 |
this.trans = trans;
|
jaroslav@258
|
334 |
this.rcvr = rcvr;
|
jaroslav@258
|
335 |
}
|
jaroslav@258
|
336 |
|
jaroslav@258
|
337 |
static <Socket> WS create(WSTransfer<Socket> t, RcvrJSON r) {
|
jaroslav@258
|
338 |
return new WSImpl<Socket>(t, r);
|
jaroslav@258
|
339 |
}
|
jaroslav@258
|
340 |
|
jaroslav@258
|
341 |
@Override
|
jaroslav@262
|
342 |
public void send(BrwsrCtx ctx, String url, Object data) {
|
jaroslav@258
|
343 |
Socket s = socket;
|
jaroslav@258
|
344 |
if (s == null) {
|
jaroslav@258
|
345 |
if (data != null) {
|
jaroslav@258
|
346 |
throw new IllegalStateException("WebSocket is not opened yet. Call with null data, was: " + data);
|
jaroslav@258
|
347 |
}
|
jaroslav@262
|
348 |
JSONCall call = PropertyBindingAccessor.createCall(ctx, rcvr, url, null, "WebSocket", null);
|
jaroslav@258
|
349 |
socket = trans.open(url, call);
|
jaroslav@258
|
350 |
prevURL = url;
|
jaroslav@258
|
351 |
return;
|
jaroslav@258
|
352 |
}
|
jaroslav@258
|
353 |
if (data == null) {
|
jaroslav@258
|
354 |
trans.close(s);
|
jaroslav@258
|
355 |
socket = null;
|
jaroslav@258
|
356 |
return;
|
jaroslav@258
|
357 |
}
|
jaroslav@258
|
358 |
if (!prevURL.equals(url)) {
|
jaroslav@258
|
359 |
throw new IllegalStateException(
|
jaroslav@258
|
360 |
"Can't call to different URL " + url + " was: " + prevURL + "!"
|
jaroslav@258
|
361 |
+ " Close the socket by calling it will null data first!"
|
jaroslav@258
|
362 |
);
|
jaroslav@258
|
363 |
}
|
jaroslav@262
|
364 |
JSONCall call = PropertyBindingAccessor.createCall(ctx, rcvr, prevURL, null, "WebSocket", data);
|
jaroslav@258
|
365 |
trans.send(s, call);
|
jaroslav@258
|
366 |
}
|
jaroslav@258
|
367 |
|
jaroslav@258
|
368 |
}
|
jaroslav@27
|
369 |
|
jaroslav@374
|
370 |
private static final Map<Class,Proto.Type<?>> modelTypes;
|
jaroslav@27
|
371 |
static {
|
jaroslav@374
|
372 |
modelTypes = new HashMap<Class, Proto.Type<?>>();
|
jaroslav@27
|
373 |
}
|
jaroslav@374
|
374 |
public static void register(Class c, Proto.Type<?> type) {
|
jaroslav@374
|
375 |
modelTypes.put(c, type);
|
jaroslav@27
|
376 |
}
|
jaroslav@27
|
377 |
|
jaroslav@59
|
378 |
public static boolean isModel(Class<?> clazz) {
|
jaroslav@374
|
379 |
return findType(clazz) != null;
|
jaroslav@111
|
380 |
}
|
jaroslav@111
|
381 |
|
jaroslav@384
|
382 |
static Proto.Type<?> findType(Class<?> clazz) {
|
jaroslav@59
|
383 |
for (int i = 0; i < 2; i++) {
|
jaroslav@374
|
384 |
Proto.Type<?> from = modelTypes.get(clazz);
|
jaroslav@59
|
385 |
if (from == null) {
|
jaroslav@59
|
386 |
initClass(clazz);
|
jaroslav@59
|
387 |
} else {
|
jaroslav@111
|
388 |
return from;
|
jaroslav@59
|
389 |
}
|
jaroslav@59
|
390 |
}
|
jaroslav@111
|
391 |
return null;
|
jaroslav@111
|
392 |
}
|
jaroslav@111
|
393 |
|
jaroslav@111
|
394 |
public static <Model> Model bindTo(Model model, BrwsrCtx c) {
|
jaroslav@412
|
395 |
Proto.Type<Model> from = (Proto.Type<Model>) findType(model.getClass());
|
jaroslav@111
|
396 |
if (from == null) {
|
jaroslav@111
|
397 |
throw new IllegalArgumentException();
|
jaroslav@111
|
398 |
}
|
jaroslav@412
|
399 |
return PropertyBindingAccessor.clone(from, model, c);
|
jaroslav@59
|
400 |
}
|
jaroslav@59
|
401 |
|
jaroslav@110
|
402 |
public static <T> T readStream(BrwsrCtx c, Class<T> modelClazz, InputStream data)
|
jaroslav@60
|
403 |
throws IOException {
|
jaroslav@110
|
404 |
Transfer tr = findTransfer(c);
|
jaroslav@60
|
405 |
return read(c, modelClazz, tr.toJSON((InputStream)data));
|
jaroslav@60
|
406 |
}
|
jaroslav@110
|
407 |
public static <T> T read(BrwsrCtx c, Class<T> modelClazz, Object data) {
|
jaroslav@250
|
408 |
if (data == null) {
|
jaroslav@250
|
409 |
return null;
|
jaroslav@250
|
410 |
}
|
jaroslav@73
|
411 |
if (modelClazz == String.class) {
|
jaroslav@73
|
412 |
return modelClazz.cast(data.toString());
|
jaroslav@73
|
413 |
}
|
jaroslav@30
|
414 |
for (int i = 0; i < 2; i++) {
|
jaroslav@374
|
415 |
Proto.Type<?> from = modelTypes.get(modelClazz);
|
jaroslav@30
|
416 |
if (from == null) {
|
jaroslav@30
|
417 |
initClass(modelClazz);
|
jaroslav@30
|
418 |
} else {
|
jaroslav@412
|
419 |
return modelClazz.cast(PropertyBindingAccessor.readFrom(from, c, data));
|
jaroslav@30
|
420 |
}
|
jaroslav@27
|
421 |
}
|
jaroslav@30
|
422 |
throw new NullPointerException();
|
jaroslav@30
|
423 |
}
|
jaroslav@30
|
424 |
static void initClass(Class<?> modelClazz) {
|
jaroslav@30
|
425 |
try {
|
jaroslav@30
|
426 |
// try to resolve the class
|
jaroslav@30
|
427 |
ClassLoader l;
|
jaroslav@30
|
428 |
try {
|
jaroslav@30
|
429 |
l = modelClazz.getClassLoader();
|
jaroslav@30
|
430 |
} catch (SecurityException ex) {
|
jaroslav@30
|
431 |
l = null;
|
jaroslav@30
|
432 |
}
|
jaroslav@30
|
433 |
if (l != null) {
|
jaroslav@30
|
434 |
Class.forName(modelClazz.getName(), true, l);
|
jaroslav@30
|
435 |
}
|
jaroslav@30
|
436 |
modelClazz.newInstance();
|
jaroslav@100
|
437 |
} catch (Exception ex) {
|
jaroslav@30
|
438 |
// ignore and try again
|
jaroslav@30
|
439 |
}
|
jaroslav@27
|
440 |
}
|
jaroslav@110
|
441 |
|
jaroslav@258
|
442 |
private static final class EmptyTech
|
jaroslav@258
|
443 |
implements Technology<Object>, Transfer, WSTransfer<Void> {
|
jaroslav@110
|
444 |
private static final EmptyTech EMPTY = new EmptyTech();
|
jaroslav@110
|
445 |
|
jaroslav@110
|
446 |
@Override
|
jaroslav@110
|
447 |
public Object wrapModel(Object model) {
|
jaroslav@110
|
448 |
return model;
|
jaroslav@110
|
449 |
}
|
jaroslav@110
|
450 |
|
jaroslav@110
|
451 |
@Override
|
jaroslav@110
|
452 |
public void valueHasMutated(Object data, String propertyName) {
|
jaroslav@110
|
453 |
}
|
jaroslav@110
|
454 |
|
jaroslav@110
|
455 |
@Override
|
jaroslav@110
|
456 |
public void bind(PropertyBinding b, Object model, Object data) {
|
jaroslav@110
|
457 |
}
|
jaroslav@110
|
458 |
|
jaroslav@110
|
459 |
@Override
|
jaroslav@110
|
460 |
public void expose(FunctionBinding fb, Object model, Object d) {
|
jaroslav@110
|
461 |
}
|
jaroslav@110
|
462 |
|
jaroslav@110
|
463 |
@Override
|
jaroslav@110
|
464 |
public void applyBindings(Object data) {
|
jaroslav@110
|
465 |
}
|
jaroslav@110
|
466 |
|
jaroslav@110
|
467 |
@Override
|
jaroslav@110
|
468 |
public Object wrapArray(Object[] arr) {
|
jaroslav@110
|
469 |
return arr;
|
jaroslav@110
|
470 |
}
|
jaroslav@110
|
471 |
|
jaroslav@110
|
472 |
@Override
|
jaroslav@110
|
473 |
public void extract(Object obj, String[] props, Object[] values) {
|
jaroslav@110
|
474 |
for (int i = 0; i < values.length; i++) {
|
jaroslav@110
|
475 |
values[i] = null;
|
jaroslav@110
|
476 |
}
|
jaroslav@110
|
477 |
}
|
jaroslav@110
|
478 |
|
jaroslav@110
|
479 |
@Override
|
jaroslav@110
|
480 |
public void loadJSON(JSONCall call) {
|
jaroslav@110
|
481 |
call.notifyError(new UnsupportedOperationException());
|
jaroslav@110
|
482 |
}
|
jaroslav@110
|
483 |
|
jaroslav@110
|
484 |
@Override
|
jaroslav@110
|
485 |
public <M> M toModel(Class<M> modelClass, Object data) {
|
jaroslav@110
|
486 |
return modelClass.cast(data);
|
jaroslav@110
|
487 |
}
|
jaroslav@110
|
488 |
|
jaroslav@110
|
489 |
@Override
|
jaroslav@110
|
490 |
public Object toJSON(InputStream is) throws IOException {
|
jaroslav@110
|
491 |
throw new IOException("Not supported");
|
jaroslav@110
|
492 |
}
|
jaroslav@240
|
493 |
|
jaroslav@240
|
494 |
@Override
|
jaroslav@240
|
495 |
public synchronized void runSafe(Runnable r) {
|
jaroslav@240
|
496 |
r.run();
|
jaroslav@240
|
497 |
}
|
jaroslav@258
|
498 |
|
jaroslav@258
|
499 |
@Override
|
jaroslav@258
|
500 |
public Void open(String url, JSONCall onReply) {
|
jaroslav@259
|
501 |
onReply.notifyError(new UnsupportedOperationException("WebSockets not supported!"));
|
jaroslav@258
|
502 |
return null;
|
jaroslav@258
|
503 |
}
|
jaroslav@258
|
504 |
|
jaroslav@258
|
505 |
@Override
|
jaroslav@258
|
506 |
public void send(Void socket, JSONCall data) {
|
jaroslav@258
|
507 |
}
|
jaroslav@258
|
508 |
|
jaroslav@258
|
509 |
@Override
|
jaroslav@258
|
510 |
public void close(Void socket) {
|
jaroslav@258
|
511 |
}
|
jaroslav@110
|
512 |
}
|
jaroslav@110
|
513 |
|
jtulach@2
|
514 |
}
|