nashorn/samples/gutenberg.js
author lana
Thu, 21 Jan 2016 09:46:08 -0800
changeset 35321 fb8ec0381d33
parent 26067 b32ccc3a76c9
permissions -rw-r--r--
Added tag jdk-9+102 for changeset 722ab3267903
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 gutenberg.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
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    36
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    37
// Java classes used
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    38
var Characters = Java.type("javax.xml.stream.events.Characters");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    39
var Factory = Java.type("javax.xml.stream.XMLInputFactory");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    40
var File = Java.type("java.io.File");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    41
var FileWriter = Java.type("java.io.FileWriter");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    42
var PrintWriter = Java.type("java.io.PrintWriter");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    43
var URL = Java.type("java.net.URL");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    44
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    45
// read Rss feed from a URL. Returns an array
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    46
// of objects having only title and link properties
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    47
function readRssFeed(url) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    48
    var fac = Factory.newInstance();
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    49
    var reader = fac.createXMLEventReader(url.openStream());
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    50
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    51
    // get text content from next event
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    52
    function getChars() {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    53
        var result = "";
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    54
        var e = reader.nextEvent();
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    55
        if (e instanceof Characters) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    56
            result = e.getData();
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    57
        }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    58
        return result;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    59
    }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    60
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    61
    var items = [];
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    62
    var title, link;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    63
    var inItem = false;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    64
    while (reader.hasNext()) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    65
        var evt = reader.nextEvent();
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    66
        if (evt.isStartElement()) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    67
            var local = evt.name.localPart;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    68
            if (local == "item") {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    69
               // capture title, description now
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    70
               inItem = true;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    71
            }
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 24283
diff changeset
    72
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    73
            if (inItem) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    74
                switch (local) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    75
                    case 'title':
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    76
                        title = getChars();
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    77
                        break;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    78
                    case 'link':
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    79
                        link = getChars();
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    80
                        break;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    81
                }
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
        } else if (evt.isEndElement()) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    84
            var local = evt.name.localPart;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    85
            if (local == "item") {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    86
                // one item done, save it in result array
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    87
                items.push({ title: title, link: link });
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    88
                inItem = false;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    89
            }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    90
        }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    91
    }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    92
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    93
    return items;
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
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    96
// generate simple HTML for an RSS feed
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    97
function getBooksHtml() {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    98
    var url = new URL("http://www.gutenberg.org/cache/epub/feeds/today.rss");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    99
    var items = readRssFeed(url);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   100
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   101
    var str = "<ul>";
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
    // Nashorn's string interpolation and heredoc
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   104
    // support is very handy in generating text content
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   105
    // that is filled with elements from runtime objects.
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   106
    // We insert title and link in <li> elements here.
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   107
    for each (i in items) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   108
        str += <<EOF
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   109
<li>
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   110
    <a href="${i.link}">${i.title}</a>
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   111
</li>
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   112
EOF
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   113
    }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   114
    str += "</ul>";
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   115
    return str;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   116
}
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   117
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   118
// write the string to the given file
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   119
function writeTo(file, str) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   120
    var w = new PrintWriter(new FileWriter(file));
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   121
    try {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   122
        w.print(str);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   123
    } finally {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   124
        w.close();
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
}
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   127
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   128
// generate books HTML
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   129
var str = getBooksHtml();
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   130
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   131
// write to file. __DIR__ is directory where
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   132
// this script is stored.
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   133
var file = new File(__DIR__ + "books.html");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   134
writeTo(file, str);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   135
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   136
// show it by desktop browser
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   137
try {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   138
    var Desktop = Java.type("java.awt.Desktop");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   139
    Desktop.desktop.browse(file.toURI());
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   140
} catch (e) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   141
    print(e);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   142
}