nashorn/samples/dynalink/dom_linker_gutenberg.js
author amurillo
Wed, 13 Jan 2016 12:45:36 -0800
changeset 35234 854fc566323e
parent 34549 412a690d4414
permissions -rw-r--r--
8146660: Resolve merge issue in resulting from sun.misc.VM move to jdk.internal.misc Reviewed-by: twisti, erikj, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34455
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents: 34452
diff changeset
     1
#// Usage: jjs -cp dom_linker.jar -scripting dom_linker_gutenberg.js
34452
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
     2
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
     3
/*
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
     4
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
     5
 *
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
     6
 * Redistribution and use in source and binary forms, with or without
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
     7
 * modification, are permitted provided that the following conditions
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
     8
 * are met:
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
     9
 *
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    10
 *   - Redistributions of source code must retain the above copyright
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    11
 *     notice, this list of conditions and the following disclaimer.
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    12
 *
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    13
 *   - Redistributions in binary form must reproduce the above copyright
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    14
 *     notice, this list of conditions and the following disclaimer in the
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    15
 *     documentation and/or other materials provided with the distribution.
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    16
 *
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    17
 *   - Neither the name of Oracle nor the names of its
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    18
 *     contributors may be used to endorse or promote products derived
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    19
 *     from this software without specific prior written permission.
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    20
 *
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    21
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    22
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    23
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    24
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    25
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    26
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    27
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    28
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    29
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    30
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    31
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    32
 */
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    33
34455
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents: 34452
diff changeset
    34
// This script depends on DOM Element dynalink linker!
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents: 34452
diff changeset
    35
// Without that linker, this script will fail to run!
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents: 34452
diff changeset
    36
34452
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    37
// Simple example that demonstrates reading XML Rss feed
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    38
// to generate a HTML file from script and show it by browser.
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    39
// Uses XML DOM parser along with DOM element pluggable dynalink linker
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    40
// for ease of navigation of DOM (child) elements by name.
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    41
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    42
// Java classes used
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    43
var DocBuilderFac = Java.type("javax.xml.parsers.DocumentBuilderFactory");
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    44
var Node = Java.type("org.w3c.dom.Node");
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    45
var File = Java.type("java.io.File");
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    46
var FileWriter = Java.type("java.io.FileWriter");
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    47
var PrintWriter = Java.type("java.io.PrintWriter");
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    48
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    49
// parse XML from uri and return Document
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    50
function parseXML(uri) {
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    51
    var docBuilder = DocBuilderFac.newInstance().newDocumentBuilder();
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    52
    return docBuilder["parse(java.lang.String)"](uri);
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    53
}
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    54
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    55
// generate HTML using here-doc and string interpolation
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    56
function getBooksHtml() {
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    57
    var doc = parseXML("http://www.gutenberg.org/cache/epub/feeds/today.rss");
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    58
    // wrap document root Element as script convenient object
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    59
    var rss = doc.documentElement;
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    60
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    61
    var str = <<HEAD
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    62
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    63
<html>
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    64
<title>${rss._channel._title._}</title>
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    65
<body>
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    66
<h1>${rss._channel._description._}</h1>
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    67
<p>
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    68
Published on ${rss._channel._pubDate._}
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    69
</p>
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    70
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    71
HEAD
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    72
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    73
    var items = rss._channel._item;
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    74
    for each (var i in items) {
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    75
        str += <<LIST
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    76
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    77
<dl>
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    78
<dt><a href="${i._link._}">${i._title._}</a></dt>
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    79
<dd>${i._description._}</dd>
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    80
</dl>
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    81
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    82
LIST
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    83
    }
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    84
    str += <<END
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    85
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    86
</body>
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    87
</html>
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    88
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    89
END
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    90
    return str;
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    91
}
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    92
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    93
// write the string to the given file
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    94
function writeTo(file, str) {
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    95
    var w = new PrintWriter(new FileWriter(file));
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    96
    try {
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    97
        w.print(str);
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    98
    } finally {
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
    99
        w.close();
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
   100
    }
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
   101
}
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
   102
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
   103
// generate books HTML
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
   104
var str = getBooksHtml();
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
   105
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
   106
// write to file. __DIR__ is directory where
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
   107
// this script is stored.
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
   108
var file = new File(__DIR__ + "books.html");
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
   109
writeTo(file, str);
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
   110
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
   111
// show it by desktop browser
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
   112
try {
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
   113
    var Desktop = Java.type("java.awt.Desktop");
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
   114
    Desktop.desktop.browse(file.toURI());
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
   115
} catch (e) {
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
   116
    print(e);
c34f322b89b9 8144230: Add a sample for pluggable dynalink linker
sundar
parents:
diff changeset
   117
}