ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/ConvertTypes.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 01 May 2013 07:06:43 +0200
changeset 1194 3213724a4996
parent 1189 9f8b07dcbe79
child 1196 fb83f58ece66
permissions -rw-r--r--
Making more things private, removing KOList and providing some fake Javadoc
     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 = { "url", "arr", "callback" }, body = ""
   111         + "var request = new XMLHttpRequest();\n"
   112         + "request.open('GET', url, true);\n"
   113         + "request.setRequestHeader('Content-Type', 'application/json; charset=utf-8');\n"
   114         + "request.onreadystatechange = function() {\n"
   115         + "  if (this.readyState!==4) return;\n"
   116         + "  try {\n"
   117         + "    arr[0] = eval('(' + this.response + ')');\n"
   118         + "  } catch (error) {;\n"
   119         + "    throw 'Cannot parse' + error + ':' + this.response;\n"
   120         + "  };\n"
   121         + "  callback.run__V();\n"
   122         + "};"
   123         + "request.send();"
   124     )
   125     private static void loadJSON(
   126         String url, Object[] jsonResult, Runnable whenDone
   127     ) {
   128     }
   129     
   130     public static void loadJSON(
   131         String url, Object[] jsonResult, Runnable whenDone, String jsonp
   132     ) {
   133         if (jsonp == null) {
   134             loadJSON(url, jsonResult, whenDone);
   135         } else {
   136             loadJSONP(url, jsonp);
   137         }
   138     }
   139     
   140     @JavaScriptBody(args = { "url", "jsonp" }, body = 
   141         "var scrpt = window.document.createElement('script');\n "
   142         + "scrpt.setAttribute('src', url);\n "
   143         + "scrpt.setAttribute('id', jsonp);\n "
   144         + "scrpt.setAttribute('type', 'text/javascript');\n "
   145         + "var body = document.getElementsByTagName('body')[0];\n "
   146         + "body.appendChild(scrpt);\n"
   147     )
   148     private static void loadJSONP(String url, String jsonp) {
   149         
   150     }
   151     
   152     public static void extractJSON(Object jsonObject, String[] props, Object[] values) {
   153         for (int i = 0; i < props.length; i++) {
   154             values[i] = getProperty(jsonObject, props[i]);
   155         }
   156     }
   157     
   158 }