nashorn/samples/xmlviewer.js
author amlu
Fri, 07 Jul 2017 12:42:53 +0800
changeset 45836 37379617f15a
parent 29409 613987e37f79
permissions -rw-r--r--
8183378: Refactor java/lang/System/MacEncoding/MacJNUEncoding.sh to java Reviewed-by: bchristi, xiaofeya
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29409
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     1
#jjs -fx xmlviewer.js [-- <url-of-xml-doc>]
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     2
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     3
/*
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     4
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     5
 *
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     6
 * Redistribution and use in source and binary forms, with or without
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     7
 * modification, are permitted provided that the following conditions
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     8
 * are met:
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     9
 *
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    10
 *   - Redistributions of source code must retain the above copyright
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    11
 *     notice, this list of conditions and the following disclaimer.
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    12
 *
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    13
 *   - Redistributions in binary form must reproduce the above copyright
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    14
 *     notice, this list of conditions and the following disclaimer in the
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    15
 *     documentation and/or other materials provided with the distribution.
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    16
 *
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    17
 *   - Neither the name of Oracle nor the names of its
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    18
 *     contributors may be used to endorse or promote products derived
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    19
 *     from this software without specific prior written permission.
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    20
 *
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    21
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    22
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    23
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    24
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    25
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    26
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    27
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    28
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    29
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    30
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    31
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    32
 */
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    33
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    34
if (! $OPTIONS._fx) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    35
    print("Usage: jjs -fx xmlviewer.js [-- <url-of-xml-doc>]");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    36
    exit(1);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    37
}
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    38
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    39
// Using JavaFX from Nashorn. See also:
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    40
// http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/javafx.html
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    41
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    42
// Simple sample to view a XML document as a JavaFX tree.
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    43
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    44
// JavaFX classes used
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    45
var StackPane = Java.type("javafx.scene.layout.StackPane");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    46
var Scene     = Java.type("javafx.scene.Scene");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    47
var TreeItem  = Java.type("javafx.scene.control.TreeItem");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    48
var TreeView  = Java.type("javafx.scene.control.TreeView");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    49
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    50
// XML DocumentBuilderFactory
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    51
var DocBuilderFac = Java.type("javax.xml.parsers.DocumentBuilderFactory");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    52
var Attr = Java.type("org.w3c.dom.Attr");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    53
var Element = Java.type("org.w3c.dom.Element");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    54
var Text = Java.type("org.w3c.dom.Text");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    55
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    56
// parse XML from uri and return Document
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    57
function parseXML(uri) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    58
    var docBuilder = DocBuilderFac.newInstance().newDocumentBuilder();
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    59
    docBuilder.validating = false;
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    60
    return docBuilder["parse(java.lang.String)"](uri);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    61
}
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    62
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    63
// Create a javafx TreeItem to view a XML element
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    64
function treeItemForObject(element, name) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    65
    var item = new TreeItem(name);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    66
    item.expanded = true;
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    67
    var attrs = element.attributes;
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    68
    var numAttrs = attrs.length;
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    69
    for (var a = 0; a < numAttrs; a++) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    70
        var attr = attrs.item(a);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    71
        var subitem = new TreeItem(attr.name + " = " + attr.value);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    72
        item.children.add(subitem);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    73
    }
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    74
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    75
    var childNodes = element.childNodes;
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    76
    var numNodes = childNodes.length;
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    77
    for (var n = 0; n < numNodes; n++) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    78
       var node = childNodes.item(n);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    79
       if (node instanceof Element) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    80
           var subitem = treeItemForObject(node, node.tagName);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    81
           item.children.add(subitem);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    82
       }
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    83
    }
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    84
    
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    85
    return item;
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    86
}
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    87
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    88
// Ofcourse, the best default URL is cricket score :) 
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    89
var DEFAULT_URL = "http://synd.cricbuzz.com/j2me/1.0/livematches.xml";
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    90
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    91
var url = arguments.length == 0? DEFAULT_URL : arguments[0];
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    92
var element = parseXML(url).getDocumentElement();
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    93
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    94
// JavaFX start method
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    95
function start(stage) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    96
    stage.title = "XML Viewer: " + url;
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    97
    var rootItem = treeItemForObject(element, element.tagName);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    98
    var tree = new TreeView(rootItem);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    99
    var root = new StackPane();
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   100
    root.children.add(tree);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   101
    stage.scene = new Scene(root, 300, 450);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   102
    stage.show();
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   103
}