aoqi@0: #// Usage: jjs -fx jsonviewer.js aoqi@0: // or aoqi@0: // jjs -fx jsonviewer.js -- aoqi@0: aoqi@0: /* aoqi@0: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. attila@962: * aoqi@0: * Redistribution and use in source and binary forms, with or without aoqi@0: * modification, are permitted provided that the following conditions aoqi@0: * are met: attila@962: * aoqi@0: * - Redistributions of source code must retain the above copyright aoqi@0: * notice, this list of conditions and the following disclaimer. attila@962: * aoqi@0: * - Redistributions in binary form must reproduce the above copyright aoqi@0: * notice, this list of conditions and the following disclaimer in the aoqi@0: * documentation and/or other materials provided with the distribution. attila@962: * aoqi@0: * - Neither the name of Oracle nor the names of its aoqi@0: * contributors may be used to endorse or promote products derived aoqi@0: * from this software without specific prior written permission. attila@962: * aoqi@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS aoqi@0: * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, aoqi@0: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR aoqi@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR aoqi@0: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, aoqi@0: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, aoqi@0: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR aoqi@0: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF aoqi@0: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING aoqi@0: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS aoqi@0: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. aoqi@0: */ aoqi@0: aoqi@0: if (! $OPTIONS._fx) { aoqi@0: print("Usage: jjs -fx jsonviewer.js -- "); aoqi@0: exit(1); aoqi@0: } aoqi@0: aoqi@0: // This example downloads a JSON file from a URL and aoqi@0: // shows the same as a JavaFX tree view. aoqi@0: aoqi@0: // Using JavaFX from Nashorn. See also: aoqi@0: // http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/javafx.html aoqi@0: aoqi@0: // JavaFX classes used aoqi@0: var StackPane = Java.type("javafx.scene.layout.StackPane"); aoqi@0: var Scene = Java.type("javafx.scene.Scene"); aoqi@0: var TreeItem = Java.type("javafx.scene.control.TreeItem"); aoqi@0: var TreeView = Java.type("javafx.scene.control.TreeView"); aoqi@0: aoqi@0: // read text content of a URL aoqi@0: function readTextFromURL(url) { attila@962: // equivalent to attila@962: // aoqi@0: // import java.io.*; aoqi@0: // import java.net.*; aoqi@0: // import java.lang.StringBuffer; aoqi@0: // aoqi@0: // only inside the 'with' statement aoqi@0: with (new JavaImporter(java.io, aoqi@0: java.net, aoqi@0: java.lang.StringBuilder)) { aoqi@0: var buf = new StringBuilder(); aoqi@0: var u = new URL(url); aoqi@0: var reader = new BufferedReader( aoqi@0: new InputStreamReader(u.openStream())); aoqi@0: var line = null; aoqi@0: try { aoqi@0: while ((line = reader.readLine()) != null) aoqi@0: buf.append(line).append('\n'); aoqi@0: } finally { aoqi@0: reader.close(); aoqi@0: } aoqi@0: aoqi@0: return buf.toString(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Create a javafx TreeItem to view a script object aoqi@0: function treeItemForObject(obj, name) { aoqi@0: var item = new TreeItem(name); aoqi@0: for (var prop in obj) { aoqi@0: var node = obj[prop]; aoqi@0: if (typeof node == 'object') { aoqi@0: if (node == null) { aoqi@0: // skip nulls aoqi@0: continue; aoqi@0: } aoqi@0: aoqi@0: if (Array.isArray(node) && node.length == 0) { aoqi@0: // skip empty arrays aoqi@0: continue; aoqi@0: } aoqi@0: aoqi@0: var subitem = treeItemForObject(node, prop); aoqi@0: } else { aoqi@0: var subitem = new TreeItem(prop + ": " + node); aoqi@0: } aoqi@0: item.children.add(subitem); aoqi@0: } aoqi@0: return item; aoqi@0: } aoqi@0: aoqi@0: var DEFAULT_URL = "http://api.openweathermap.org/data/2.5/forecast/daily?q=Chennai&mode=json&units=metric&cnt=7`"; aoqi@0: aoqi@0: var url = arguments.length == 0? DEFAULT_URL : arguments[0]; aoqi@0: var obj = JSON.parse(readTextFromURL(url)); aoqi@0: aoqi@0: // JavaFX start method aoqi@0: function start(stage) { aoqi@0: stage.title = "JSON Viewer"; aoqi@0: var rootItem = treeItemForObject(obj, url); aoqi@0: var tree = new TreeView(rootItem); aoqi@0: var root = new StackPane(); aoqi@0: root.children.add(tree); aoqi@0: stage.scene = new Scene(root, 300, 450); aoqi@0: stage.show(); aoqi@0: }