dmitri 2003/01/29 09:55:01
Modified: jxpath/src/java/org/apache/commons/jxpath
PackageFunctions.java
jxpath/src/test/org/apache/commons/jxpath/ri/compiler
ExtensionFunctionTest.java
jxpath/src/java/org/apache/commons/jxpath/ri/compiler
ExtensionFunction.java
jxpath/src/java/org/apache/commons/jxpath/ri
EvalContext.java
jxpath/src/java/org/apache/commons/jxpath/util
BasicTypeConverter.java
Log:
Fixed type conversion issues with extension functions
Revision Changes Path
1.8 +13 -9
jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/PackageFunctions.java
Index: PackageFunctions.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/PackageFunctions.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- PackageFunctions.java 11 Jan 2003 05:41:22 -0000 1.7
+++ PackageFunctions.java 29 Jan 2003 17:55:00 -0000 1.8
@@ -63,6 +63,8 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
+import java.util.*;
+import java.util.Collection;
import java.util.Collections;
import java.util.Set;
@@ -165,11 +167,13 @@
if (parameters.length >= 1) {
Object target = parameters[0];
if (target != null) {
- if (target instanceof ExpressionContext) {
- Pointer pointer =
- ((ExpressionContext) target).getContextNodePointer();
- if (pointer != null) {
- target = pointer.getValue();
+ if (target instanceof Collection) {
+ Iterator iter = ((Collection) target).iterator();
+ if (iter.hasNext()) {
+ target = iter.next();
+ if (target instanceof Pointer) {
+ target = ((Pointer) target).getValue();
+ }
}
else {
target = null;
1.5 +15 -6
jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/compiler/ExtensionFunctionTest.java
Index: ExtensionFunctionTest.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/compiler/ExtensionFunctionTest.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- ExtensionFunctionTest.java 20 Jan 2003 00:00:27 -0000 1.4
+++ ExtensionFunctionTest.java 29 Jan 2003 17:55:00 -0000 1.5
@@ -225,7 +225,7 @@
// Invoke a function implemented as a regular method
assertXPathValue(context, "string(test:getFoo($test))", "4");
-
+
// Note that the prefix is ignored anyway, we are just calling a method
assertXPathValue(context, "string(call:getFoo($test))", "4");
@@ -268,7 +268,10 @@
// Execute an extension function for each node while searching
// The function uses ExpressionContext to get to the current
// node.
- assertXPathValue(context, "//.[test:isMap()]/Key1", "Value 1");
+ assertXPathValue(
+ context,
+ "//.[test:isMap()]/Key1",
+ "Value 1");
// The function uses ExpressionContext to get to all
// nodes in the context that is passed to it.
@@ -276,6 +279,12 @@
context,
"count(//.[test:count(strings) = 3])",
new Double(7));
+
+ // Another example of the same
+ assertXPathValue(
+ context,
+ "test:count(//strings)",
+ new Integer(21));
// The function uses ExpressionContext to get to the current
// pointer and returns its path.
1.8 +16 -6
jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/compiler/ExtensionFunction.java
Index: ExtensionFunction.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/compiler/ExtensionFunction.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- ExtensionFunction.java 19 Jan 2003 23:59:24 -0000 1.7
+++ ExtensionFunction.java 29 Jan 2003 17:55:00 -0000 1.8
@@ -63,10 +63,12 @@
import org.apache.commons.jxpath.Function;
import org.apache.commons.jxpath.JXPathException;
+import org.apache.commons.jxpath.Pointer;
import org.apache.commons.jxpath.ri.QName;
import org.apache.commons.jxpath.ri.EvalContext;
import java.util.Arrays;
+import java.util.List;
/**
* Represents an element of the parse tree representing an extension function
@@ -122,8 +124,7 @@
if (args != null) {
parameters = new Object[args.length];
for (int i = 0; i < args.length; i++) {
- Object param = args[i].compute(context);
- parameters[i] = param;
+ parameters[i] = convert(args[i].compute(context));
}
}
Function function =
@@ -137,4 +138,13 @@
return function.invoke(context, parameters);
}
+
+ private Object convert(Object object) {
+ if (object instanceof EvalContext) {
+ return ((EvalContext) object).getPointerList();
+ }
+ return object;
+ }
+
+
}
1.21 +13 -14
jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/EvalContext.java
Index: EvalContext.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/EvalContext.java,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -r1.20 -r1.21
--- EvalContext.java 11 Jan 2003 05:41:22 -0000 1.20
+++ EvalContext.java 29 Jan 2003 17:55:00 -0000 1.21
@@ -69,6 +69,7 @@
import java.util.List;
import java.util.NoSuchElementException;
+import org.apache.commons.jxpath.*;
import org.apache.commons.jxpath.ExpressionContext;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.Pointer;
@@ -256,24 +257,22 @@
/**
* Returns the list of all Pointers in this context for all positions
- * of the parent contexts.
+ * of the parent contexts. If there was an ongoing iteration over
+ * this context, the method should not be called.
*/
public List getPointerList() {
- int pos = position;
- if (pos != 0) {
- reset();
+ if (position != 0) {
+ throw new JXPathException(
+ "Simultaneous operations: "
+ + "should not request pointer list while "
+ + "iterating over an EvalContext");
}
+
List list = new ArrayList();
while (nextSet()) {
while (nextNode()) {
list.add(getCurrentNodePointer());
}
- }
- if (pos != 0) {
- setPosition(pos);
- }
- else {
- reset();
}
return list;
}
1.4 +20 -52
jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/util/BasicTypeConverter.java
Index: BasicTypeConverter.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/util/BasicTypeConverter.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- BasicTypeConverter.java 11 Jan 2003 05:41:27 -0000 1.3
+++ BasicTypeConverter.java 29 Jan 2003 17:55:01 -0000 1.4
@@ -63,15 +63,8 @@
import java.lang.reflect.Array;
import java.lang.reflect.Modifier;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Set;
-import java.util.Vector;
+import java.util.*;
-import org.apache.commons.jxpath.ExpressionContext;
import org.apache.commons.jxpath.JXPathException;
import org.apache.commons.jxpath.Pointer;
@@ -141,17 +134,6 @@
return true;
}
}
- else if (object instanceof ExpressionContext) {
- if (Collection.class.isAssignableFrom(toType)) {
- return true;
- }
- Pointer pointer =
- ((ExpressionContext) object).getContextNodePointer();
- if (pointer != null) {
- Object value = pointer.getValue();
- return canConvert(value, toType);
- }
- }
else if (fromType.isArray()) {
// Collection -> array
if (toType.isArray()) {
@@ -201,6 +183,9 @@
return canConvert(value, toType);
}
}
+ else if (object instanceof Pointer) {
+ return canConvert(((Pointer) object).getValue(), toType);
+ }
return false;
}
/**
@@ -220,35 +205,6 @@
return object;
}
- if (object instanceof ExpressionContext) {
- if (Collection.class.isAssignableFrom(toType)) {
- List list = ((ExpressionContext) object).getContextNodeList();
- Collection result = new ArrayList();
- if (toType == List.class || toType == ArrayList.class) {
- result = new ArrayList();
- }
- else if (toType == Vector.class) {
- result = new Vector();
- }
- else if (toType == Set.class || toType == HashSet.class) {
- result = new HashSet();
- }
- int count = list.size();
- for (int i = 0; i < count; i++) {
- Pointer ptr = (Pointer) list.get(i);
- result.add(ptr.getValue());
- }
- return result;
- }
- else {
- Object value =
- ((ExpressionContext) object)
- .getContextNodePointer()
- .getValue();
- return convert(value, toType);
- }
- }
-
Class fromType = object.getClass();
if (fromType.equals(toType) || toType.isAssignableFrom(fromType)) {
return object;
@@ -338,7 +294,19 @@
}
return convert(value, toType);
}
+ else {
+ throw new RuntimeException(
+ "Cannot convert collection to "
+ + toType
+ + ", it contains "
+ + length
+ + " elements");
+ }
+ }
+ else if (object instanceof Pointer) {
+ return convert(((Pointer) object).getValue(), toType);
}
+
throw new RuntimeException(
"Cannot convert " + object.getClass() + " to " + toType);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]