javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypes.java
branchfx
changeset 992 bae9b96bfd2c
parent 990 9ddce13e8ff9
child 997 e9b07d41de7f
     1.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypes.java	Mon Apr 15 18:30:30 2013 +0200
     1.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypes.java	Mon Apr 15 21:56:56 2013 +0200
     1.3 @@ -17,14 +17,31 @@
     1.4   */
     1.5  package org.apidesign.bck2brwsr.htmlpage;
     1.6  
     1.7 +import java.io.IOException;
     1.8 +import java.io.InputStream;
     1.9 +import java.io.InputStreamReader;
    1.10 +import java.io.PushbackInputStream;
    1.11 +import java.io.Reader;
    1.12 +import java.net.URL;
    1.13 +import java.util.Iterator;
    1.14 +import java.util.concurrent.Executor;
    1.15 +import java.util.concurrent.Executors;
    1.16 +import java.util.logging.Level;
    1.17 +import java.util.logging.Logger;
    1.18 +import javafx.application.Platform;
    1.19  import netscape.javascript.JSObject;
    1.20  import org.apidesign.bck2brwsr.core.JavaScriptBody;
    1.21 +import org.json.JSONArray;
    1.22 +import org.json.JSONException;
    1.23 +import org.json.JSONObject;
    1.24 +import org.json.JSONTokener;
    1.25  
    1.26  /**
    1.27   *
    1.28   * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.29   */
    1.30  public final class ConvertTypes {
    1.31 +    private static final Logger LOG = Logger.getLogger(ConvertTypes.class.getName());
    1.32      ConvertTypes() {
    1.33      }
    1.34      
    1.35 @@ -90,59 +107,84 @@
    1.36      }
    1.37      
    1.38      public static String createJSONP(Object[] jsonResult, Runnable whenDone) {
    1.39 -        int h = whenDone.hashCode();
    1.40 -        String name;
    1.41 -        for (;;) {
    1.42 -            name = "jsonp" + Integer.toHexString(h);
    1.43 -            if (defineIfUnused(name, jsonResult, whenDone)) {
    1.44 -                return name;
    1.45 -            }
    1.46 -            h++;
    1.47 -        }
    1.48 +        return "json" + Integer.toHexString(whenDone.hashCode());
    1.49      }
    1.50  
    1.51 -    @JavaScriptBody(args = { "name", "arr", "run" }, body = 
    1.52 -        "if (window[name]) return false;\n "
    1.53 -      + "window[name] = function(data) {\n "
    1.54 -      + "  delete window[name];\n"
    1.55 -      + "  var el = window.document.getElementById(name);\n"
    1.56 -      + "  el.parentNode.removeChild(el);\n"
    1.57 -      + "  arr[0] = data;\n"
    1.58 -      + "  run.run__V();\n"
    1.59 -      + "};\n"
    1.60 -      + "return true;\n"
    1.61 -    )
    1.62 -    private static boolean defineIfUnused(String name, Object[] arr, Runnable run) {
    1.63 -        return true;
    1.64 -    }
    1.65 -    
    1.66 -    @JavaScriptBody(args = { "url", "arr", "callback" }, body = ""
    1.67 -        + "var request = new XMLHttpRequest();\n"
    1.68 -        + "request.open('GET', url, true);\n"
    1.69 -        + "request.setRequestHeader('Content-Type', 'application/json; charset=utf-8');\n"
    1.70 -        + "request.onreadystatechange = function() {\n"
    1.71 -        + "  if (this.readyState!==4) return;\n"
    1.72 -        + "  try {\n"
    1.73 -        + "    arr[0] = eval('(' + this.response + ')');\n"
    1.74 -        + "  } catch (error) {;\n"
    1.75 -        + "    throw 'Cannot parse' + error + ':' + this.response;\n"
    1.76 -        + "  };\n"
    1.77 -        + "  callback.run__V();\n"
    1.78 -        + "};"
    1.79 -        + "request.send();"
    1.80 -    )
    1.81 -    private static void loadJSON(
    1.82 -        String url, Object[] jsonResult, Runnable whenDone
    1.83 -    ) {
    1.84 -    }
    1.85 -    
    1.86      public static void loadJSON(
    1.87          String url, Object[] jsonResult, Runnable whenDone, String jsonp
    1.88      ) {
    1.89 -        if (jsonp == null) {
    1.90 -            loadJSON(url, jsonResult, whenDone);
    1.91 -        } else {
    1.92 -            loadJSONP(url, jsonp);
    1.93 +        REQ.execute(new LoadJSON(url, jsonResult, whenDone, jsonp));
    1.94 +    }
    1.95 +    
    1.96 +    private static final Executor REQ = Executors.newCachedThreadPool();
    1.97 +    private static final class LoadJSON implements Runnable {
    1.98 +        
    1.99 +        private final String url;
   1.100 +        private final Object[] jsonResult;
   1.101 +        private final Runnable whenDone;
   1.102 +        private final String jsonp;
   1.103 +
   1.104 +        LoadJSON(String url, Object[] jsonResult, Runnable whenDone, String jsonp) {
   1.105 +            this.url = url;
   1.106 +            this.jsonResult = jsonResult;
   1.107 +            this.whenDone = whenDone;
   1.108 +            this.jsonp = jsonp;
   1.109 +        }
   1.110 +
   1.111 +        @Override
   1.112 +        public void run() {
   1.113 +            if (Platform.isFxApplicationThread()) {
   1.114 +                whenDone.run();
   1.115 +                return;
   1.116 +            }
   1.117 +            try {
   1.118 +                URL u = new URL(url.replace(" ", "%20"));
   1.119 +                InputStream is = u.openStream();
   1.120 +                if (jsonp != null) {
   1.121 +                    PushbackInputStream pis = new PushbackInputStream(is, 1);
   1.122 +                    is = pis;
   1.123 +                    for (;;) {
   1.124 +                        int ch = pis.read();
   1.125 +                        if (ch == -1) {
   1.126 +                            break;
   1.127 +                        }
   1.128 +                        if (ch == '{') {
   1.129 +                            pis.unread(ch);
   1.130 +                            break;
   1.131 +                        }
   1.132 +                    }
   1.133 +                }
   1.134 +                Reader r = new InputStreamReader(is, "UTF-8");
   1.135 +                
   1.136 +                JSONTokener tok = new JSONTokener(r);
   1.137 +                JSONObject obj = new JSONObject(tok);
   1.138 +                jsonResult[0] = convertToArray(obj);
   1.139 +            } catch (JSONException | IOException ex) {
   1.140 +                jsonResult[0] = ex;
   1.141 +            } finally {
   1.142 +                Platform.runLater(this);
   1.143 +            }
   1.144 +        }
   1.145 +        
   1.146 +        private static Object convertToArray(Object o) throws JSONException {
   1.147 +            if (o instanceof JSONArray) {
   1.148 +                JSONArray ja = (JSONArray)o;
   1.149 +                Object[] arr = new Object[ja.length()];
   1.150 +                for (int i = 0; i < arr.length; i++) {
   1.151 +                    arr[i] = convertToArray(ja.get(i));
   1.152 +                }
   1.153 +                return arr;
   1.154 +            } else if (o instanceof JSONObject) {
   1.155 +                JSONObject obj = (JSONObject)o;
   1.156 +                Iterator it = obj.keys();
   1.157 +                while (it.hasNext()) {
   1.158 +                    String key = (String)it.next();
   1.159 +                    obj.put(key, convertToArray(obj.get(key)));
   1.160 +                }
   1.161 +                return obj;
   1.162 +            } else {
   1.163 +                return o;
   1.164 +            }
   1.165          }
   1.166      }
   1.167      
   1.168 @@ -159,9 +201,18 @@
   1.169      }
   1.170      
   1.171      public static void extractJSON(Object jsonObject, String[] props, Object[] values) {
   1.172 -        for (int i = 0; i < props.length; i++) {
   1.173 -            values[i] = getProperty(jsonObject, props[i]);
   1.174 +        if (jsonObject instanceof JSONObject) {
   1.175 +            JSONObject obj = (JSONObject)jsonObject;
   1.176 +            for (int i = 0; i < props.length; i++) {
   1.177 +                try {
   1.178 +                    values[i] = obj.get(props[i]);
   1.179 +                } catch (JSONException ex) {
   1.180 +                    LOG.log(Level.SEVERE, "Can't read " + props[i] + " from " + jsonObject, ex);
   1.181 +                }
   1.182 +            }
   1.183 +            
   1.184          }
   1.185 +        
   1.186      }
   1.187      
   1.188  }