1.1 --- a/ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCntxt.java Thu May 16 14:10:02 2013 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,135 +0,0 @@
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 java.util.logging.Level;
1.31 -import java.util.logging.Logger;
1.32 -import net.java.html.json.Context;
1.33 -import org.apidesign.html.json.spi.ContextBuilder;
1.34 -import org.apidesign.html.json.spi.FunctionBinding;
1.35 -import org.apidesign.html.json.spi.JSONCall;
1.36 -import org.apidesign.html.json.spi.PropertyBinding;
1.37 -import org.apidesign.html.json.spi.Technology;
1.38 -import org.apidesign.html.json.spi.Transfer;
1.39 -
1.40 -/**
1.41 - *
1.42 - * @author Jaroslav Tulach <jtulach@netbeans.org>
1.43 - */
1.44 -final class BrwsrCntxt implements Technology<Object>, Transfer {
1.45 - private BrwsrCntxt() {}
1.46 -
1.47 - public static final Context DEFAULT;
1.48 - static {
1.49 - BrwsrCntxt c = new BrwsrCntxt();
1.50 - DEFAULT = ContextBuilder.create().withTechnology(c).withTransfer(c).build();
1.51 - }
1.52 -
1.53 - @Override
1.54 - public void extract(Object obj, String[] props, Object[] values) {
1.55 - ConvertTypes.extractJSON(obj, props, values);
1.56 - }
1.57 -
1.58 - @Override
1.59 - public void loadJSON(final JSONCall call) {
1.60 - class R implements Runnable {
1.61 - Object[] arr = { null };
1.62 - @Override
1.63 - public void run() {
1.64 - call.notifySuccess(arr[0]);
1.65 - }
1.66 - }
1.67 - R r = new R();
1.68 - if (call.isJSONP()) {
1.69 - String me = ConvertTypes.createJSONP(r.arr, r);
1.70 - ConvertTypes.loadJSONP(call.composeURL(me), me);
1.71 - } else {
1.72 - String data = null;
1.73 - if (call.isDoOutput()) {
1.74 - try {
1.75 - ByteArrayOutputStream bos = new ByteArrayOutputStream();
1.76 - call.writeData(bos);
1.77 - data = new String(bos.toByteArray(), "UTF-8");
1.78 - } catch (IOException ex) {
1.79 - call.notifyError(ex);
1.80 - }
1.81 - }
1.82 - ConvertTypes.loadJSON(call.composeURL(null), r.arr, r, call.getMethod(), data);
1.83 - }
1.84 - }
1.85 -
1.86 - @Override
1.87 - public Object wrapModel(Object model) {
1.88 - return model;
1.89 - }
1.90 -
1.91 - @Override
1.92 - public void bind(PropertyBinding b, Object model, Object data) {
1.93 - Knockout.bind(data, b, b.getPropertyName(),
1.94 - "getValue__Ljava_lang_Object_2",
1.95 - b.isReadOnly() ? null : "setValue__VLjava_lang_Object_2",
1.96 - false, false
1.97 - );
1.98 - }
1.99 -
1.100 - @Override
1.101 - public void valueHasMutated(Object data, String propertyName) {
1.102 - Knockout.valueHasMutated(data, propertyName);
1.103 - }
1.104 -
1.105 - @Override
1.106 - public void expose(FunctionBinding fb, Object model, Object d) {
1.107 - Knockout.expose(d, fb, fb.getFunctionName(), "call__VLjava_lang_Object_2Ljava_lang_Object_2");
1.108 - }
1.109 -
1.110 - @Override
1.111 - public void applyBindings(Object data) {
1.112 - Knockout.applyBindings(data);
1.113 - }
1.114 -
1.115 - @Override
1.116 - public Object wrapArray(Object[] arr) {
1.117 - return arr;
1.118 - }
1.119 -
1.120 - @Override
1.121 - public <M> M toModel(Class<M> modelClass, Object data) {
1.122 - return modelClass.cast(data);
1.123 - }
1.124 -
1.125 - @Override
1.126 - public Object toJSON(InputStream is) throws IOException {
1.127 - StringBuilder sb = new StringBuilder();
1.128 - InputStreamReader r = new InputStreamReader(is);
1.129 - for (;;) {
1.130 - int ch = r.read();
1.131 - if (ch == -1) {
1.132 - break;
1.133 - }
1.134 - sb.append((char)ch);
1.135 - }
1.136 - return ConvertTypes.parse(sb.toString());
1.137 - }
1.138 -}