Javascript or
ECMAScript
is an industry standard scripting language most
commonly used for client-side programming
within a web browser.
NetKernel provides server-side JavaScript development
by integrating the Mozilla
Rhino
JavaScript library.
NetKernel's support of JavaScript provides the opportunity to code
both client and server side programs in the same language.
In NetKernel, JavaScript programs are dynamically compiled to JVM bytecodes.
Refer to the detailed
available JavaScript Guide
for more information.
E4X
The
E4X
extension to JavaScript provides first-class treatment of XML resources
within the language.
Chris Wensel has developed a production proven E4X NetKernel library mod-e4x which provides a number of helper
functions to support E4X on NetKernel. He has kindly made the code public and published an installable
module and tutorial
.
E4X by Example
We also include a brief tutorial on E4X...
create a new literal XML document:
doc = <a><b c="1"/><b c="2"/></a>;
create cursor into document
add/assign an attribute
delete a node:
iterate over nodes:
for each (b in doc.b) { x = b.@c; }
find all decendants:
insert a new fragment:
frag = <b c="3"/>;
doc.insertChildAfter(doc.b[1], frag);
insert a new fragment at beginning:
frag = <b c="0"/>;
doc.insertChildAfter(null, frag);
parameterized locate:
cValue=2;
bNode = doc.b.(@c==cValue);
parameterized text in literal:
text="text here";
frag = <b c="4">{text}</b>;
parameterized attribute in literal:
text="4";
frag = <b/>;
frag.@c=text;
count matches:
NetKernel Specifics
To get E4X objects in and out of a script first you need to import the xml-core XmlObject libraries which provides IAspectXmlObject...
importPackage(Packages.org.ten60.netkernel.xml.representation);
To source a resource as an XML object:
org=new XML(context.sourceAspect(
"http://www.1060.org/",
IAspectXmlObject).getXmlObject()
);
To create a response from an XML object:
aspect=new XmlObjectAspect(doc.getXmlObject());
response=context.createResponseFrom(aspect);
response.setMimeType("text/xml");
context.setResponse(response);
To process an XML object with, for example, an XSLT transform:
req = context.createSubRequest();
req.setURI("active:xslt");
req.addArgument("operator","style.xsl");
req.addArgument("operand", new XmlObjectAspect(doc.getXmlObject()));
req.setAspectClass(IAspectXmlObject);
table=new XML(context.issueSubRequestForAspect(req).getXmlObject());
Putting it all together here's a hello world E4X script...
importPackage(Packages.org.ten60.netkernel.xml.representation);
importPackage(Packages.java.lang);
x= <a><b>hello</b></a>;
y= <c>world</c>;
x.* += y;
System.out.println(x);
//Construct a new XmlObjectAspect
xoa=new XmlObjectAspect(x.getXmlObject());
//Return Response
resp=context.createResponseFrom(xoa);
resp.setMimeType("text/xml");
context.setResponse(resp);