Christopher Oliver says ...In order to reduce the barrier to entry of the Cocoon flow layer for Java programmers that haven't done much with JavaScript I thought I'd give an overview of some of the similarities and differences between
Read the full message in the cocoon-dev archives. In order to reduce the barrier to entry of the Cocoon flow layer for Java programmers that haven't done much with JavaScript I thought I'd give an overview of some of the similarities and differences between JavaScript (specifically Rhino) and Java. I also encourage you to play around with the Cocoon Flow debugger, as it contains an interactive environment where you can type in JavaScript code and see the results.
RegExp
These objects and functions are comprehensively documented in many places on the web so I won't describe them further here. Please see Quick Reference or Netscape Core JavaScript Reference 1.5.
JavaScript has two types of variables: global variables and local variables. Local variables are only visible in the function in which they are declared. However, they are also visible to other functions defined in the same scope. Inside a function definition local variables are identified by the var keyword: You should *always* use the var keyword to declare variables.
JavaScript statement syntax and control structures are pretty much the same as Java: if, for, switch, while, do, etc. JavaScript also supports an "in" operator that tests whether an object contains a named property: The JavaScript "for" statement together with the "in" operator supports a kind of "foreach": it iterates over the properties of an object: As you can see you can also use a string argument to the [] operator. This is equivalent to using the "." (dot) operator with an identifier literal, i.e. is the same as A new instance of an object can be created with the "new" operator: Alternatively you can use the literal syntax to create instances of built-in types:
Rhino includes bidirectional support for Java objects: you can create Java objects and call their methods, and you can extend Java classes and implement Java interfaces in JavaScript. Classes in packages under "java" are accessible directly in your scripts: Note that classes under "java.lang" are not automatically imported, however: All other java packages and classes are accessible under the property "Packages": You can get the effect of Java imports using the importPackage() and importClass() functions: In Java: In Rhino: import foo.*; importPackage(Packages.foo); import foo.Bar; importClass(Packages.foo.Bar); Note: Be sure to make importPackage(...) the first code line in your script, or you may get an "Ambiguous import" error on the second and following runs of your script when attempting to reference an imported class. Rhino understands Java bean properties, so if your Java classes have getters and setters you can access them as properties in JavaScript:
The Rhino interpreter that supports continuations adds the following two objects: ContinuationException See http://marc.theaimsgroup.com/?l=xml-cocoon-dev&m=102781075226697&w=2 for a description of these. And the Cocoon flow layer adds the following objects and functions to that:
For a higher level description of the Cocoon objects see: http://www.webweavertech.com/ovidiu/weblog/archives/000042.html For more on using Rhino for scripting Java see: http://www.mozilla.org/rhino/scriptjava.html JavaScript...''
JavaScript contains the following built-in objects and top-level functions:
Built-in Objects
Top-level Functions
Type of Variables
function foo() {
var x;
x = 3; // x is a local variable
y = 5; // y is a global variable
function inner() {
print(x); // refers to the var x declared above in foo()
}
}Statements
if ("propName" in obj) {
// do something
}for (i in obj) {
print(obj[i]);
} foo.bar
foo["bar"]
var x = new String();
Literal Syntax Equivalent using new
"" new String()
'' new String()
[] new Array()
{} new Object()Using Java Objects
var map = new java.util.HashMap();
var n = new java.lang.Integer(3);
var tree = new Packages.javax.swing.JTree();
var d = new java.util.Date();
d.year = 2003; // same effect as d.setYear(2003);
Continuations
// interface to various cocoon abstractions:
- cocoon
- environment // property of type org.apache.cocoon.environment.Environment
- parameters // JavaScript array of <not sure?>
- request // property of type org.apache.cocoon.environment.Request
- response // property of type org.apache.cocoon.environment.Response
- session // property of type org.apache.cocoon.environment.Session
- context // property of type org.apache.cocoon.environment.Context
- componentManager // property of type org.apache.avalon.framework.ComponentManager
- load(fileName) // loads a script
- createSession() // attaches this flow script instance to session
- removeSession() // detaches this flow script instance from session
// action/input/output module interfaces:
- callAction(type, source, parameters)
- inputModuleGetAttribute(type, attribute)
- outputModuleSetAttribute(type, attribute, value)
// flow API
- sendPage(uri, bizData); // call presentation layer to present bizData
- sendPageAndWait(uri, bizData, timeToLive) // call presentation layer to present bizData and
// wait for subsequent request
// action/input/output module interfaces:
- act(type, src, param)
- inputValue(type, name)
- outputSet(type, name, value)
- outputCommit(type)
- outputRollback(type)
// logging support:
- print(args) // prints args to standard out
// log object: provides access to cocoon logging system via its methods:
- log
- error(message)
- debug(message)
- warn(message)
- info(message)See Also