nashorn/samples/jsadapter_dom.js
author vlivanov
Mon, 09 May 2016 12:39:41 +0300
changeset 38358 cb99c6d2af1b
parent 26067 b32ccc3a76c9
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
     1
#// Usage: jjs -scripting jsadapter_dom.js
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
     2
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
     3
/*
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
     4
 * 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
     5
 *
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
     6
 * Redistribution and use in source and binary forms, with or without
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
     7
 * modification, are permitted provided that the following conditions
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
     8
 * are met:
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 24283
diff changeset
     9
 *
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    10
 *   - Redistributions of source code must retain the above copyright
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    11
 *     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
    12
 *
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    13
 *   - Redistributions in binary form must reproduce the above copyright
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    14
 *     notice, this list of conditions and the following disclaimer in the
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    15
 *     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
    16
 *
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    17
 *   - Neither the name of Oracle nor the names of its
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    18
 *     contributors may be used to endorse or promote products derived
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    19
 *     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
    20
 *
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    21
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    22
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    23
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    24
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    25
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    26
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    27
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    28
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    29
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    30
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    31
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    32
 */
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    33
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    34
// Simple example that demonstrates reading XML Rss feed
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    35
// to generate a HTML file from script and show it by browser
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 24283
diff changeset
    36
// Uses XML DOM parser and DOM element wrapped by script
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    37
// "proxy" (JSAdapter constructor)
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    38
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    39
// Java classes used
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    40
var DocBuilderFac = Java.type("javax.xml.parsers.DocumentBuilderFactory");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    41
var Node = Java.type("org.w3c.dom.Node");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    42
var File = Java.type("java.io.File");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    43
var FileWriter = Java.type("java.io.FileWriter");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    44
var PrintWriter = Java.type("java.io.PrintWriter");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    45
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    46
// constants from Node class
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    47
var ELEMENT_NODE = Node.ELEMENT_NODE;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    48
var TEXT_NODE = Node.TEXT_NODE;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    49
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    50
// parse XML from uri and return Document
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    51
function parseXML(uri) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    52
    var docBuilder = DocBuilderFac.newInstance().newDocumentBuilder();
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    53
    return docBuilder["parse(java.lang.String)"](uri);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    54
}
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    55
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    56
// get child Elements of given name of the parent element given
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    57
function getChildElements(elem, name) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    58
    var nodeList = elem.childNodes;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    59
    var childElems = [];
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    60
    var len = nodeList.length;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    61
    for (var i = 0; i < len; i++) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    62
        var node = nodeList.item(i);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    63
        if (node.nodeType == ELEMENT_NODE &&
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    64
            node.tagName == name) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    65
            childElems.push(wrapElement(node));
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    66
        }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    67
    }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    68
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    69
    return childElems;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    70
}
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    71
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    72
// get concatenated child text content of an Element
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    73
function getElemText(elem) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    74
    var nodeList = elem.childNodes;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    75
    var len = nodeList.length;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    76
    var text = '';
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    77
    for (var i = 0; i < len; i++) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    78
        var node = nodeList.item(i);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    79
        if (node.nodeType == TEXT_NODE) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    80
            text += node.nodeValue;
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 24283
diff changeset
    81
        }
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    82
    }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    83
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    84
    return text;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    85
}
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    86
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    87
// Wrap DOM Element object as a convenient script object
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    88
// using JSAdapter. JSAdapter is like java.lang.reflect.Proxy
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    89
// in that it allows property access, method calls be trapped
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    90
// by 'magic' methods like __get__, __call__.
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    91
function wrapElement(elem) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    92
    if (! elem) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    93
        return elem;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    94
    }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    95
    return new JSAdapter() {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    96
        // getter to expose child elements and attributes by name
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    97
        __get__: function(name) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    98
            if (typeof name == 'string') {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    99
                if (name.startsWith('@')) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   100
                    var attr = elem.getAttributeNode(name.substring(1));
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   101
                    return !attr? undefined : attr.value;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   102
                }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   103
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   104
                var arr = getChildElements(elem, name);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   105
                if (arr.length == 1) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   106
                    // single child element, expose as single element
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   107
                    return arr[0];
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   108
                } else {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   109
                    // multiple children of given name, expose as array
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   110
                    return arr;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   111
                }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   112
            }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   113
            return undefined;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   114
        },
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   115
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   116
        __call__: function(name) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   117
            // toString override to get text content of this Element
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   118
            if (name == 'toString' || name == 'valueOf') {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   119
                return getElemText(elem);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   120
            }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   121
            return undefined;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   122
        }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   123
    }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   124
}
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   125
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   126
// generate HTML using here-doc and string interpolation
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   127
function getBooksHtml() {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   128
    var doc = parseXML("http://www.gutenberg.org/cache/epub/feeds/today.rss");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   129
    // wrap document root Element as script convenient object
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   130
    var rss = wrapElement(doc.documentElement);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   131
    print("rss file version " + rss['@version']);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   132
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   133
    var str = <<HEAD
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   134
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   135
<html>
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   136
<title>${rss.channel.title}</title>
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   137
<body>
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   138
<h1>${rss.channel.description}</h1>
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   139
<p>
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   140
Published on ${rss.channel.pubDate}
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   141
</p>
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   142
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   143
HEAD
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   144
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   145
    var items = rss.channel.item;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   146
    for each (var i in items) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   147
        str += <<LIST
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   148
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   149
<dl>
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   150
<dt><a href="${i.link}">${i.title}</a></dt>
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   151
<dd>${i.description}</dd>
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   152
</dl>
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   153
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   154
LIST
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   155
    }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   156
    str += <<END
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   157
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   158
</body>
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   159
</html>
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   160
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   161
END
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   162
    return str;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   163
}
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   164
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   165
// write the string to the given file
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   166
function writeTo(file, str) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   167
    var w = new PrintWriter(new FileWriter(file));
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   168
    try {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   169
        w.print(str);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   170
    } finally {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   171
        w.close();
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   172
    }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   173
}
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   174
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   175
// generate books HTML
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   176
var str = getBooksHtml();
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   177
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   178
// write to file. __DIR__ is directory where
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   179
// this script is stored.
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   180
var file = new File(__DIR__ + "books.html");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   181
writeTo(file, str);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   182
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   183
// show it by desktop browser
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   184
try {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   185
    var Desktop = Java.type("java.awt.Desktop");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   186
    Desktop.desktop.browse(file.toURI());
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   187
} catch (e) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   188
    print(e);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   189
}