ko/bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCtxImpl.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 24 Jun 2013 17:50:44 +0200
branchclassloader
changeset 1225 73c0973e8e0a
parent 1220 ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCtxImpl.java@cf9ba1d883c5
child 1227 5a907f38608d
permissions -rw-r--r--
Moving all knockout related code under the 'ko' directory
     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.ByteArrayOutputStream;
    24 import java.io.IOException;
    25 import java.io.InputStream;
    26 import java.io.InputStreamReader;
    27 import net.java.html.BrwsrCtx;
    28 import org.apidesign.html.context.spi.Contexts;
    29 import org.apidesign.html.json.spi.FunctionBinding;
    30 import org.apidesign.html.json.spi.JSONCall;
    31 import org.apidesign.html.json.spi.PropertyBinding;
    32 import org.apidesign.html.json.spi.Technology;
    33 import org.apidesign.html.json.spi.Transfer;
    34 
    35 /**
    36  *
    37  * @author Jaroslav Tulach <jtulach@netbeans.org>
    38  */
    39 final class BrwsrCtxImpl implements Technology<Object>, Transfer {
    40     private BrwsrCtxImpl() {}
    41     
    42     public static final BrwsrCtxImpl DEFAULT = new BrwsrCtxImpl();
    43     
    44     @Override
    45     public void extract(Object obj, String[] props, Object[] values) {
    46         ConvertTypes.extractJSON(obj, props, values);
    47     }
    48 
    49     @Override
    50     public void loadJSON(final JSONCall call) {
    51         class R implements Runnable {
    52             Object[] arr = { null };
    53             @Override
    54             public void run() {
    55                 call.notifySuccess(arr[0]);
    56             }
    57         }
    58         R r = new R();
    59         if (call.isJSONP()) {
    60             String me = ConvertTypes.createJSONP(r.arr, r);
    61             ConvertTypes.loadJSONP(call.composeURL(me), me);
    62         } else {
    63             String data = null;
    64             if (call.isDoOutput()) {
    65                 try {
    66                     ByteArrayOutputStream bos = new ByteArrayOutputStream();
    67                     call.writeData(bos);
    68                     data = new String(bos.toByteArray(), "UTF-8");
    69                 } catch (IOException ex) {
    70                     call.notifyError(ex);
    71                 }
    72             }
    73             ConvertTypes.loadJSON(call.composeURL(null), r.arr, r, call.getMethod(), data);
    74         }
    75     }
    76 
    77     @Override
    78     public Object wrapModel(Object model) {
    79         return model;
    80     }
    81 
    82     @Override
    83     public void bind(PropertyBinding b, Object model, Object data) {
    84         Knockout.bind(data, b, b.getPropertyName(), 
    85             "getValue__Ljava_lang_Object_2", 
    86             b.isReadOnly() ? null : "setValue__VLjava_lang_Object_2", 
    87             false, false
    88         );
    89     }
    90 
    91     @Override
    92     public void valueHasMutated(Object data, String propertyName) {
    93         Knockout.valueHasMutated(data, propertyName);
    94     }
    95 
    96     @Override
    97     public void expose(FunctionBinding fb, Object model, Object d) {
    98         Knockout.expose(d, fb, fb.getFunctionName(), "call__VLjava_lang_Object_2Ljava_lang_Object_2");
    99     }
   100 
   101     @Override
   102     public void applyBindings(Object data) {
   103         Knockout.applyBindings(data);
   104     }
   105 
   106     @Override
   107     public Object wrapArray(Object[] arr) {
   108         return arr;
   109     }
   110 
   111     @Override
   112     public <M> M toModel(Class<M> modelClass, Object data) {
   113         return modelClass.cast(data);
   114     }
   115 
   116     @Override
   117     public Object toJSON(InputStream is) throws IOException {
   118         StringBuilder sb = new StringBuilder();
   119         InputStreamReader r = new InputStreamReader(is);
   120         for (;;) {
   121             int ch = r.read();
   122             if (ch == -1) {
   123                 break;
   124             }
   125             sb.append((char)ch);
   126         }
   127         return ConvertTypes.parse(sb.toString());
   128     }
   129 }