javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypes.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 08 Apr 2013 16:51:30 +0200
branchmodel
changeset 954 6448c284fe21
parent 949 3bd43aa6f08d
child 963 62d77cc38117
permissions -rw-r--r--
Support for JSONP
     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.htmlpage;
    19 
    20 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    21 
    22 /**
    23  *
    24  * @author Jaroslav Tulach <jtulach@netbeans.org>
    25  */
    26 public final class ConvertTypes {
    27     ConvertTypes() {
    28     }
    29     
    30     public static String toString(Object object, String property) {
    31         Object ret = getProperty(object, property);
    32         return ret == null ? null : ret.toString();
    33     }
    34 
    35     public static double toDouble(Object object, String property) {
    36         Object ret = getProperty(object, property);
    37         return ret instanceof Number ? ((Number)ret).doubleValue() : Double.NaN;
    38     }
    39 
    40     public static int toInt(Object object, String property) {
    41         Object ret = getProperty(object, property);
    42         return ret instanceof Number ? ((Number)ret).intValue() : Integer.MIN_VALUE;
    43     }
    44 
    45     public static <T> T toModel(Class<T> modelClass, Object object, String property) {
    46         Object ret = getProperty(object, property);
    47         if (ret == null || modelClass.isInstance(ret)) {
    48             return modelClass.cast(ret);
    49         }
    50         throw new IllegalStateException("Value " + ret + " is not of type " + modelClass);
    51     }
    52     
    53     public static String toJSON(Object value) {
    54         if (value == null) {
    55             return "null";
    56         }
    57         if (value instanceof String) {
    58             return '"' + 
    59                 ((String)value).
    60                     replace("\"", "\\\"").
    61                     replace("\n", "\\n").
    62                     replace("\r", "\\r").
    63                     replace("\t", "\\t")
    64                 + '"';
    65         }
    66         return value.toString();
    67     }
    68     
    69     @JavaScriptBody(args = { "object", "property" },
    70         body = "if (property === null) return object;\n"
    71         + "var p = object[property]; return p ? p : null;"
    72     )
    73     private static Object getProperty(Object object, String property) {
    74         return null;
    75     }
    76     
    77     public static String createJSONP(Object[] jsonResult, Runnable whenDone) {
    78         int h = whenDone.hashCode();
    79         String name;
    80         for (;;) {
    81             name = "jsonp" + Integer.toHexString(h);
    82             if (defineIfUnused(name, jsonResult, whenDone)) {
    83                 return name;
    84             }
    85             h++;
    86         }
    87     }
    88 
    89     @JavaScriptBody(args = { "name", "arr", "run" }, body = 
    90         "if (window[name]) return false;\n "
    91       + "window[name] = function(data) {\n "
    92       + "  arr[0] = data;\n"
    93       + "  run.run__V();\n"
    94       + "  delete window[name];\n"
    95       + "};"
    96       + "return true;\n"
    97     )
    98     private static boolean defineIfUnused(String name, Object[] arr, Runnable run) {
    99         return true;
   100     }
   101     
   102     @JavaScriptBody(args = { "url", "arr", "callback" }, body = ""
   103         + "var request = new XMLHttpRequest();\n"
   104         + "request.open('GET', url, true);\n"
   105         + "request.setRequestHeader('Content-Type', 'application/json; charset=utf-8');\n"
   106         + "request.onreadystatechange = function() {\n"
   107         + "  if (this.readyState!==4) return;\n"
   108         + "  try {\n"
   109         + "    arr[0] = eval('(' + this.response + ')');\n"
   110         + "  } catch (error) {;\n"
   111         + "    throw 'Cannot parse' + error + ':' + this.response;\n"
   112         + "  };\n"
   113         + "  callback.run__V();\n"
   114         + "};"
   115         + "request.send();"
   116     )
   117     private static void loadJSON(
   118         String url, Object[] jsonResult, Runnable whenDone
   119     ) {
   120     }
   121     
   122     public static void loadJSON(
   123         String url, Object[] jsonResult, Runnable whenDone, String jsonp
   124     ) {
   125         if (jsonp == null) {
   126             loadJSON(url, jsonResult, whenDone);
   127         } else {
   128             loadJSONP(url, jsonp);
   129         }
   130     }
   131     
   132     @JavaScriptBody(args = { "url", "jsonp" }, body = 
   133         "var scrpt = window.document.createElement('script');\n "
   134         + "scrpt.setAttribute('src', url);\n "
   135         + "scrpt.setAttribute('id', jsonp);\n "
   136         + "scrpt.setAttribute('type', 'text/javascript');\n "
   137         + "var body = document.getElementsByTagName('body')[0];\n "
   138         + "body.appendChild(scrpt);\n"
   139     )
   140     private static void loadJSONP(String url, String jsonp) {
   141         
   142     }
   143     
   144     public static void extractJSON(Object jsonObject, String[] props, Object[] values) {
   145         for (int i = 0; i < props.length; i++) {
   146             values[i] = getProperty(jsonObject, props[i]);
   147         }
   148     }
   149     
   150 }