Monday, May 3, 2010

Using the Rhino JavaScript engine in XPages

Not sure how useful this is yet, but if you want to use the Rhino engine with XPages, here's how:
var rhinoEngine = new javax.script.ScriptEngineManager().getEngineByName(
"JavaScript");
// to eval JavaScript string: rhinoEngine.eval( jsString );

Oracle Tutorial

General API for javax.script.ScriptEngine

Java 6 uses Rhino v1.6R2 (documentation/etc)


One thing that just came to mind is maybe to use Rhino where IBM JScript fails.

E.g.
String.prototype.rhinoMatch = function( regExpString, modifiers ){
var rhino = new javax.script.ScriptEngineManager().getEngineByName("JavaScript");
rhino.eval(
'var result = "' + this + '".match( ' + regExpString + modifiers + ' )' );
return rhino.get( 'result' )
}

'{lastName}, {firstName}'.rhinoMatch( '/\\{(\\w+)\\}/', 'g' );
// returns array, ["{lastName}", "{firstName}"]

2 comments:

Stephan H. Wissel said...

You probably can push that one step further. XPages allows you to add your own languages (in source it is #{javascript:.... }. Almost nothing would stop you to write #{rhino: ....} except the missing editor support. Just food for your thoughts.

Tommy Valand said...

Tell me more. Tell me more. :)

Do you have to mess with files on the server/etc. or can to control this on an app by app basis?