ko/bck2brwsr/src/main/java/org/apidesign/bck2brwsr/ko2brwsr/BrwsrCtxImpl.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 08 Jan 2014 14:06:21 +0100
branchNbHtml4J
changeset 1420 246ee398b411
parent 1254 2e0da2375ef5
child 1425 43bb0053f3e2
permissions -rw-r--r--
Re-use the shared knockout binding provided by html4j project
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 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.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.ko2brwsr;
    19 
    20 import java.io.ByteArrayOutputStream;
    21 import java.io.IOException;
    22 import java.io.InputStream;
    23 import java.io.InputStreamReader;
    24 import org.apidesign.html.json.spi.JSONCall;
    25 import org.apidesign.html.json.spi.Transfer;
    26 import org.apidesign.html.json.spi.WSTransfer;
    27 
    28 /**
    29  *
    30  * @author Jaroslav Tulach <jtulach@netbeans.org>
    31  */
    32 final class BrwsrCtxImpl implements Transfer, WSTransfer<LoadWS> {
    33     private BrwsrCtxImpl() {}
    34     
    35     public static final BrwsrCtxImpl DEFAULT = new BrwsrCtxImpl();
    36     
    37     @Override
    38     public void extract(Object obj, String[] props, Object[] values) {
    39         ConvertTypes.extractJSON(obj, props, values);
    40     }
    41 
    42     @Override
    43     public void loadJSON(final JSONCall call) {
    44         class R implements Runnable {
    45             final boolean success;
    46 
    47             public R(boolean success) {
    48                 this.success = success;
    49             }
    50             
    51             Object[] arr = { null };
    52             @Override
    53             public void run() {
    54                 if (success) {
    55                     call.notifySuccess(arr[0]);
    56                 } else {
    57                     Throwable t;
    58                     if (arr[0] instanceof Throwable) {
    59                         t = (Throwable) arr[0];
    60                     } else {
    61                         if (arr[0] == null) {
    62                             t = new IOException();
    63                         } else {
    64                             t = new IOException(arr[0].toString());
    65                         }
    66                     }
    67                     call.notifyError(t);
    68                 }
    69             }
    70         }
    71         R success = new R(true);
    72         R failure = new R(false);
    73         if (call.isJSONP()) {
    74             String me = ConvertTypes.createJSONP(success.arr, success);
    75             ConvertTypes.loadJSONP(call.composeURL(me), me);
    76         } else {
    77             String data = null;
    78             if (call.isDoOutput()) {
    79                 try {
    80                     ByteArrayOutputStream bos = new ByteArrayOutputStream();
    81                     call.writeData(bos);
    82                     data = new String(bos.toByteArray(), "UTF-8");
    83                 } catch (IOException ex) {
    84                     call.notifyError(ex);
    85                 }
    86             }
    87             ConvertTypes.loadJSON(call.composeURL(null), success.arr, success, failure, call.getMethod(), data);
    88         }
    89     }
    90 
    91     @Override
    92     public Object toJSON(InputStream is) throws IOException {
    93         StringBuilder sb = new StringBuilder();
    94         InputStreamReader r = new InputStreamReader(is);
    95         for (;;) {
    96             int ch = r.read();
    97             if (ch == -1) {
    98                 break;
    99             }
   100             sb.append((char)ch);
   101         }
   102         return ConvertTypes.parse(sb.toString());
   103     }
   104 
   105     @Override
   106     public LoadWS open(String url, JSONCall callback) {
   107         return new LoadWS(callback, url);
   108     }
   109 
   110     @Override
   111     public void send(LoadWS socket, JSONCall data) {
   112         socket.send(data);
   113     }
   114 
   115     @Override
   116     public void close(LoadWS socket) {
   117         socket.close();
   118     }
   119 }