cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Don Brown <donald.br...@gmail.com>
Subject Better collections support and JVM extensions for flow
Date Wed, 16 Feb 2005 07:26:35 GMT
One aspect that has frustrated me about working with Rhino is the lack
of Javascript's terse syntax when working with Java collections.  I
want to access a HashMap's properties with the dot or bracket
notations and especially want the ability to for..in over a Map or
List.

To this end, I took advantage of Rhino's Context.setWrapFactory() to
override its automatic wrapping of Java objects.  When a List or Map
is detected, it uses a special wrapper to better integrate the
collection interface with Javascript.  In the case of Map, Cocoon
already had ScriptableMap, but this approach has several advantages:
 * The wrapping is automatic
 * I modified it to allow method calls on the underlying Map implementation

In addition, I've always been interested in Groovy's extensions of the
Java core classes to provide useful shortcuts that usually involve
closures.  Unfortunately, Groovy isn't nearly as stable as Rhino and
lacks continuations support.  Since I was already writing special
wrappers for List and Map implementations, I added an "each()" method
to ScriptableList which takes a function that will be called for every
value in the list.  To my pleasant suprise, it worked out quite well
which opens the door to the many if not all the Groovy improvements:
http://groovy.codehaus.org/groovy-jdk.html

Anyways, I don't know if Cocoon has already considered "spicing up"
flow to cut down on code size and make flow more simple, but if not, I
thought I'd mention these efforts I'm adding to Struts Flow
(http://struts.sf.net/struts-flow in the process of moving to Apache
Struts as a subproject).  These efforts represent a couple of hours of
experimentation, but I think they hold a lot of promise.

To demonstrate the changes, here is the Javascript test script I was using:

 // Test map access
map = new java.util.HashMap();
map.foo = "bar";
map.put("jim", "sara");
for (x in map) {
    print("map "+x);
}

// Test overriding methods
print("map size:"+map.size());
map.size = 3;
print("map.size:"+map.size);

// Test explicit function access
print("map size with prefix:"+map.fn_size());

// Test list wrapper
list = new java.util.ArrayList(10);
list.add("this is 0");
list.add("this is 1");
print(list[0]);
for (x in list) {
  print("value:"+x);
} 

// Testing each closure on list
print("===== testing closure");
list.each(function(val) {print("testing again:"+val);});

// Print method for testing
function print(txt) {
 java.lang.System.out.println("print: "+txt);
} 

http://svn.apache.org/viewcvs.cgi/struts/flow/trunk/src/java/org/apache/struts/flow/core/ScriptableMap.java?rev=154003&view=log
http://svn.apache.org/viewcvs.cgi/struts/flow/trunk/src/java/org/apache/struts/flow/core/ScriptableList.java?rev=154003&view=log

Don

Mime
View raw message