samples/jsonviewer.js

changeset 0
b1a7da25b547
child 962
ac62e33a99b0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/jsonviewer.js	Wed Apr 27 01:36:41 2016 +0800
     1.3 @@ -0,0 +1,120 @@
     1.4 +#// Usage: jjs -fx jsonviewer.js
     1.5 +// or
     1.6 +//        jjs -fx jsonviewer.js -- <url-of-json-doc>
     1.7 +
     1.8 +/*
     1.9 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
    1.10 + * 
    1.11 + * Redistribution and use in source and binary forms, with or without
    1.12 + * modification, are permitted provided that the following conditions
    1.13 + * are met:
    1.14 + * 
    1.15 + *   - Redistributions of source code must retain the above copyright
    1.16 + *     notice, this list of conditions and the following disclaimer.
    1.17 + * 
    1.18 + *   - Redistributions in binary form must reproduce the above copyright
    1.19 + *     notice, this list of conditions and the following disclaimer in the
    1.20 + *     documentation and/or other materials provided with the distribution.
    1.21 + * 
    1.22 + *   - Neither the name of Oracle nor the names of its
    1.23 + *     contributors may be used to endorse or promote products derived
    1.24 + *     from this software without specific prior written permission.
    1.25 + * 
    1.26 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
    1.27 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
    1.28 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    1.29 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    1.30 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    1.31 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    1.32 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    1.33 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    1.34 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    1.35 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    1.36 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.37 + */
    1.38 +
    1.39 +if (! $OPTIONS._fx) {
    1.40 +    print("Usage: jjs -fx jsonviewer.js -- <url-of-json-doc>");
    1.41 +    exit(1);
    1.42 +}
    1.43 +
    1.44 +// This example downloads a JSON file from a URL and
    1.45 +// shows the same as a JavaFX tree view.
    1.46 +
    1.47 +// Using JavaFX from Nashorn. See also:
    1.48 +// http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/javafx.html
    1.49 +
    1.50 +// JavaFX classes used
    1.51 +var StackPane = Java.type("javafx.scene.layout.StackPane");
    1.52 +var Scene     = Java.type("javafx.scene.Scene");
    1.53 +var TreeItem  = Java.type("javafx.scene.control.TreeItem");
    1.54 +var TreeView  = Java.type("javafx.scene.control.TreeView");
    1.55 +
    1.56 +// read text content of a URL
    1.57 +function readTextFromURL(url) {
    1.58 +    // equivalent to 
    1.59 +    // 
    1.60 +    //    import java.io.*;
    1.61 +    //    import java.net.*;
    1.62 +    //    import java.lang.StringBuffer;
    1.63 +    //
    1.64 +    // only inside the 'with' statement
    1.65 +    with (new JavaImporter(java.io,
    1.66 +        java.net,
    1.67 +        java.lang.StringBuilder)) {
    1.68 +        var buf = new StringBuilder();
    1.69 +        var u = new URL(url);
    1.70 +        var reader = new BufferedReader(
    1.71 +            new InputStreamReader(u.openStream()));
    1.72 +        var line = null;
    1.73 +        try {
    1.74 +            while ((line = reader.readLine()) != null)
    1.75 +                buf.append(line).append('\n');
    1.76 +        } finally {
    1.77 +            reader.close();
    1.78 +        }
    1.79 +
    1.80 +        return buf.toString();
    1.81 +    }
    1.82 +}
    1.83 +
    1.84 +// Create a javafx TreeItem to view a script object
    1.85 +function treeItemForObject(obj, name) {
    1.86 +    var item = new TreeItem(name);
    1.87 +    for (var prop in obj) {
    1.88 +       var node = obj[prop];
    1.89 +       if (typeof node == 'object') {
    1.90 +           if (node == null) {
    1.91 +               // skip nulls
    1.92 +               continue;
    1.93 +           }
    1.94 +
    1.95 +           if (Array.isArray(node) && node.length == 0) {
    1.96 +               // skip empty arrays
    1.97 +               continue;
    1.98 +           }
    1.99 +
   1.100 +           var subitem = treeItemForObject(node, prop);
   1.101 +       } else {
   1.102 +           var subitem = new TreeItem(prop + ": " + node);
   1.103 +       }
   1.104 +       item.children.add(subitem);
   1.105 +    }
   1.106 +    return item;
   1.107 +}
   1.108 +
   1.109 +var DEFAULT_URL = "http://api.openweathermap.org/data/2.5/forecast/daily?q=Chennai&amp;mode=json&amp;units=metric&amp;cnt=7`";
   1.110 +
   1.111 +var url = arguments.length == 0? DEFAULT_URL : arguments[0];
   1.112 +var obj = JSON.parse(readTextFromURL(url));
   1.113 +
   1.114 +// JavaFX start method
   1.115 +function start(stage) {
   1.116 +    stage.title = "JSON Viewer";
   1.117 +    var rootItem = treeItemForObject(obj, url);
   1.118 +    var tree = new TreeView(rootItem);
   1.119 +    var root = new StackPane();
   1.120 +    root.children.add(tree);
   1.121 +    stage.scene = new Scene(root, 300, 450);
   1.122 +    stage.show();
   1.123 +}

mercurial