Tue, 13 Jun 2017 16:30:24 -0700
Added tag jdk8u151-b01 for changeset 681ab42e7c73
aoqi@0 | 1 | #// Usage: jjs -scripting gutenberg.js |
aoqi@0 | 2 | |
aoqi@0 | 3 | /* |
aoqi@0 | 4 | * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
attila@962 | 5 | * |
aoqi@0 | 6 | * Redistribution and use in source and binary forms, with or without |
aoqi@0 | 7 | * modification, are permitted provided that the following conditions |
aoqi@0 | 8 | * are met: |
attila@962 | 9 | * |
aoqi@0 | 10 | * - Redistributions of source code must retain the above copyright |
aoqi@0 | 11 | * notice, this list of conditions and the following disclaimer. |
attila@962 | 12 | * |
aoqi@0 | 13 | * - Redistributions in binary form must reproduce the above copyright |
aoqi@0 | 14 | * notice, this list of conditions and the following disclaimer in the |
aoqi@0 | 15 | * documentation and/or other materials provided with the distribution. |
attila@962 | 16 | * |
aoqi@0 | 17 | * - Neither the name of Oracle nor the names of its |
aoqi@0 | 18 | * contributors may be used to endorse or promote products derived |
aoqi@0 | 19 | * from this software without specific prior written permission. |
attila@962 | 20 | * |
aoqi@0 | 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
aoqi@0 | 22 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
aoqi@0 | 23 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
aoqi@0 | 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
aoqi@0 | 25 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
aoqi@0 | 26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
aoqi@0 | 27 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
aoqi@0 | 28 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
aoqi@0 | 29 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
aoqi@0 | 30 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
aoqi@0 | 31 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
aoqi@0 | 32 | */ |
aoqi@0 | 33 | |
aoqi@0 | 34 | // Simple example that demonstrates reading XML Rss feed |
aoqi@0 | 35 | // to generate a HTML file from script and show it by browser |
aoqi@0 | 36 | |
aoqi@0 | 37 | // Java classes used |
aoqi@0 | 38 | var Characters = Java.type("javax.xml.stream.events.Characters"); |
aoqi@0 | 39 | var Factory = Java.type("javax.xml.stream.XMLInputFactory"); |
aoqi@0 | 40 | var File = Java.type("java.io.File"); |
aoqi@0 | 41 | var FileWriter = Java.type("java.io.FileWriter"); |
aoqi@0 | 42 | var PrintWriter = Java.type("java.io.PrintWriter"); |
aoqi@0 | 43 | var URL = Java.type("java.net.URL"); |
aoqi@0 | 44 | |
aoqi@0 | 45 | // read Rss feed from a URL. Returns an array |
aoqi@0 | 46 | // of objects having only title and link properties |
aoqi@0 | 47 | function readRssFeed(url) { |
aoqi@0 | 48 | var fac = Factory.newInstance(); |
aoqi@0 | 49 | var reader = fac.createXMLEventReader(url.openStream()); |
aoqi@0 | 50 | |
aoqi@0 | 51 | // get text content from next event |
aoqi@0 | 52 | function getChars() { |
aoqi@0 | 53 | var result = ""; |
aoqi@0 | 54 | var e = reader.nextEvent(); |
aoqi@0 | 55 | if (e instanceof Characters) { |
aoqi@0 | 56 | result = e.getData(); |
aoqi@0 | 57 | } |
aoqi@0 | 58 | return result; |
aoqi@0 | 59 | } |
aoqi@0 | 60 | |
aoqi@0 | 61 | var items = []; |
aoqi@0 | 62 | var title, link; |
aoqi@0 | 63 | var inItem = false; |
aoqi@0 | 64 | while (reader.hasNext()) { |
aoqi@0 | 65 | var evt = reader.nextEvent(); |
aoqi@0 | 66 | if (evt.isStartElement()) { |
aoqi@0 | 67 | var local = evt.name.localPart; |
aoqi@0 | 68 | if (local == "item") { |
aoqi@0 | 69 | // capture title, description now |
aoqi@0 | 70 | inItem = true; |
aoqi@0 | 71 | } |
attila@962 | 72 | |
aoqi@0 | 73 | if (inItem) { |
aoqi@0 | 74 | switch (local) { |
aoqi@0 | 75 | case 'title': |
aoqi@0 | 76 | title = getChars(); |
aoqi@0 | 77 | break; |
aoqi@0 | 78 | case 'link': |
aoqi@0 | 79 | link = getChars(); |
aoqi@0 | 80 | break; |
aoqi@0 | 81 | } |
aoqi@0 | 82 | } |
aoqi@0 | 83 | } else if (evt.isEndElement()) { |
aoqi@0 | 84 | var local = evt.name.localPart; |
aoqi@0 | 85 | if (local == "item") { |
aoqi@0 | 86 | // one item done, save it in result array |
aoqi@0 | 87 | items.push({ title: title, link: link }); |
aoqi@0 | 88 | inItem = false; |
aoqi@0 | 89 | } |
aoqi@0 | 90 | } |
aoqi@0 | 91 | } |
aoqi@0 | 92 | |
aoqi@0 | 93 | return items; |
aoqi@0 | 94 | } |
aoqi@0 | 95 | |
aoqi@0 | 96 | // generate simple HTML for an RSS feed |
aoqi@0 | 97 | function getBooksHtml() { |
aoqi@0 | 98 | var url = new URL("http://www.gutenberg.org/cache/epub/feeds/today.rss"); |
aoqi@0 | 99 | var items = readRssFeed(url); |
aoqi@0 | 100 | |
aoqi@0 | 101 | var str = "<ul>"; |
aoqi@0 | 102 | |
aoqi@0 | 103 | // Nashorn's string interpolation and heredoc |
aoqi@0 | 104 | // support is very handy in generating text content |
aoqi@0 | 105 | // that is filled with elements from runtime objects. |
aoqi@0 | 106 | // We insert title and link in <li> elements here. |
aoqi@0 | 107 | for each (i in items) { |
aoqi@0 | 108 | str += <<EOF |
aoqi@0 | 109 | <li> |
aoqi@0 | 110 | <a href="${i.link}">${i.title}</a> |
aoqi@0 | 111 | </li> |
aoqi@0 | 112 | EOF |
aoqi@0 | 113 | } |
aoqi@0 | 114 | str += "</ul>"; |
aoqi@0 | 115 | return str; |
aoqi@0 | 116 | } |
aoqi@0 | 117 | |
aoqi@0 | 118 | // write the string to the given file |
aoqi@0 | 119 | function writeTo(file, str) { |
aoqi@0 | 120 | var w = new PrintWriter(new FileWriter(file)); |
aoqi@0 | 121 | try { |
aoqi@0 | 122 | w.print(str); |
aoqi@0 | 123 | } finally { |
aoqi@0 | 124 | w.close(); |
aoqi@0 | 125 | } |
aoqi@0 | 126 | } |
aoqi@0 | 127 | |
aoqi@0 | 128 | // generate books HTML |
aoqi@0 | 129 | var str = getBooksHtml(); |
aoqi@0 | 130 | |
aoqi@0 | 131 | // write to file. __DIR__ is directory where |
aoqi@0 | 132 | // this script is stored. |
aoqi@0 | 133 | var file = new File(__DIR__ + "books.html"); |
aoqi@0 | 134 | writeTo(file, str); |
aoqi@0 | 135 | |
aoqi@0 | 136 | // show it by desktop browser |
aoqi@0 | 137 | try { |
aoqi@0 | 138 | var Desktop = Java.type("java.awt.Desktop"); |
aoqi@0 | 139 | Desktop.desktop.browse(file.toURI()); |
aoqi@0 | 140 | } catch (e) { |
aoqi@0 | 141 | print(e); |
aoqi@0 | 142 | } |