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