1 #// Usage: jjs -scripting cricket.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 // Example that demonstrates reading XML Rss feed.
  35 // XML DOM Document element is wrapped by script
  36 // "proxy" (JSAdapter constructor)
  37 
  38 // Java classes used
  39 var DocBuilderFac = Java.type("javax.xml.parsers.DocumentBuilderFactory");
  40 var Node = Java.type("org.w3c.dom.Node");
  41 
  42 // constants from Node class
  43 var ELEMENT_NODE = Node.ELEMENT_NODE;
  44 var TEXT_NODE = Node.TEXT_NODE;
  45 
  46 // parse XML from uri and return Document
  47 function parseXML(uri) {
  48     var docBuilder = DocBuilderFac.newInstance().newDocumentBuilder();
  49     return docBuilder["parse(java.lang.String)"](uri);
  50 }
  51 
  52 // get child Elements of given name of the parent element given
  53 function getChildElements(elem, name) {
  54     var nodeList = elem.childNodes;
  55     var childElems = [];
  56     var len = nodeList.length;
  57     for (var i = 0; i < len; i++) {
  58         var node = nodeList.item(i);
  59         if (node.nodeType == ELEMENT_NODE &&
  60             node.tagName == name) {
  61             childElems.push(wrapElement(node));
  62         }
  63     }
  64 
  65     return childElems;
  66 }
  67 
  68 // get concatenated child text content of an Element
  69 function getElemText(elem) {
  70     var nodeList = elem.childNodes;
  71     var len = nodeList.length;
  72     var text = '';
  73     for (var i = 0; i < len; i++) {
  74         var node = nodeList.item(i);
  75         if (node.nodeType == TEXT_NODE) {
  76             text += node.nodeValue;
  77         }
  78     }
  79 
  80     return text;
  81 }
  82 
  83 // Wrap DOM Element object as a convenient script object
  84 // using JSAdapter. JSAdapter is like java.lang.reflect.Proxy
  85 // in that it allows property access, method calls be trapped
  86 // by 'magic' methods like __get__, __call__.
  87 function wrapElement(elem) {
  88     if (! elem) {
  89         return elem;
  90     }
  91     return new JSAdapter() {
  92         // getter to expose child elements and attributes by name
  93         __get__: function(name) {
  94             if (typeof name == 'string') {
  95                 if (name.startsWith('@')) {
  96                     var attr = elem.getAttributeNode(name.substring(1));
  97                     return !attr? undefined : attr.value;
  98                 }
  99 
 100                 var arr = getChildElements(elem, name);
 101                 if (arr.length == 1) {
 102                     // single child element, expose as single element
 103                     return arr[0];
 104                 } else {
 105                     // multiple children of given name, expose as array
 106                     return arr;
 107                 }
 108             }
 109             return undefined;
 110         },
 111 
 112         __call__: function(name) {
 113             // toString override to get text content of this Element
 114             if (name == 'toString' || name == 'valueOf') {
 115                 return getElemText(elem);
 116             }
 117             return undefined;
 118         }
 119     }
 120 }
 121 
 122 function printCricketScore() {
 123     var doc = parseXML("http://static.cricinfo.com/rss/livescores.xml");
 124     // wrap document root Element as script convenient object
 125     var rss = wrapElement(doc.documentElement);
 126     print("rss file version " + rss['@version']);
 127 
 128     print(rss.channel.title);
 129     print(rss.channel.description);
 130     print(rss.channel.pubDate);
 131 
 132     print("=====================");
 133 
 134     var items = rss.channel.item;
 135     for each (var i in items) {
 136         print(i.description);
 137     }
 138 }
 139 
 140 printCricketScore();