Mon, 17 Apr 2017 11:46:11 -0700
Added tag jdk8u141-b05 for changeset f7fc5fc0e2cb
aoqi@0 | 1 | #// Usage: jjs -fx jsonviewer.js |
aoqi@0 | 2 | // or |
aoqi@0 | 3 | // jjs -fx jsonviewer.js -- <url-of-json-doc> |
aoqi@0 | 4 | |
aoqi@0 | 5 | /* |
aoqi@0 | 6 | * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
attila@962 | 7 | * |
aoqi@0 | 8 | * Redistribution and use in source and binary forms, with or without |
aoqi@0 | 9 | * modification, are permitted provided that the following conditions |
aoqi@0 | 10 | * are met: |
attila@962 | 11 | * |
aoqi@0 | 12 | * - Redistributions of source code must retain the above copyright |
aoqi@0 | 13 | * notice, this list of conditions and the following disclaimer. |
attila@962 | 14 | * |
aoqi@0 | 15 | * - Redistributions in binary form must reproduce the above copyright |
aoqi@0 | 16 | * notice, this list of conditions and the following disclaimer in the |
aoqi@0 | 17 | * documentation and/or other materials provided with the distribution. |
attila@962 | 18 | * |
aoqi@0 | 19 | * - Neither the name of Oracle nor the names of its |
aoqi@0 | 20 | * contributors may be used to endorse or promote products derived |
aoqi@0 | 21 | * from this software without specific prior written permission. |
attila@962 | 22 | * |
aoqi@0 | 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
aoqi@0 | 24 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
aoqi@0 | 25 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
aoqi@0 | 26 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
aoqi@0 | 27 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
aoqi@0 | 28 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
aoqi@0 | 29 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
aoqi@0 | 30 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
aoqi@0 | 31 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
aoqi@0 | 32 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
aoqi@0 | 33 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
aoqi@0 | 34 | */ |
aoqi@0 | 35 | |
aoqi@0 | 36 | if (! $OPTIONS._fx) { |
aoqi@0 | 37 | print("Usage: jjs -fx jsonviewer.js -- <url-of-json-doc>"); |
aoqi@0 | 38 | exit(1); |
aoqi@0 | 39 | } |
aoqi@0 | 40 | |
aoqi@0 | 41 | // This example downloads a JSON file from a URL and |
aoqi@0 | 42 | // shows the same as a JavaFX tree view. |
aoqi@0 | 43 | |
aoqi@0 | 44 | // Using JavaFX from Nashorn. See also: |
aoqi@0 | 45 | // http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/javafx.html |
aoqi@0 | 46 | |
aoqi@0 | 47 | // JavaFX classes used |
aoqi@0 | 48 | var StackPane = Java.type("javafx.scene.layout.StackPane"); |
aoqi@0 | 49 | var Scene = Java.type("javafx.scene.Scene"); |
aoqi@0 | 50 | var TreeItem = Java.type("javafx.scene.control.TreeItem"); |
aoqi@0 | 51 | var TreeView = Java.type("javafx.scene.control.TreeView"); |
aoqi@0 | 52 | |
aoqi@0 | 53 | // read text content of a URL |
aoqi@0 | 54 | function readTextFromURL(url) { |
attila@962 | 55 | // equivalent to |
attila@962 | 56 | // |
aoqi@0 | 57 | // import java.io.*; |
aoqi@0 | 58 | // import java.net.*; |
aoqi@0 | 59 | // import java.lang.StringBuffer; |
aoqi@0 | 60 | // |
aoqi@0 | 61 | // only inside the 'with' statement |
aoqi@0 | 62 | with (new JavaImporter(java.io, |
aoqi@0 | 63 | java.net, |
aoqi@0 | 64 | java.lang.StringBuilder)) { |
aoqi@0 | 65 | var buf = new StringBuilder(); |
aoqi@0 | 66 | var u = new URL(url); |
aoqi@0 | 67 | var reader = new BufferedReader( |
aoqi@0 | 68 | new InputStreamReader(u.openStream())); |
aoqi@0 | 69 | var line = null; |
aoqi@0 | 70 | try { |
aoqi@0 | 71 | while ((line = reader.readLine()) != null) |
aoqi@0 | 72 | buf.append(line).append('\n'); |
aoqi@0 | 73 | } finally { |
aoqi@0 | 74 | reader.close(); |
aoqi@0 | 75 | } |
aoqi@0 | 76 | |
aoqi@0 | 77 | return buf.toString(); |
aoqi@0 | 78 | } |
aoqi@0 | 79 | } |
aoqi@0 | 80 | |
aoqi@0 | 81 | // Create a javafx TreeItem to view a script object |
aoqi@0 | 82 | function treeItemForObject(obj, name) { |
aoqi@0 | 83 | var item = new TreeItem(name); |
aoqi@0 | 84 | for (var prop in obj) { |
aoqi@0 | 85 | var node = obj[prop]; |
aoqi@0 | 86 | if (typeof node == 'object') { |
aoqi@0 | 87 | if (node == null) { |
aoqi@0 | 88 | // skip nulls |
aoqi@0 | 89 | continue; |
aoqi@0 | 90 | } |
aoqi@0 | 91 | |
aoqi@0 | 92 | if (Array.isArray(node) && node.length == 0) { |
aoqi@0 | 93 | // skip empty arrays |
aoqi@0 | 94 | continue; |
aoqi@0 | 95 | } |
aoqi@0 | 96 | |
aoqi@0 | 97 | var subitem = treeItemForObject(node, prop); |
aoqi@0 | 98 | } else { |
aoqi@0 | 99 | var subitem = new TreeItem(prop + ": " + node); |
aoqi@0 | 100 | } |
aoqi@0 | 101 | item.children.add(subitem); |
aoqi@0 | 102 | } |
aoqi@0 | 103 | return item; |
aoqi@0 | 104 | } |
aoqi@0 | 105 | |
aoqi@0 | 106 | var DEFAULT_URL = "http://api.openweathermap.org/data/2.5/forecast/daily?q=Chennai&mode=json&units=metric&cnt=7`"; |
aoqi@0 | 107 | |
aoqi@0 | 108 | var url = arguments.length == 0? DEFAULT_URL : arguments[0]; |
aoqi@0 | 109 | var obj = JSON.parse(readTextFromURL(url)); |
aoqi@0 | 110 | |
aoqi@0 | 111 | // JavaFX start method |
aoqi@0 | 112 | function start(stage) { |
aoqi@0 | 113 | stage.title = "JSON Viewer"; |
aoqi@0 | 114 | var rootItem = treeItemForObject(obj, url); |
aoqi@0 | 115 | var tree = new TreeView(rootItem); |
aoqi@0 | 116 | var root = new StackPane(); |
aoqi@0 | 117 | root.children.add(tree); |
aoqi@0 | 118 | stage.scene = new Scene(root, 300, 450); |
aoqi@0 | 119 | stage.show(); |
aoqi@0 | 120 | } |