Path: blob/aarch64-shenandoah-jdk8u272-b10/nashorn/samples/gutenberg.js
32278 views
#// Usage: jjs -scripting gutenberg.js12/*3* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* - Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* - Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* - Neither the name of Oracle nor the names of its17* contributors may be used to endorse or promote products derived18* from this software without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS21* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,22* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR23* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR24* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,25* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,26* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR27* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF28* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING29* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS30* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31*/3233// Simple example that demonstrates reading XML Rss feed34// to generate a HTML file from script and show it by browser3536// Java classes used37var Characters = Java.type("javax.xml.stream.events.Characters");38var Factory = Java.type("javax.xml.stream.XMLInputFactory");39var File = Java.type("java.io.File");40var FileWriter = Java.type("java.io.FileWriter");41var PrintWriter = Java.type("java.io.PrintWriter");42var URL = Java.type("java.net.URL");4344// read Rss feed from a URL. Returns an array45// of objects having only title and link properties46function readRssFeed(url) {47var fac = Factory.newInstance();48var reader = fac.createXMLEventReader(url.openStream());4950// get text content from next event51function getChars() {52var result = "";53var e = reader.nextEvent();54if (e instanceof Characters) {55result = e.getData();56}57return result;58}5960var items = [];61var title, link;62var inItem = false;63while (reader.hasNext()) {64var evt = reader.nextEvent();65if (evt.isStartElement()) {66var local = evt.name.localPart;67if (local == "item") {68// capture title, description now69inItem = true;70}7172if (inItem) {73switch (local) {74case 'title':75title = getChars();76break;77case 'link':78link = getChars();79break;80}81}82} else if (evt.isEndElement()) {83var local = evt.name.localPart;84if (local == "item") {85// one item done, save it in result array86items.push({ title: title, link: link });87inItem = false;88}89}90}9192return items;93}9495// generate simple HTML for an RSS feed96function getBooksHtml() {97var url = new URL("http://www.gutenberg.org/cache/epub/feeds/today.rss");98var items = readRssFeed(url);99100var str = "<ul>";101102// Nashorn's string interpolation and heredoc103// support is very handy in generating text content104// that is filled with elements from runtime objects.105// We insert title and link in <li> elements here.106for each (i in items) {107str += <<EOF108<li>109<a href="${i.link}">${i.title}</a>110</li>111EOF112}113str += "</ul>";114return str;115}116117// write the string to the given file118function writeTo(file, str) {119var w = new PrintWriter(new FileWriter(file));120try {121w.print(str);122} finally {123w.close();124}125}126127// generate books HTML128var str = getBooksHtml();129130// write to file. __DIR__ is directory where131// this script is stored.132var file = new File(__DIR__ + "books.html");133writeTo(file, str);134135// show it by desktop browser136try {137var Desktop = Java.type("java.awt.Desktop");138Desktop.desktop.browse(file.toURI());139} catch (e) {140print(e);141}142143144