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 java.io.ByteArrayOutputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 import net.java.html.json.Context;
30 import org.apidesign.html.json.spi.ContextBuilder;
31 import org.apidesign.html.json.spi.FunctionBinding;
32 import org.apidesign.html.json.spi.JSONCall;
33 import org.apidesign.html.json.spi.PropertyBinding;
34 import org.apidesign.html.json.spi.Technology;
35 import org.apidesign.html.json.spi.Transfer;
39 * @author Jaroslav Tulach <jtulach@netbeans.org>
41 final class BrwsrCntxt implements Technology<Object>, Transfer {
42 private BrwsrCntxt() {}
44 public static final Context DEFAULT;
46 BrwsrCntxt c = new BrwsrCntxt();
47 DEFAULT = ContextBuilder.create().withTechnology(c).withTransfer(c).build();
51 public void extract(Object obj, String[] props, Object[] values) {
52 ConvertTypes.extractJSON(obj, props, values);
56 public void loadJSON(final JSONCall call) {
57 class R implements Runnable {
58 Object[] arr = { null };
61 call.notifySuccess(arr[0]);
66 String me = ConvertTypes.createJSONP(r.arr, r);
67 ConvertTypes.loadJSONP(call.composeURL(me), me);
70 if (call.isDoOutput()) {
72 ByteArrayOutputStream bos = new ByteArrayOutputStream();
74 data = new String(bos.toByteArray(), "UTF-8");
75 } catch (IOException ex) {
79 ConvertTypes.loadJSON(call.composeURL(null), r.arr, r, call.getMethod(), data);
84 public Object wrapModel(Object model) {
89 public void bind(PropertyBinding b, Object model, Object data) {
90 Knockout.bind(data, b, b.getPropertyName(),
91 "getValue__Ljava_lang_Object_2",
92 b.isReadOnly() ? null : "setValue__VLjava_lang_Object_2",
98 public void valueHasMutated(Object data, String propertyName) {
99 Knockout.valueHasMutated(data, propertyName);
103 public void expose(FunctionBinding fb, Object model, Object d) {
104 Knockout.expose(d, fb, fb.getFunctionName(), "call__VLjava_lang_Object_2Ljava_lang_Object_2");
108 public void applyBindings(Object data) {
109 Knockout.applyBindings(data);
113 public Object wrapArray(Object[] arr) {
118 public <M> M toModel(Class<M> modelClass, Object data) {
119 return modelClass.cast(data);
123 public Object toJSON(InputStream is) throws IOException {
124 StringBuilder sb = new StringBuilder();
125 InputStreamReader r = new InputStreamReader(is);
133 return ConvertTypes.parse(sb.toString());