ko-fx/src/main/java/org/apidesign/html/kofx/LoadJSON.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 01 May 2013 06:59:55 +0200
changeset 54 0cc18086f731
parent 37 184f08369400
child 60 939453361b2d
permissions -rw-r--r--
Providing some Javadoc, so Nexus allows the JAR to be released
jaroslav@37
     1
/**
jaroslav@37
     2
 * HTML via Java(tm) Language Bindings
jaroslav@37
     3
 * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@37
     4
 *
jaroslav@37
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@37
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@37
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@37
     8
 *
jaroslav@37
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@37
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@37
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@37
    12
 * GNU General Public License for more details. apidesign.org
jaroslav@37
    13
 * designates this particular file as subject to the
jaroslav@37
    14
 * "Classpath" exception as provided by apidesign.org
jaroslav@37
    15
 * in the License file that accompanied this code.
jaroslav@37
    16
 *
jaroslav@37
    17
 * You should have received a copy of the GNU General Public License
jaroslav@37
    18
 * along with this program. Look for COPYING file in the top folder.
jaroslav@37
    19
 * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
jaroslav@37
    20
 */
jaroslav@37
    21
package org.apidesign.html.kofx;
jaroslav@37
    22
jaroslav@37
    23
import java.io.IOException;
jaroslav@37
    24
import java.io.InputStreamReader;
jaroslav@37
    25
import java.io.PushbackInputStream;
jaroslav@37
    26
import java.io.Reader;
jaroslav@37
    27
import java.net.MalformedURLException;
jaroslav@37
    28
import java.net.URL;
jaroslav@37
    29
import java.util.Iterator;
jaroslav@37
    30
import java.util.concurrent.Executor;
jaroslav@37
    31
import java.util.concurrent.Executors;
jaroslav@37
    32
import java.util.logging.Level;
jaroslav@37
    33
import java.util.logging.Logger;
jaroslav@37
    34
import javafx.application.Platform;
jaroslav@37
    35
import javafx.scene.web.WebEngine;
jaroslav@37
    36
import netscape.javascript.JSObject;
jaroslav@37
    37
import org.apidesign.html.json.spi.JSONCall;
jaroslav@37
    38
import org.json.JSONArray;
jaroslav@37
    39
import org.json.JSONException;
jaroslav@37
    40
import org.json.JSONObject;
jaroslav@37
    41
import org.json.JSONTokener;
jaroslav@37
    42
jaroslav@54
    43
/** This is an implementation package - just
jaroslav@54
    44
 * include its JAR on classpath and use official {@link Context} API
jaroslav@54
    45
 * to access the functionality.
jaroslav@37
    46
 *
jaroslav@37
    47
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@37
    48
 */
jaroslav@37
    49
final class LoadJSON implements Runnable {
jaroslav@37
    50
    private static final Logger LOG = FXContext.LOG;
jaroslav@37
    51
    private static final Executor REQ = Executors.newCachedThreadPool();
jaroslav@37
    52
jaroslav@37
    53
    private final JSONCall call;
jaroslav@37
    54
    private final URL base;
jaroslav@37
    55
    private Throwable error;
jaroslav@37
    56
    private Object json;
jaroslav@37
    57
jaroslav@37
    58
jaroslav@37
    59
    private LoadJSON(JSONCall call) {
jaroslav@37
    60
        this.call = call;
jaroslav@37
    61
        URL b;
jaroslav@37
    62
        try {
jaroslav@37
    63
            b = new URL(findBaseURL());
jaroslav@37
    64
        } catch (MalformedURLException ex) {
jaroslav@37
    65
            LOG.log(Level.SEVERE, "Can't find base url for " + call.composeURL("dummy"), ex);
jaroslav@37
    66
            b = null;
jaroslav@37
    67
        }
jaroslav@37
    68
        this.base = b;
jaroslav@37
    69
    }
jaroslav@37
    70
jaroslav@37
    71
    public static void loadJSON(JSONCall call) {
jaroslav@37
    72
        REQ.execute(new LoadJSON((call)));
jaroslav@37
    73
    }
jaroslav@37
    74
jaroslav@37
    75
    @Override
jaroslav@37
    76
    public void run() {
jaroslav@37
    77
        if (Platform.isFxApplicationThread()) {
jaroslav@37
    78
            if (error != null) {
jaroslav@37
    79
                call.notifyError(error);
jaroslav@37
    80
            } else {
jaroslav@37
    81
                call.notifySuccess(json);
jaroslav@37
    82
            }
jaroslav@37
    83
            return;
jaroslav@37
    84
        }
jaroslav@37
    85
        final String url;
jaroslav@37
    86
        if (call.isJSONP()) {
jaroslav@37
    87
            url = call.composeURL("dummy");
jaroslav@37
    88
        } else {
jaroslav@37
    89
            url = call.composeURL(null);
jaroslav@37
    90
        }
jaroslav@37
    91
        try {
jaroslav@37
    92
            final URL u = new URL(base, url.replace(" ", "%20"));
jaroslav@37
    93
            final PushbackInputStream is = new PushbackInputStream(u.openStream(), 1);
jaroslav@37
    94
            boolean array = false;
jaroslav@37
    95
            if (call.isJSONP()) {
jaroslav@37
    96
                for (;;) {
jaroslav@37
    97
                    int ch = is.read();
jaroslav@37
    98
                    if (ch == -1) {
jaroslav@37
    99
                        break;
jaroslav@37
   100
                    }
jaroslav@37
   101
                    if (ch == '[') {
jaroslav@37
   102
                        is.unread(ch);
jaroslav@37
   103
                        array = true;
jaroslav@37
   104
                        break;
jaroslav@37
   105
                    }
jaroslav@37
   106
                    if (ch == '{') {
jaroslav@37
   107
                        is.unread(ch);
jaroslav@37
   108
                        break;
jaroslav@37
   109
                    }
jaroslav@37
   110
                }
jaroslav@37
   111
            } else {
jaroslav@37
   112
                int ch = is.read();
jaroslav@37
   113
                array = ch == '[';
jaroslav@37
   114
                is.unread(ch);
jaroslav@37
   115
            }
jaroslav@37
   116
            Reader r = new InputStreamReader(is, "UTF-8");
jaroslav@37
   117
jaroslav@37
   118
            JSONTokener tok = new JSONTokener(r);
jaroslav@37
   119
            Object obj = array ? new JSONArray(tok) : new JSONObject(tok);
jaroslav@37
   120
            json = convertToArray(obj);
jaroslav@37
   121
        } catch (JSONException | IOException ex) {
jaroslav@37
   122
            error = ex;
jaroslav@37
   123
            LOG.log(Level.WARNING, "Cannot connect to " + url, ex);
jaroslav@37
   124
        } finally {
jaroslav@37
   125
            Platform.runLater(this);
jaroslav@37
   126
        }
jaroslav@37
   127
    }
jaroslav@37
   128
jaroslav@37
   129
    private static Object convertToArray(Object o) throws JSONException {
jaroslav@37
   130
        if (o instanceof JSONArray) {
jaroslav@37
   131
            JSONArray ja = (JSONArray)o;
jaroslav@37
   132
            Object[] arr = new Object[ja.length()];
jaroslav@37
   133
            for (int i = 0; i < arr.length; i++) {
jaroslav@37
   134
                arr[i] = convertToArray(ja.get(i));
jaroslav@37
   135
            }
jaroslav@37
   136
            return arr;
jaroslav@37
   137
        } else if (o instanceof JSONObject) {
jaroslav@37
   138
            JSONObject obj = (JSONObject)o;
jaroslav@37
   139
            Iterator it = obj.keys();
jaroslav@37
   140
            while (it.hasNext()) {
jaroslav@37
   141
                String key = (String)it.next();
jaroslav@37
   142
                obj.put(key, convertToArray(obj.get(key)));
jaroslav@37
   143
            }
jaroslav@37
   144
            return obj;
jaroslav@37
   145
        } else {
jaroslav@37
   146
            return o;
jaroslav@37
   147
        }
jaroslav@37
   148
    }
jaroslav@37
   149
    
jaroslav@37
   150
    public static void extractJSON(Object jsonObject, String[] props, Object[] values) {
jaroslav@37
   151
        if (jsonObject instanceof JSONObject) {
jaroslav@37
   152
            JSONObject obj = (JSONObject)jsonObject;
jaroslav@37
   153
            for (int i = 0; i < props.length; i++) {
jaroslav@37
   154
                try {
jaroslav@37
   155
                    values[i] = obj.has(props[i]) ? obj.get(props[i]) : null;
jaroslav@37
   156
                } catch (JSONException ex) {
jaroslav@37
   157
                    LoadJSON.LOG.log(Level.SEVERE, "Can't read " + props[i] + " from " + jsonObject, ex);
jaroslav@37
   158
                }
jaroslav@37
   159
            }
jaroslav@37
   160
        }
jaroslav@37
   161
        if (jsonObject instanceof JSObject) {
jaroslav@37
   162
            JSObject obj = (JSObject)jsonObject;
jaroslav@37
   163
            for (int i = 0; i < props.length; i++) {
jaroslav@37
   164
                Object val = obj.getMember(props[i]);
jaroslav@37
   165
                values[i] = isDefined(val) ? val : null;
jaroslav@37
   166
            }
jaroslav@37
   167
        }
jaroslav@37
   168
    }
jaroslav@37
   169
    
jaroslav@37
   170
    private static String findBaseURL() {
jaroslav@37
   171
        WebEngine eng = (WebEngine) System.getProperties().get("webEngine");
jaroslav@37
   172
        return (String) eng.executeScript(
jaroslav@37
   173
            "var h;"
jaroslav@37
   174
            + "if (!!window && !!window.location && !!window.location.href)\n"
jaroslav@37
   175
            + "  h = window.location.href;\n"
jaroslav@37
   176
            + "else "
jaroslav@37
   177
            + "  h = null;"
jaroslav@37
   178
            + "h\n");
jaroslav@37
   179
    }
jaroslav@37
   180
jaroslav@37
   181
    private static boolean isDefined(Object val) {
jaroslav@37
   182
        return !"undefined".equals(val);
jaroslav@37
   183
    }
jaroslav@37
   184
}