ko4j/src/main/java/org/netbeans/html/ko4j/LoadJSON.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 22 Nov 2015 21:18:35 +0100
changeset 1020 b5d5cbb44ce0
parent 940 bdec4103bdb2
permissions -rw-r--r--
fromRaw(toRaw(...)) should yield object with same values
     1 /**
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
     5  *
     6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
     7  * Other names may be trademarks of their respective owners.
     8  *
     9  * The contents of this file are subject to the terms of either the GNU
    10  * General Public License Version 2 only ("GPL") or the Common
    11  * Development and Distribution License("CDDL") (collectively, the
    12  * "License"). You may not use this file except in compliance with the
    13  * License. You can obtain a copy of the License at
    14  * http://www.netbeans.org/cddl-gplv2.html
    15  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    16  * specific language governing permissions and limitations under the
    17  * License.  When distributing the software, include this License Header
    18  * Notice in each file and include the License file at
    19  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    20  * particular file as subject to the "Classpath" exception as provided
    21  * by Oracle in the GPL Version 2 section of the License file that
    22  * accompanied this code. If applicable, add the following below the
    23  * License Header, with the fields enclosed by brackets [] replaced by
    24  * your own identifying information:
    25  * "Portions Copyrighted [year] [name of copyright owner]"
    26  *
    27  * Contributor(s):
    28  *
    29  * The Original Software is NetBeans. The Initial Developer of the Original
    30  * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
    31  *
    32  * If you wish your version of this file to be governed by only the CDDL
    33  * or only the GPL Version 2, indicate your decision by adding
    34  * "[Contributor] elects to include this software in this distribution
    35  * under the [CDDL or GPL Version 2] license." If you do not indicate a
    36  * single choice of license, a recipient has the option to distribute
    37  * your version of this file under either the CDDL, the GPL Version 2 or
    38  * to extend the choice of license to its licensees as provided above.
    39  * However, if you add GPL Version 2 code and therefore, elected the GPL
    40  * Version 2 license, then the option applies only if the new code is
    41  * made subject to such option by the copyright holder.
    42  */
    43 package org.netbeans.html.ko4j;
    44 
    45 import net.java.html.js.JavaScriptBody;
    46 import org.netbeans.html.json.spi.JSONCall;
    47 
    48 /**
    49  *
    50  * @author Jaroslav Tulach
    51  */
    52 final class LoadJSON {
    53     private LoadJSON() {}
    54 
    55     static String createJSONP(JSONCall whenDone) {
    56         int h = whenDone.hashCode();
    57         String name;
    58         for (;;) {
    59             name = "jsonp" + Integer.toHexString(h);
    60             if (defineIfUnused(name, whenDone)) {
    61                 return name;
    62             }
    63             h++;
    64         }
    65     }
    66 
    67     @JavaScriptBody(args = {"name", "done"}, javacall = true, body
    68         = "if (window[name]) return false;\n "
    69         + "window[name] = function(data) {\n "
    70         + "  delete window[name];\n"
    71         + "  var el = window.document.getElementById(name);\n"
    72         + "  el.parentNode.removeChild(el);\n"
    73         + "  done.@org.netbeans.html.json.spi.JSONCall::notifySuccess(Ljava/lang/Object;)(data);\n"
    74         + "};\n"
    75         + "return true;\n"
    76     )
    77     private static boolean defineIfUnused(String name, JSONCall done) {
    78         return true;
    79     }
    80 
    81     @JavaScriptBody(args = {"s"}, body = "return eval('(' + s + ')');")
    82     static Object parse(String s) {
    83         return s;
    84     }
    85 
    86     @JavaScriptBody(args = {"url", "done", "method", "data", "hp"}, javacall = true, body = ""
    87         + "var request = new XMLHttpRequest();\n"
    88         + "if (!method) method = 'GET';\n"
    89         + "request.open(method, url, true);\n"
    90         + "request.setRequestHeader('Content-Type', 'application/json; charset=utf-8');\n"
    91         + "for (var i = 0; i < hp.length; i += 2) {\n"
    92         + "  var h = hp[i];\n"
    93         + "  var v = hp[i + 1];\n"
    94         + "  request.setRequestHeader(h, v);\n"
    95         + "}\n"
    96         + "request.onreadystatechange = function() {\n"
    97         + "  if (request.readyState !== 4) return;\n"
    98         + "  var r = request.response || request.responseText;\n"
    99         + "  try {\n"
   100         + "    var str = r;\n"
   101         + "    if (request.status !== 0)\n"
   102         + "      if (request.status < 100 || request.status >= 400) throw request.status + ': ' + request.statusText;"
   103         + "    try { r = eval('(' + r + ')'); } catch (ignore) { }"
   104         + "    @org.netbeans.html.ko4j.KOTransfer::notifySuccess(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)(done, str, r);\n"
   105         + "  } catch (error) {;\n"
   106         + "    @org.netbeans.html.ko4j.KOTransfer::notifyError(Ljava/lang/Object;Ljava/lang/Object;)(done, error);\n"
   107         + "  }\n"
   108         + "};\n"
   109         + "request.onerror = function (e) {\n"
   110         + "  @org.netbeans.html.ko4j.KOTransfer::notifyError(Ljava/lang/Object;Ljava/lang/Object;)(done, e.type + ' status ' + request.status);\n"
   111         + "};\n"
   112         + "if (data) request.send(data);\n"
   113         + "else request.send();\n"
   114     )
   115     static void loadJSON(
   116         String url, JSONCall done, String method, String data, Object[] headerPairs
   117     ) {
   118     }
   119 
   120     @JavaScriptBody(args = {"url", "jsonp"}, body
   121         = "var scrpt = window.document.createElement('script');\n "
   122         + "scrpt.setAttribute('src', url);\n "
   123         + "scrpt.setAttribute('id', jsonp);\n "
   124         + "scrpt.setAttribute('type', 'text/javascript');\n "
   125         + "var body = document.getElementsByTagName('body')[0];\n "
   126         + "body.appendChild(scrpt);\n"
   127     )
   128     static void loadJSONP(String url, String jsonp) {
   129 
   130     }
   131 
   132     static void extractJSON(Object jsonObject, String[] props, Object[] values) {
   133         for (int i = 0; i < props.length; i++) {
   134             values[i] = Knockout.getProperty(jsonObject, props[i]);
   135         }
   136     }
   137 
   138 }