cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stefano Mazzocchi <stef...@apache.org>
Subject Re: JavaScript for Java programmers (who want to use the Cocoon flow layer)
Date Mon, 10 Feb 2003 10:53:57 GMT
Christopher Oliver wrote:
> 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 - http://www.mozilla.org/rhino) and Java.
> 
> I also encourage you to play around with the Cocoon Flow debugger 
> (http://xml.apache.org/cocoon/howto/howto-flow-debugger.html), as it 
> contains an interactive environment where you can type in JavaScript 
> code and see the results.
> 
> JavaScript contains the following built-in objects and top-level functions:
> 
> Built-in objects           Top-level functions
> 
> - Array                    - decodeURI()
> - Boolean                  - decodeURIComponent()
> - Date                     - encodeURI()
> - Error                    - eval()
> - Function                 - escape()
> - Math                     - isFinite()
> - Number                   - isNAN()
> - Object                   - parseFloat()
> - RegExp                   - parseInt()
> - String                   - unescape()
> 
> These objects and functions are comprehensively documented in many 
> places on the web so I won't describe them further here (e.g. 
> http://www.devguru.com/Technologies/ecmascript/quickref/javascript_intro.html). 
> 
> 
> 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:
> 
> 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()
>    }
> }
> 
> 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:
> 
> if ("propName" in obj) {
>     // do something
> }
> 
> The JavaScript "for" statement together with the "in" operator supports 
> a kind of "foreach": it iterates over the properties of an object:
> 
> for (i in obj) {
>     print(obj[i]);
> }
> 
> 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.
>         foo.bar
> 
> is the same as
> 
>       foo["bar"]
> 
> A new instance of an object can be created with the "new" operator:
> 
>    var x = new String();
> 
> Alternatively you can use the literal syntax to create instances of 
> built-in types:
> 
> Literal Syntax                              Equivalent using new
> ""                                          new String()
> ''                                          new String()
> []                                          new Array()
> {}                                          new Object()
> 
> 
> 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:
> 
>    var map = new java.util.HashMap();
> 
> Note that classes under "java.lang" are not automatically imported, 
> however:
> 
>    var n = new java.lang.Integer(3);
> 
> All other java packages and classes are accessible under the property 
> "Packages":
> 
>    var tree = new Packages.javax.swing.JTree();
> 
> 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);
> 
> 
> Rhino understands Java bean properties, so if your Java classes have 
> getters and setters you can access them as properties in JavaScript:
> 
>  var d = new java.util.Date();
>  d.year = 2003;    // same effect as d.setYear(2003);
> 
> The Rhino interpreter that supports continuations adds the following two 
> objects:
> 
> - Continuation
> - 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:
> 
> // 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)
> 
> For a higher level description of the Cocoon objects see: 
> http://www.webweavertech.com/ovidiu/weblog/archives/000042.html

Thanks Chris, you rock!

-- 
Stefano Mazzocchi                               <stefano@apache.org>
    Pluralitas non est ponenda sine necessitate [William of Ockham]
--------------------------------------------------------------------



---------------------------------------------------------------------
To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
For additional commands, email: cocoon-dev-help@xml.apache.org


Mime
View raw message