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