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