First updates to run with forthcoming version 0.5 - WebSockets in FX launcher and etc.
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.ko2brwsr;
20 import java.io.ByteArrayOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.InputStreamReader;
24 import org.apidesign.html.json.spi.FunctionBinding;
25 import org.apidesign.html.json.spi.JSONCall;
26 import org.apidesign.html.json.spi.PropertyBinding;
27 import org.apidesign.html.json.spi.Technology;
28 import org.apidesign.html.json.spi.Transfer;
32 * @author Jaroslav Tulach <jtulach@netbeans.org>
34 final class BrwsrCtxImpl implements Technology<Object>, Transfer {
35 private BrwsrCtxImpl() {}
37 public static final BrwsrCtxImpl DEFAULT = new BrwsrCtxImpl();
40 public void extract(Object obj, String[] props, Object[] values) {
41 ConvertTypes.extractJSON(obj, props, values);
45 public void loadJSON(final JSONCall call) {
46 class R implements Runnable {
47 Object[] arr = { null };
50 call.notifySuccess(arr[0]);
55 String me = ConvertTypes.createJSONP(r.arr, r);
56 ConvertTypes.loadJSONP(call.composeURL(me), me);
59 if (call.isDoOutput()) {
61 ByteArrayOutputStream bos = new ByteArrayOutputStream();
63 data = new String(bos.toByteArray(), "UTF-8");
64 } catch (IOException ex) {
68 ConvertTypes.loadJSON(call.composeURL(null), r.arr, r, call.getMethod(), data);
73 public Object wrapModel(Object model) {
78 public void bind(PropertyBinding b, Object model, Object data) {
79 Knockout.bind(data, b, b.getPropertyName(),
80 "getValue__Ljava_lang_Object_2",
81 b.isReadOnly() ? null : "setValue__VLjava_lang_Object_2",
87 public void valueHasMutated(Object data, String propertyName) {
88 Knockout.valueHasMutated(data, propertyName);
92 public void expose(FunctionBinding fb, Object model, Object d) {
93 Knockout.expose(d, fb, fb.getFunctionName(), "call__VLjava_lang_Object_2Ljava_lang_Object_2");
97 public void applyBindings(Object data) {
98 Knockout.applyBindings(data);
102 public Object wrapArray(Object[] arr) {
107 public <M> M toModel(Class<M> modelClass, Object data) {
108 return modelClass.cast(data);
112 public Object toJSON(InputStream is) throws IOException {
113 StringBuilder sb = new StringBuilder();
114 InputStreamReader r = new InputStreamReader(is);
122 return ConvertTypes.parse(sb.toString());
126 public void runSafe(Runnable r) {