author | jlahoda |
Thu, 28 Jun 2018 10:05:39 +0200 | |
changeset 50867 | e84038f37713 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
24283 | 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. |
|
26067
b32ccc3a76c9
8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents:
24283
diff
changeset
|
7 |
* |
24283 | 8 |
* Redistribution and use in source and binary forms, with or without |
9 |
* modification, are permitted provided that the following conditions |
|
10 |
* are met: |
|
26067
b32ccc3a76c9
8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents:
24283
diff
changeset
|
11 |
* |
24283 | 12 |
* - Redistributions of source code must retain the above copyright |
13 |
* notice, this list of conditions and the following disclaimer. |
|
26067
b32ccc3a76c9
8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents:
24283
diff
changeset
|
14 |
* |
24283 | 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. |
|
26067
b32ccc3a76c9
8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents:
24283
diff
changeset
|
18 |
* |
24283 | 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. |
|
26067
b32ccc3a76c9
8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents:
24283
diff
changeset
|
22 |
* |
24283 | 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) { |
|
26067
b32ccc3a76c9
8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents:
24283
diff
changeset
|
55 |
// equivalent to |
b32ccc3a76c9
8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents:
24283
diff
changeset
|
56 |
// |
24283 | 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 |
||
45898 | 106 |
var DEFAULT_URL = "http://api.openweathermap.org/data/2.5/forecast?q=chennai,india&units=metric&mode=json&appid=9b2982987c080ae88d81f081dcb129e8"; |
24283 | 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 |
} |