You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by mr...@apache.org on 2005/02/21 20:02:21 UTC
svn commit: r154713 - in struts/flow/trunk/src: java/
java/org/apache/struts/flow/sugar/ test/
Author: mrdon
Date: Mon Feb 21 11:02:19 2005
New Revision: 154713
URL: http://svn.apache.org/viewcvs?view=rev&rev=154713
Log:
* Adding support for extension fields (.length for collections for example)
* Adding wrapping of return extension values
* Adding Collection extensions (each(), find(), findAll(), .length)
* Adding unit tests for new extensions
* Cleaning up struts helper functions by removing unneccessary ScriptableMap
wrapping
Added:
struts/flow/trunk/src/java/org/apache/struts/flow/sugar/CollectionExtensions.java (with props)
Modified:
struts/flow/trunk/src/java/org/apache/struts/flow/sugar/ExtensionFunction.java
struts/flow/trunk/src/java/org/apache/struts/flow/sugar/JavaObjectWrapper.java
struts/flow/trunk/src/java/org/apache/struts/flow/sugar/ListExtensions.java
struts/flow/trunk/src/java/org/apache/struts/flow/sugar/SugarWrapFactory.java
struts/flow/trunk/src/java/struts.js
struts/flow/trunk/src/test/listTest.js
Added: struts/flow/trunk/src/java/org/apache/struts/flow/sugar/CollectionExtensions.java
URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/java/org/apache/struts/flow/sugar/CollectionExtensions.java?view=auto&rev=154713
==============================================================================
--- struts/flow/trunk/src/java/org/apache/struts/flow/sugar/CollectionExtensions.java (added)
+++ struts/flow/trunk/src/java/org/apache/struts/flow/sugar/CollectionExtensions.java Mon Feb 21 11:02:19 2005
@@ -0,0 +1,83 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.struts.flow.sugar;
+
+import org.mozilla.javascript.*;
+
+import java.util.*;
+import java.io.Serializable;
+
+/**
+ * Adds various function extensions to java.util.List implementations.
+ */
+public class CollectionExtensions {
+
+ public static ExtensionFunction each(final Collection col) {
+ return new ExtensionFunction() {
+ public Object execute(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args) {
+ Function func = (Function)args[0];
+ Object[] param = new Object[1];
+
+ for (Iterator i = col.iterator(); i.hasNext(); ) {
+ param[0] = i.next();
+ func.call(cx, scope, thisObj, param);
+ }
+ return null;
+ }
+ };
+ }
+
+ public static ExtensionFunction find(final Collection col) {
+ return new ExtensionFunction() {
+ public Object execute(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args) {
+ Function func = (Function)args[0];
+ Object[] param = new Object[1];
+ Object match = null;
+ for (Iterator i = col.iterator(); i.hasNext(); ) {
+ param[0] = i.next();
+ match = func.call(cx, scope, thisObj, param);
+ if (match != null) {
+ return match;
+ }
+ }
+ return null;
+ }
+ };
+ }
+
+ public static ExtensionFunction findAll(final Collection col) {
+ return new ExtensionFunction() {
+ public Object execute(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args) {
+ Function func = (Function)args[0];
+ Object[] param = new Object[1];
+ ArrayList found = new ArrayList();
+ Object match = null;
+ for (Iterator i = col.iterator(); i.hasNext(); ) {
+ param[0] = i.next();
+ match = func.call(cx, scope, thisObj, param);
+ if (match != null) {
+ found.add(match);
+ }
+ }
+ return found;
+ }
+ };
+ }
+
+ public static int length(Collection col, Scriptable scope) {
+ return col.size();
+ }
+}
Propchange: struts/flow/trunk/src/java/org/apache/struts/flow/sugar/CollectionExtensions.java
------------------------------------------------------------------------------
svn:executable = *
Modified: struts/flow/trunk/src/java/org/apache/struts/flow/sugar/ExtensionFunction.java
URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/java/org/apache/struts/flow/sugar/ExtensionFunction.java?view=diff&r1=154712&r2=154713
==============================================================================
--- struts/flow/trunk/src/java/org/apache/struts/flow/sugar/ExtensionFunction.java (original)
+++ struts/flow/trunk/src/java/org/apache/struts/flow/sugar/ExtensionFunction.java Mon Feb 21 11:02:19 2005
@@ -30,7 +30,20 @@
public Object call(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args) {
try {
- return execute(cx, scope, thisObj, args);
+ Object o = execute(cx, scope, thisObj, args);
+ if (o instanceof Scriptable) {
+ return o;
+ } else if (o == target) {
+ return wrapper;
+ } else {
+ // Need to wrap the object before we return it.
+ scope = ScriptableObject.getTopLevelScope(scope);
+ Class type = Object.class;
+ if (o != null) {
+ type = o.getClass();
+ }
+ return cx.getWrapFactory().wrap(cx, scope, o, type);
+ }
} catch (Exception ex) {
throw Context.throwAsScriptRuntimeEx(ex);
}
Modified: struts/flow/trunk/src/java/org/apache/struts/flow/sugar/JavaObjectWrapper.java
URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/java/org/apache/struts/flow/sugar/JavaObjectWrapper.java?view=diff&r1=154712&r2=154713
==============================================================================
--- struts/flow/trunk/src/java/org/apache/struts/flow/sugar/JavaObjectWrapper.java (original)
+++ struts/flow/trunk/src/java/org/apache/struts/flow/sugar/JavaObjectWrapper.java Mon Feb 21 11:02:19 2005
@@ -42,10 +42,18 @@
Method func = (Method)functions.get(name);
if (func != null) {
try {
- ExtensionFunction f = (ExtensionFunction)func.invoke(null, new Object[]{javaObject});
- f.setTarget(javaObject);
- f.setWrapper(this);
- return f;
+ if (func.getParameterTypes().length == 2) {
+ Object val = func.invoke(null, new Object[]{javaObject, start});
+ Class type = func.getReturnType();
+ start = ScriptableObject.getTopLevelScope(start);
+ Context cx = Context.getCurrentContext();
+ return cx.getWrapFactory().wrap(cx, start, val, type);
+ } else {
+ ExtensionFunction f = (ExtensionFunction)func.invoke(null, new Object[]{javaObject});
+ f.setTarget(javaObject);
+ f.setWrapper(this);
+ return f;
+ }
} catch (Exception ex) {
throw new RuntimeException("Unable to create function "+name+" on "+javaObject);
}
Modified: struts/flow/trunk/src/java/org/apache/struts/flow/sugar/ListExtensions.java
URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/java/org/apache/struts/flow/sugar/ListExtensions.java?view=diff&r1=154712&r2=154713
==============================================================================
--- struts/flow/trunk/src/java/org/apache/struts/flow/sugar/ListExtensions.java (original)
+++ struts/flow/trunk/src/java/org/apache/struts/flow/sugar/ListExtensions.java Mon Feb 21 11:02:19 2005
@@ -103,4 +103,8 @@
}
};
}
+
+ public static int length(List list, Scriptable scope) {
+ return list.size();
+ }
}
Modified: struts/flow/trunk/src/java/org/apache/struts/flow/sugar/SugarWrapFactory.java
URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/java/org/apache/struts/flow/sugar/SugarWrapFactory.java?view=diff&r1=154712&r2=154713
==============================================================================
--- struts/flow/trunk/src/java/org/apache/struts/flow/sugar/SugarWrapFactory.java (original)
+++ struts/flow/trunk/src/java/org/apache/struts/flow/sugar/SugarWrapFactory.java Mon Feb 21 11:02:19 2005
@@ -35,6 +35,7 @@
super();
// Add default methods
+ addExtensionFunctions(CollectionExtensions.class);
addExtensionFunctions(ListExtensions.class);
addExtensionFunctions(FileExtensions.class);
}
@@ -46,7 +47,7 @@
public void addExtensionFunction(Class cls, String name, Method func) {
int modifier = func.getModifiers();
if (Modifier.isStatic(modifier) && Modifier.isPublic(modifier)) {
- ExtensionFunctionEntry entry = new ExtensionFunctionEntry(cls, name, func);
+ ExtensionEntry entry = new ExtensionEntry(cls, name, func);
functionRegistry.add(entry);
} else {
throw new IllegalArgumentException("Method "+func+" must be static and public");
@@ -60,7 +61,7 @@
if (Modifier.isStatic(modifier) && Modifier.isPublic(modifier)) {
String name = methods[x].getName();
Class target = methods[x].getParameterTypes()[0];
- ExtensionFunctionEntry entry = new ExtensionFunctionEntry(target, name, methods[x]);
+ ExtensionEntry entry = new ExtensionEntry(target, name, methods[x]);
functionRegistry.add(entry);
}
}
@@ -100,11 +101,11 @@
private Map getExtensionFunctions(Class cls) {
Map map = (Map)functionMappings.get(cls);
- ExtensionFunctionEntry entry;
+ ExtensionEntry entry;
if (map == null) {
map = new HashMap();
for (Iterator i = functionRegistry.iterator(); i.hasNext(); ) {
- entry = (ExtensionFunctionEntry)i.next();
+ entry = (ExtensionEntry)i.next();
if (entry.clazz.isAssignableFrom(cls)) {
map.put(entry.name, entry.function);
}
@@ -113,12 +114,12 @@
return map;
}
- class ExtensionFunctionEntry {
+ class ExtensionEntry {
public Class clazz;
public String name;
public Method function;
- public ExtensionFunctionEntry(Class cls, String name, Method func) {
+ public ExtensionEntry(Class cls, String name, Method func) {
this.clazz = cls;
this.name = name;
this.function = func;
Modified: struts/flow/trunk/src/java/struts.js
URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/java/struts.js?view=diff&r1=154712&r2=154713
==============================================================================
--- struts/flow/trunk/src/java/struts.js (original)
+++ struts/flow/trunk/src/java/struts.js Mon Feb 21 11:02:19 2005
@@ -2,23 +2,23 @@
importPackage(Packages.org.apache.struts.flow.core);
function getRequestParams() {
- return new ScriptableMap(context.chainContext.getParam());
+ return context.chainContext.getParam();
}
function getRequestParamValues() {
- return new ScriptableMap(context.chainContext.getParamValues());
+ return context.chainContext.getParamValues();
}
function getRequestScope() {
- return new ScriptableMap(context.chainContext.getRequestScope());
+ return context.chainContext.getRequestScope();
}
function getSessionScope() {
- return new ScriptableMap(context.chainContext.getSessionScope());
+ return context.chainContext.getSessionScope();
}
function getApplicationScope() {
- return new ScriptableMap(context.chainContext.getApplicationScope());
+ return context.chainContext.getApplicationScope();
}
function getForm() {
Modified: struts/flow/trunk/src/test/listTest.js
URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/test/listTest.js?view=diff&r1=154712&r2=154713
==============================================================================
--- struts/flow/trunk/src/test/listTest.js (original)
+++ struts/flow/trunk/src/test/listTest.js Mon Feb 21 11:02:19 2005
@@ -41,3 +41,18 @@
assertTrue("Didn't sort, bar should be first", list[0] == "bar");
}
+function testLength() {
+ assertTrue("Length should be 2", list.length == 2);
+}
+
+function testFind() {
+ result = list.find(function(val) { return (val == "bar" ? val : null);});
+ assertTrue("Didn't find bar", result == "bar");
+}
+
+function testFindAll() {
+ list.add("bar");
+ result = list.findAll(function(val) { return (val == "bar" ? val : null);});
+ assertTrue("Didn't find bars", result.size() == 2);
+}
+
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org