aoqi@0: #// Usage: jjs -scripting gutenberg.js aoqi@0: aoqi@0: /* aoqi@0: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * aoqi@0: * Redistribution and use in source and binary forms, with or without aoqi@0: * modification, are permitted provided that the following conditions aoqi@0: * are met: aoqi@0: * aoqi@0: * - Redistributions of source code must retain the above copyright aoqi@0: * notice, this list of conditions and the following disclaimer. aoqi@0: * aoqi@0: * - Redistributions in binary form must reproduce the above copyright aoqi@0: * notice, this list of conditions and the following disclaimer in the aoqi@0: * documentation and/or other materials provided with the distribution. aoqi@0: * aoqi@0: * - Neither the name of Oracle nor the names of its aoqi@0: * contributors may be used to endorse or promote products derived aoqi@0: * from this software without specific prior written permission. aoqi@0: * aoqi@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS aoqi@0: * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, aoqi@0: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR aoqi@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR aoqi@0: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, aoqi@0: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, aoqi@0: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR aoqi@0: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF aoqi@0: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING aoqi@0: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS aoqi@0: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. aoqi@0: */ aoqi@0: aoqi@0: // Simple example that demonstrates reading XML Rss feed aoqi@0: // to generate a HTML file from script and show it by browser aoqi@0: aoqi@0: // Java classes used aoqi@0: var Characters = Java.type("javax.xml.stream.events.Characters"); aoqi@0: var Factory = Java.type("javax.xml.stream.XMLInputFactory"); aoqi@0: var File = Java.type("java.io.File"); aoqi@0: var FileWriter = Java.type("java.io.FileWriter"); aoqi@0: var PrintWriter = Java.type("java.io.PrintWriter"); aoqi@0: var URL = Java.type("java.net.URL"); aoqi@0: aoqi@0: // read Rss feed from a URL. Returns an array aoqi@0: // of objects having only title and link properties aoqi@0: function readRssFeed(url) { aoqi@0: var fac = Factory.newInstance(); aoqi@0: var reader = fac.createXMLEventReader(url.openStream()); aoqi@0: aoqi@0: // get text content from next event aoqi@0: function getChars() { aoqi@0: var result = ""; aoqi@0: var e = reader.nextEvent(); aoqi@0: if (e instanceof Characters) { aoqi@0: result = e.getData(); aoqi@0: } aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: var items = []; aoqi@0: var title, link; aoqi@0: var inItem = false; aoqi@0: while (reader.hasNext()) { aoqi@0: var evt = reader.nextEvent(); aoqi@0: if (evt.isStartElement()) { aoqi@0: var local = evt.name.localPart; aoqi@0: if (local == "item") { aoqi@0: // capture title, description now aoqi@0: inItem = true; aoqi@0: } aoqi@0: aoqi@0: if (inItem) { aoqi@0: switch (local) { aoqi@0: case 'title': aoqi@0: title = getChars(); aoqi@0: break; aoqi@0: case 'link': aoqi@0: link = getChars(); aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: } else if (evt.isEndElement()) { aoqi@0: var local = evt.name.localPart; aoqi@0: if (local == "item") { aoqi@0: // one item done, save it in result array aoqi@0: items.push({ title: title, link: link }); aoqi@0: inItem = false; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return items; aoqi@0: } aoqi@0: aoqi@0: // generate simple HTML for an RSS feed aoqi@0: function getBooksHtml() { aoqi@0: var url = new URL("http://www.gutenberg.org/cache/epub/feeds/today.rss"); aoqi@0: var items = readRssFeed(url); aoqi@0: aoqi@0: var str = ""; aoqi@0: return str; aoqi@0: } aoqi@0: aoqi@0: // write the string to the given file aoqi@0: function writeTo(file, str) { aoqi@0: var w = new PrintWriter(new FileWriter(file)); aoqi@0: try { aoqi@0: w.print(str); aoqi@0: } finally { aoqi@0: w.close(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // generate books HTML aoqi@0: var str = getBooksHtml(); aoqi@0: aoqi@0: // write to file. __DIR__ is directory where aoqi@0: // this script is stored. aoqi@0: var file = new File(__DIR__ + "books.html"); aoqi@0: writeTo(file, str); aoqi@0: aoqi@0: // show it by desktop browser aoqi@0: try { aoqi@0: var Desktop = Java.type("java.awt.Desktop"); aoqi@0: Desktop.desktop.browse(file.toURI()); aoqi@0: } catch (e) { aoqi@0: print(e); aoqi@0: }