Download presentation
Presentation is loading. Please wait.
Published byGodwin Stevenson Modified over 9 years ago
1
Rhino – JavaScript for Java
2
Speaker: Brent Wilkins Hertz – Applications Consultant – Cobol developer – 20+ years – PowerBuilder developer – 6 years – Java developer – 10+ years – Email: bwilkins@hertz.com Rhino – JavaScript for Java
3
What is it? Rhino is an open-source implementation of JavaScript written entirely in Java. It is typically embedded into Java applications to provide scripting to end users. Rhino – JavaScript for Java
4
Most people who have used JavaScript before have done so by adding scripts to their HTML web pages. However, Rhino is an implementation of the core language only and doesn't contain objects or methods for manipulating HTML documents. Rhino contains: All the features of JavaScript 1.7 Allows direct scripting of Java A JavaScript shell for executing JavaScript scripts A JavaScript compiler to transform JavaScript source files into Java class files A JavaScript debugger for scripts executed with Rhino Rhino – JavaScript for Java
5
Outline: JavaScript context Optimization levels Initializing and scripting the basic Java objects Scripting custom objects Evaluating JavaScript expressions Rhino – JavaScript for Java
6
JavaScript Context The Rhino JavaScript Context object is used to store thread-specific information about the execution environment. There should be one and only one Context associated with each thread that will be executing JavaScript. Import org.mozilla.javascript.Context; To associate the current thread with a Context, simply call the enter method of Context: Context jsContext = Context.enter(); Context.exit(); Once you are done with execution, simply exit the Context: Rhino – JavaScript for Java
7
Optimization Levels Interpretive mode is used. No objects are compiled. 0No optimization is performed. Objects are compiled to bytecode. 1 – 9All optimizations are performed. Rhino – JavaScript for Java
8
Initializing and scripting the basic Java objects The basic Java objects (String, Boolean, Integer, etc.) must be initialized within the JavaScript Context. This allows for access to custom Java objects from with the Context. org.mozilla.javascript.Scriptable Scriptable script = jsContext.initStandardObjects(); Rhino – JavaScript for Java
9
Demo time Rhino – JavaScript for Java
10
Scripting custom objects Convert the object from Java to a JavaScript scriptable object Scriptable jsArgs = Context.toObject( obj, script ); Add the JavaScript object to the other Scriptable objects in the context script.put( objectName, script, jsArgs ); Rhino – JavaScript for Java
11
Evaluating JavaScript expressions Object result = jsContext.evaluateString( script, expression, expression, 1, null ); Determine the result type of the return object: String String stringResult = Context.toString( result ); Boolean boolean boolResult = Context.toBoolean( result );
12
Demo time Rhino – JavaScript for Java
13
Thermal Printing at Hertz Must be able to access data from the Rental Agreement Must be able to evaluate psuedocode that contains: – AND, OR – () order of operations – =, <>,, = – IN and NIN – Evaluates if a variable is in a group of values Rhino – JavaScript for Java
14
Thermal Printing at Hertz Must be able to access data from the Rental Agreement Must be able to evaluate psuedocode that contains: – AND, OR&&, || – () order of operations – =, <>,, ===, != – IN and NIN – Evaluates if a variable is in a group of values INJava method isInGroup( “|” + var + “|”, “|value|value|value|” ) NIN!isInGroup() Rhino – JavaScript for Java
15
Demo time Rhino – JavaScript for Java
16
Resources http://www.mozilla.org/rhino/ http://www.rhino-tutorial.buss.hk/
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.