ko/bck2brwsr/src/main/java/org/apidesign/bck2brwsr/ko2brwsr/BrwsrCtxImpl.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 09 Jan 2014 16:38:15 +0100
branchNbHtml4J
changeset 1425 43bb0053f3e2
parent 1420 246ee398b411
permissions -rw-r--r--
Removing useless methods and classes
     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.bck2brwsr.core.JavaScriptBody;
    25 import org.apidesign.html.json.spi.JSONCall;
    26 import org.apidesign.html.json.spi.Transfer;
    27 import org.apidesign.html.json.spi.WSTransfer;
    28 
    29 /**
    30  *
    31  * @author Jaroslav Tulach <jtulach@netbeans.org>
    32  */
    33 final class BrwsrCtxImpl implements Transfer, WSTransfer<LoadWS> {
    34     private BrwsrCtxImpl() {}
    35     
    36     public static final BrwsrCtxImpl DEFAULT = new BrwsrCtxImpl();
    37     
    38     @Override
    39     public void extract(Object obj, String[] props, Object[] values) {
    40         extractJSON(obj, props, values);
    41     }
    42 
    43     @Override
    44     public void loadJSON(final JSONCall call) {
    45         class R implements Runnable {
    46             final boolean success;
    47 
    48             public R(boolean success) {
    49                 this.success = success;
    50             }
    51             
    52             Object[] arr = { null };
    53             @Override
    54             public void run() {
    55                 if (success) {
    56                     call.notifySuccess(arr[0]);
    57                 } else {
    58                     Throwable t;
    59                     if (arr[0] instanceof Throwable) {
    60                         t = (Throwable) arr[0];
    61                     } else {
    62                         if (arr[0] == null) {
    63                             t = new IOException();
    64                         } else {
    65                             t = new IOException(arr[0].toString());
    66                         }
    67                     }
    68                     call.notifyError(t);
    69                 }
    70             }
    71         }
    72         R success = new R(true);
    73         R failure = new R(false);
    74         if (call.isJSONP()) {
    75             String me = createJSONP(success.arr, success);
    76             loadJSONP(call.composeURL(me), me);
    77         } else {
    78             String data = null;
    79             if (call.isDoOutput()) {
    80                 try {
    81                     ByteArrayOutputStream bos = new ByteArrayOutputStream();
    82                     call.writeData(bos);
    83                     data = new String(bos.toByteArray(), "UTF-8");
    84                 } catch (IOException ex) {
    85                     call.notifyError(ex);
    86                 }
    87             }
    88             loadJSON(call.composeURL(null), success.arr, success, failure, call.getMethod(), data);
    89         }
    90     }
    91 
    92     @Override
    93     public Object toJSON(InputStream is) throws IOException {
    94         StringBuilder sb = new StringBuilder();
    95         InputStreamReader r = new InputStreamReader(is);
    96         for (;;) {
    97             int ch = r.read();
    98             if (ch == -1) {
    99                 break;
   100             }
   101             sb.append((char)ch);
   102         }
   103         return parse(sb.toString());
   104     }
   105 
   106     @Override
   107     public LoadWS open(String url, JSONCall callback) {
   108         return new LoadWS(callback, url);
   109     }
   110 
   111     @Override
   112     public void send(LoadWS socket, JSONCall data) {
   113         socket.send(data);
   114     }
   115 
   116     @Override
   117     public void close(LoadWS socket) {
   118         socket.close();
   119     }
   120     
   121     //
   122     // implementations
   123     //
   124     
   125     @JavaScriptBody(args = {"object", "property"},
   126         body
   127         = "if (property === null) return object;\n"
   128         + "if (object === null) return null;\n"
   129         + "var p = object[property]; return p ? p : null;"
   130     )
   131     private static Object getProperty(Object object, String property) {
   132         return null;
   133     }
   134 
   135     public static String createJSONP(Object[] jsonResult, Runnable whenDone) {
   136         int h = whenDone.hashCode();
   137         String name;
   138         for (;;) {
   139             name = "jsonp" + Integer.toHexString(h);
   140             if (defineIfUnused(name, jsonResult, whenDone)) {
   141                 return name;
   142             }
   143             h++;
   144         }
   145     }
   146 
   147     @JavaScriptBody(args = {"name", "arr", "run"}, body
   148         = "if (window[name]) return false;\n "
   149         + "window[name] = function(data) {\n "
   150         + "  delete window[name];\n"
   151         + "  var el = window.document.getElementById(name);\n"
   152         + "  el.parentNode.removeChild(el);\n"
   153         + "  arr[0] = data;\n"
   154         + "  run.run__V();\n"
   155         + "};\n"
   156         + "return true;\n"
   157     )
   158     private static boolean defineIfUnused(String name, Object[] arr, Runnable run) {
   159         return true;
   160     }
   161 
   162     @JavaScriptBody(args = {"s"}, body = "return eval('(' + s + ')');")
   163     static Object parse(String s) {
   164         return s;
   165     }
   166 
   167     @JavaScriptBody(args = {"url", "arr", "callback", "onError", "method", "data"}, body = ""
   168         + "var request = new XMLHttpRequest();\n"
   169         + "if (!method) method = 'GET';\n"
   170         + "request.open(method, url, true);\n"
   171         + "request.setRequestHeader('Content-Type', 'application/json; charset=utf-8');\n"
   172         + "request.onreadystatechange = function() {\n"
   173         + "  if (this.readyState!==4) return;\n"
   174         + "  try {\n"
   175         + "    arr[0] = eval('(' + this.response + ')');\n"
   176         + "  } catch (error) {;\n"
   177         + "    arr[0] = this.response;\n"
   178         + "  }\n"
   179         + "  callback.run__V();\n"
   180         + "};\n"
   181         + "request.onerror = function (e) {\n"
   182         + "  arr[0] = e; onError.run__V();\n"
   183         + "}\n"
   184         + "if (data) request.send(data);"
   185         + "else request.send();"
   186     )
   187     static void loadJSON(
   188         String url, Object[] jsonResult, Runnable whenDone, Runnable whenErr, String method, String data
   189     ) {
   190     }
   191 
   192     @JavaScriptBody(args = {"url", "jsonp"}, body
   193         = "var scrpt = window.document.createElement('script');\n "
   194         + "scrpt.setAttribute('src', url);\n "
   195         + "scrpt.setAttribute('id', jsonp);\n "
   196         + "scrpt.setAttribute('type', 'text/javascript');\n "
   197         + "var body = document.getElementsByTagName('body')[0];\n "
   198         + "body.appendChild(scrpt);\n"
   199     )
   200     static void loadJSONP(String url, String jsonp) {
   201 
   202     }
   203 
   204     public static void extractJSON(Object jsonObject, String[] props, Object[] values) {
   205         for (int i = 0; i < props.length; i++) {
   206             values[i] = getProperty(jsonObject, props[i]);
   207         }
   208     }
   209     
   210 }