Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2008-04-20 22:57:02 -0400
committerpelder2008-04-20 22:57:02 -0400
commite770f7b671da5d4a5f70cd42772a6cc3e29cad28 (patch)
treecd5abfb5a3a9ceaa12044027fa578ec9a25321ca
parent46e56b28e4696a9728cd3b4a3b836f57721a8af8 (diff)
downloadorg.eclipse.jet-e770f7b671da5d4a5f70cd42772a6cc3e29cad28.tar.gz
org.eclipse.jet-e770f7b671da5d4a5f70cd42772a6cc3e29cad28.tar.xz
org.eclipse.jet-e770f7b671da5d4a5f70cd42772a6cc3e29cad28.zip
[203970] XPath engine should recognize Java collections and arrays as XPath node sets when handling function results
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/ast/Function.java11
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/ast/NodeSetCast.java9
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/xpath/XPathUtil.java153
3 files changed, 171 insertions, 2 deletions
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/ast/Function.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/ast/Function.java
index 802d5f7..5c5cbe9 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/ast/Function.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/ast/Function.java
@@ -18,12 +18,15 @@ package org.eclipse.jet.internal.xpath.ast;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.eclipse.jet.xpath.Context;
+import org.eclipse.jet.xpath.NodeSet;
import org.eclipse.jet.xpath.XPathFunction;
import org.eclipse.jet.xpath.XPathFunctionWithContext;
+import org.eclipse.jet.xpath.XPathUtil;
/**
@@ -73,7 +76,13 @@ public class Function extends ExprNode
try
{
final Object result = function.evaluate(functionArgValues);
- return result;
+ if(result != null && result.getClass().isArray()) {
+ return XPathUtil.asNodeSet((Object[])result);
+ } else if(result instanceof Collection && !(result instanceof NodeSet)) {
+ return XPathUtil.asNodeSet((Collection)result);
+ } else {
+ return result;
+ }
}
finally
{
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/ast/NodeSetCast.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/ast/NodeSetCast.java
index 9240f7f..8e40c40 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/ast/NodeSetCast.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/ast/NodeSetCast.java
@@ -17,9 +17,12 @@
package org.eclipse.jet.internal.xpath.ast;
+import java.util.Collection;
+
import org.eclipse.jet.internal.xpath.NodeSetImpl;
import org.eclipse.jet.xpath.Context;
import org.eclipse.jet.xpath.NodeSet;
+import org.eclipse.jet.xpath.XPathUtil;
import org.eclipse.jet.xpath.inspector.INodeInspector;
import org.eclipse.jet.xpath.inspector.InspectorManager;
@@ -55,6 +58,12 @@ public class NodeSetCast extends NodeSetExpr
{
return (NodeSet)leftVal;
}
+ else if(leftVal != null && leftVal.getClass().isArray()) {
+ return XPathUtil.asNodeSet((Object[])leftVal);
+ }
+ else if(leftVal instanceof Collection) {
+ return XPathUtil.asNodeSet((Collection)leftVal);
+ }
else
{
INodeInspector inspector = InspectorManager.getInstance().getInspector(leftVal);
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/xpath/XPathUtil.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/xpath/XPathUtil.java
index c77137d..1937d8b 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/xpath/XPathUtil.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/xpath/XPathUtil.java
@@ -16,6 +16,10 @@
*/
package org.eclipse.jet.xpath;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Iterator;
+
import org.eclipse.jet.internal.xpath.functions.BooleanFunction;
import org.eclipse.jet.internal.xpath.functions.NumberFunction;
import org.eclipse.jet.internal.xpath.functions.StringFunction;
@@ -23,10 +27,139 @@ import org.eclipse.jet.internal.xpath.functions.StringFunction;
/**
* Utility class for common XPath operations.
*/
-public class XPathUtil
+public final class XPathUtil
{
/**
+ * Adapter for Arrays and Collections into XPath NodeSets
+ */
+ protected static class NodeSetAdapter implements NodeSet
+ {
+
+ private final Collection collection;
+
+ public NodeSetAdapter(Object[] objectArray)
+ {
+ this(Arrays.asList(objectArray));
+ }
+
+ public NodeSetAdapter(Collection collection)
+ {
+ this.collection = collection;
+ }
+
+ /* (non-Javadoc)
+ * @see java.util.Set#add(java.lang.Object)
+ */
+ public boolean add(Object o)
+ {
+ return collection.add(o);
+ }
+
+ /* (non-Javadoc)
+ * @see java.util.Set#addAll(java.util.Collection)
+ */
+ public boolean addAll(Collection c)
+ {
+ return collection.addAll(c);
+ }
+
+ /* (non-Javadoc)
+ * @see java.util.Set#clear()
+ */
+ public void clear()
+ {
+ collection.clear();
+ }
+
+ /* (non-Javadoc)
+ * @see java.util.Set#contains(java.lang.Object)
+ */
+ public boolean contains(Object o)
+ {
+ return collection.contains(o);
+ }
+
+ /* (non-Javadoc)
+ * @see java.util.Set#containsAll(java.util.Collection)
+ */
+ public boolean containsAll(Collection c)
+ {
+ return collection.containsAll(c);
+ }
+
+ /* (non-Javadoc)
+ * @see java.util.Set#isEmpty()
+ */
+ public boolean isEmpty()
+ {
+ return collection.isEmpty();
+ }
+
+ /* (non-Javadoc)
+ * @see java.util.Set#iterator()
+ */
+ public Iterator iterator()
+ {
+ return collection.iterator();
+ }
+
+ /* (non-Javadoc)
+ * @see java.util.Set#remove(java.lang.Object)
+ */
+ public boolean remove(Object o)
+ {
+ return collection.remove(o);
+ }
+
+ /* (non-Javadoc)
+ * @see java.util.Set#removeAll(java.util.Collection)
+ */
+ public boolean removeAll(Collection c)
+ {
+ return collection.removeAll(c);
+ }
+
+ /* (non-Javadoc)
+ * @see java.util.Set#retainAll(java.util.Collection)
+ */
+ public boolean retainAll(Collection c)
+ {
+ return collection.retainAll(c);
+ }
+
+ /* (non-Javadoc)
+ * @see java.util.Set#size()
+ */
+ public int size()
+ {
+ return collection.size();
+ }
+
+ /* (non-Javadoc)
+ * @see java.util.Set#toArray()
+ */
+ public Object[] toArray()
+ {
+ return collection.toArray();
+ }
+
+ /* (non-Javadoc)
+ * @see java.util.Set#toArray(java.lang.Object[])
+ */
+ public Object[] toArray(Object[] a)
+ {
+ return collection.toArray(a);
+ }
+
+ }
+
+ private XPathUtil()
+ {
+ // prevent instantiation
+ }
+
+ /**
* Invoke the <a href="http://www.w3.org/TR/xpath#function-string">XPath 'string' function</a> on the argument.
* @param object the argument to convert to a string. Cannot be <code>null</code>
* @return the result of the XPath 'string' function.
@@ -70,4 +203,22 @@ public class XPathUtil
}
return NumberFunction.evaluate(object);
}
+
+ /**
+ * Return a {@link NodeSet} wrapper for the given array.
+ * @param objectArray an array of objects.
+ * @return a node set
+ */
+ public static NodeSet asNodeSet(Object[] objectArray) {
+ return new NodeSetAdapter(objectArray);
+ }
+
+ /**
+ * Return a {@link NodeSet} wrapper for the given collection
+ * @param collection a collection
+ * @return a node set
+ */
+ public static NodeSet asNodeSet(Collection collection) {
+ return new NodeSetAdapter(collection);
+ }
}

Back to the top