1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCtxImpl.java Tue May 28 13:31:42 2013 +0200
1.3 @@ -0,0 +1,129 @@
1.4 +/**
1.5 + * HTML via Java(tm) Language Bindings
1.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details. apidesign.org
1.16 + * designates this particular file as subject to the
1.17 + * "Classpath" exception as provided by apidesign.org
1.18 + * in the License file that accompanied this code.
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License
1.21 + * along with this program. Look for COPYING file in the top folder.
1.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
1.23 + */
1.24 +package org.apidesign.html.ko2brwsr;
1.25 +
1.26 +import java.io.ByteArrayOutputStream;
1.27 +import java.io.IOException;
1.28 +import java.io.InputStream;
1.29 +import java.io.InputStreamReader;
1.30 +import net.java.html.BrwsrCtx;
1.31 +import org.apidesign.html.context.spi.Contexts;
1.32 +import org.apidesign.html.json.spi.FunctionBinding;
1.33 +import org.apidesign.html.json.spi.JSONCall;
1.34 +import org.apidesign.html.json.spi.PropertyBinding;
1.35 +import org.apidesign.html.json.spi.Technology;
1.36 +import org.apidesign.html.json.spi.Transfer;
1.37 +
1.38 +/**
1.39 + *
1.40 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.41 + */
1.42 +final class BrwsrCtxImpl implements Technology<Object>, Transfer {
1.43 + private BrwsrCtxImpl() {}
1.44 +
1.45 + public static final BrwsrCtxImpl DEFAULT = new BrwsrCtxImpl();
1.46 +
1.47 + @Override
1.48 + public void extract(Object obj, String[] props, Object[] values) {
1.49 + ConvertTypes.extractJSON(obj, props, values);
1.50 + }
1.51 +
1.52 + @Override
1.53 + public void loadJSON(final JSONCall call) {
1.54 + class R implements Runnable {
1.55 + Object[] arr = { null };
1.56 + @Override
1.57 + public void run() {
1.58 + call.notifySuccess(arr[0]);
1.59 + }
1.60 + }
1.61 + R r = new R();
1.62 + if (call.isJSONP()) {
1.63 + String me = ConvertTypes.createJSONP(r.arr, r);
1.64 + ConvertTypes.loadJSONP(call.composeURL(me), me);
1.65 + } else {
1.66 + String data = null;
1.67 + if (call.isDoOutput()) {
1.68 + try {
1.69 + ByteArrayOutputStream bos = new ByteArrayOutputStream();
1.70 + call.writeData(bos);
1.71 + data = new String(bos.toByteArray(), "UTF-8");
1.72 + } catch (IOException ex) {
1.73 + call.notifyError(ex);
1.74 + }
1.75 + }
1.76 + ConvertTypes.loadJSON(call.composeURL(null), r.arr, r, call.getMethod(), data);
1.77 + }
1.78 + }
1.79 +
1.80 + @Override
1.81 + public Object wrapModel(Object model) {
1.82 + return model;
1.83 + }
1.84 +
1.85 + @Override
1.86 + public void bind(PropertyBinding b, Object model, Object data) {
1.87 + Knockout.bind(data, b, b.getPropertyName(),
1.88 + "getValue__Ljava_lang_Object_2",
1.89 + b.isReadOnly() ? null : "setValue__VLjava_lang_Object_2",
1.90 + false, false
1.91 + );
1.92 + }
1.93 +
1.94 + @Override
1.95 + public void valueHasMutated(Object data, String propertyName) {
1.96 + Knockout.valueHasMutated(data, propertyName);
1.97 + }
1.98 +
1.99 + @Override
1.100 + public void expose(FunctionBinding fb, Object model, Object d) {
1.101 + Knockout.expose(d, fb, fb.getFunctionName(), "call__VLjava_lang_Object_2Ljava_lang_Object_2");
1.102 + }
1.103 +
1.104 + @Override
1.105 + public void applyBindings(Object data) {
1.106 + Knockout.applyBindings(data);
1.107 + }
1.108 +
1.109 + @Override
1.110 + public Object wrapArray(Object[] arr) {
1.111 + return arr;
1.112 + }
1.113 +
1.114 + @Override
1.115 + public <M> M toModel(Class<M> modelClass, Object data) {
1.116 + return modelClass.cast(data);
1.117 + }
1.118 +
1.119 + @Override
1.120 + public Object toJSON(InputStream is) throws IOException {
1.121 + StringBuilder sb = new StringBuilder();
1.122 + InputStreamReader r = new InputStreamReader(is);
1.123 + for (;;) {
1.124 + int ch = r.read();
1.125 + if (ch == -1) {
1.126 + break;
1.127 + }
1.128 + sb.append((char)ch);
1.129 + }
1.130 + return ConvertTypes.parse(sb.toString());
1.131 + }
1.132 +}