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