ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCntxt.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 03 May 2013 10:27:06 +0200
changeset 1196 fb83f58ece66
parent 1194 3213724a4996
child 1199 15f9f43bdf5b
permissions -rw-r--r--
A way to parse an input stream and create a JSON object
     1 /**
     2  * HTML via Java(tm) Language Bindings
     3  * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    16  *
    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
    20  */
    21 package org.apidesign.html.ko2brwsr;
    22 
    23 import java.io.IOException;
    24 import java.io.InputStream;
    25 import java.io.InputStreamReader;
    26 import net.java.html.json.Context;
    27 import org.apidesign.html.json.spi.ContextBuilder;
    28 import org.apidesign.html.json.spi.FunctionBinding;
    29 import org.apidesign.html.json.spi.JSONCall;
    30 import org.apidesign.html.json.spi.PropertyBinding;
    31 import org.apidesign.html.json.spi.Technology;
    32 import org.apidesign.html.json.spi.Transfer;
    33 
    34 /**
    35  *
    36  * @author Jaroslav Tulach <jtulach@netbeans.org>
    37  */
    38 final class BrwsrCntxt implements Technology<Object>, Transfer {
    39     private BrwsrCntxt() {}
    40     
    41     public static final Context DEFAULT;
    42     static {
    43         BrwsrCntxt c = new BrwsrCntxt();
    44         DEFAULT = ContextBuilder.create().withTechnology(c).withTransfer(c).build();
    45     }
    46     
    47     @Override
    48     public void extract(Object obj, String[] props, Object[] values) {
    49         ConvertTypes.extractJSON(obj, props, values);
    50     }
    51 
    52     @Override
    53     public void loadJSON(final JSONCall call) {
    54         class R implements Runnable {
    55             Object[] arr = { null };
    56             @Override
    57             public void run() {
    58                 call.notifySuccess(arr[0]);
    59             }
    60         }
    61         R r = new R();
    62         if (call.isJSONP()) {
    63             String me = ConvertTypes.createJSONP(r.arr, r);
    64             ConvertTypes.loadJSON(call.composeURL(me), r.arr, r, me);
    65         } else {
    66             ConvertTypes.loadJSON(call.composeURL(null), r.arr, r, null);
    67         }
    68     }
    69 
    70     @Override
    71     public Object wrapModel(Object model) {
    72         return model;
    73     }
    74 
    75     @Override
    76     public void bind(PropertyBinding b, Object model, Object data) {
    77         Knockout.bind(data, b, b.getPropertyName(), 
    78             "getValue__Ljava_lang_Object_2", 
    79             b.isReadOnly() ? null : "setValue__VLjava_lang_Object_2", 
    80             false, false
    81         );
    82     }
    83 
    84     @Override
    85     public void valueHasMutated(Object data, String propertyName) {
    86         Knockout.valueHasMutated(data, propertyName);
    87     }
    88 
    89     @Override
    90     public void expose(FunctionBinding fb, Object model, Object d) {
    91         Knockout.expose(d, fb, fb.getFunctionName(), "call__VLjava_lang_Object_2Ljava_lang_Object_2");
    92     }
    93 
    94     @Override
    95     public void applyBindings(Object data) {
    96         Knockout.applyBindings(data);
    97     }
    98 
    99     @Override
   100     public Object wrapArray(Object[] arr) {
   101         return arr;
   102     }
   103 
   104     @Override
   105     public <M> M toModel(Class<M> modelClass, Object data) {
   106         return modelClass.cast(data);
   107     }
   108 
   109     @Override
   110     public Object toJSON(InputStream is) throws IOException {
   111         StringBuilder sb = new StringBuilder();
   112         InputStreamReader r = new InputStreamReader(is);
   113         for (;;) {
   114             int ch = r.read();
   115             if (ch == -1) {
   116                 break;
   117             }
   118             sb.append((char)ch);
   119         }
   120         return ConvertTypes.parse(sb.toString());
   121     }
   122 }