author | sundar |
Thu, 12 Nov 2015 12:30:15 +0530 | |
changeset 33693 | cc0617a78522 |
parent 29409 | 613987e37f79 |
permissions | -rw-r--r-- |
29409
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
1 |
#// Usage: jjs -scripting cricket.js |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
2 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
3 |
/* |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
4 |
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
5 |
* |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
6 |
* Redistribution and use in source and binary forms, with or without |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
7 |
* modification, are permitted provided that the following conditions |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
8 |
* are met: |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
9 |
* |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
10 |
* - Redistributions of source code must retain the above copyright |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
11 |
* notice, this list of conditions and the following disclaimer. |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
12 |
* |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
13 |
* - Redistributions in binary form must reproduce the above copyright |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
14 |
* notice, this list of conditions and the following disclaimer in the |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
15 |
* documentation and/or other materials provided with the distribution. |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
16 |
* |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
17 |
* - Neither the name of Oracle nor the names of its |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
18 |
* contributors may be used to endorse or promote products derived |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
19 |
* from this software without specific prior written permission. |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
20 |
* |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
21 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
22 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
23 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
24 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
25 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
26 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
27 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
28 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
29 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
30 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
31 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
32 |
*/ |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
33 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
34 |
// Example that demonstrates reading XML Rss feed. |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
35 |
// XML DOM Document element is wrapped by script |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
36 |
// "proxy" (JSAdapter constructor) |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
37 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
38 |
// Java classes used |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
39 |
var DocBuilderFac = Java.type("javax.xml.parsers.DocumentBuilderFactory"); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
40 |
var Node = Java.type("org.w3c.dom.Node"); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
41 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
42 |
// constants from Node class |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
43 |
var ELEMENT_NODE = Node.ELEMENT_NODE; |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
44 |
var TEXT_NODE = Node.TEXT_NODE; |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
45 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
46 |
// parse XML from uri and return Document |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
47 |
function parseXML(uri) { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
48 |
var docBuilder = DocBuilderFac.newInstance().newDocumentBuilder(); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
49 |
return docBuilder["parse(java.lang.String)"](uri); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
50 |
} |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
51 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
52 |
// get child Elements of given name of the parent element given |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
53 |
function getChildElements(elem, name) { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
54 |
var nodeList = elem.childNodes; |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
55 |
var childElems = []; |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
56 |
var len = nodeList.length; |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
57 |
for (var i = 0; i < len; i++) { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
58 |
var node = nodeList.item(i); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
59 |
if (node.nodeType == ELEMENT_NODE && |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
60 |
node.tagName == name) { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
61 |
childElems.push(wrapElement(node)); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
62 |
} |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
63 |
} |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
64 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
65 |
return childElems; |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
66 |
} |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
67 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
68 |
// get concatenated child text content of an Element |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
69 |
function getElemText(elem) { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
70 |
var nodeList = elem.childNodes; |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
71 |
var len = nodeList.length; |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
72 |
var text = ''; |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
73 |
for (var i = 0; i < len; i++) { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
74 |
var node = nodeList.item(i); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
75 |
if (node.nodeType == TEXT_NODE) { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
76 |
text += node.nodeValue; |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
77 |
} |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
78 |
} |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
79 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
80 |
return text; |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
81 |
} |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
82 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
83 |
// Wrap DOM Element object as a convenient script object |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
84 |
// using JSAdapter. JSAdapter is like java.lang.reflect.Proxy |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
85 |
// in that it allows property access, method calls be trapped |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
86 |
// by 'magic' methods like __get__, __call__. |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
87 |
function wrapElement(elem) { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
88 |
if (! elem) { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
89 |
return elem; |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
90 |
} |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
91 |
return new JSAdapter() { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
92 |
// getter to expose child elements and attributes by name |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
93 |
__get__: function(name) { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
94 |
if (typeof name == 'string') { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
95 |
if (name.startsWith('@')) { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
96 |
var attr = elem.getAttributeNode(name.substring(1)); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
97 |
return !attr? undefined : attr.value; |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
98 |
} |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
99 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
100 |
var arr = getChildElements(elem, name); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
101 |
if (arr.length == 1) { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
102 |
// single child element, expose as single element |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
103 |
return arr[0]; |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
104 |
} else { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
105 |
// multiple children of given name, expose as array |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
106 |
return arr; |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
107 |
} |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
108 |
} |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
109 |
return undefined; |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
110 |
}, |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
111 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
112 |
__call__: function(name) { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
113 |
// toString override to get text content of this Element |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
114 |
if (name == 'toString' || name == 'valueOf') { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
115 |
return getElemText(elem); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
116 |
} |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
117 |
return undefined; |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
118 |
} |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
119 |
} |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
120 |
} |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
121 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
122 |
function printCricketScore() { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
123 |
var doc = parseXML("http://static.cricinfo.com/rss/livescores.xml"); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
124 |
// wrap document root Element as script convenient object |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
125 |
var rss = wrapElement(doc.documentElement); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
126 |
print("rss file version " + rss['@version']); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
127 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
128 |
print(rss.channel.title); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
129 |
print(rss.channel.description); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
130 |
print(rss.channel.pubDate); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
131 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
132 |
print("====================="); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
133 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
134 |
var items = rss.channel.item; |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
135 |
for each (var i in items) { |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
136 |
print(i.description); |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
137 |
} |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
138 |
} |
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
139 |
|
613987e37f79
8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff
changeset
|
140 |
printCricketScore(); |