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