Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2008-05-04 19:17:40 -0400
committerpelder2008-05-04 19:17:40 -0400
commitd8e878910e6a537528be4f45ad30407a31f9305c (patch)
tree6767dc5bd99ddbd547ce2a59817a4fa30f1540ae
parent21a12ef56bfd30e6e7d2ad6dd6cf3883917516c9 (diff)
downloadorg.eclipse.jet-d8e878910e6a537528be4f45ad30407a31f9305c.tar.gz
org.eclipse.jet-d8e878910e6a537528be4f45ad30407a31f9305c.tar.xz
org.eclipse.jet-d8e878910e6a537528be4f45ad30407a31f9305c.zip
[175783] Unable to use certain xpath axes in jet tags
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/ast/Axis.java377
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/xpath/inspector/ExpandedName.java2
-rw-r--r--tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/AllTests.java47
-rw-r--r--tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/AncestorOrSelfTests.java86
-rw-r--r--tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/AncestorTests.java82
-rw-r--r--tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/DescendantTests.java60
-rw-r--r--tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/FollowingSiblingTests.java79
-rw-r--r--tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/FollowingTests.java102
-rw-r--r--tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/PrecedingSiblingTests.java76
-rw-r--r--tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/PrecedingTests.java116
10 files changed, 988 insertions, 39 deletions
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/ast/Axis.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/ast/Axis.java
index e9f4b4c..5010168 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/ast/Axis.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/ast/Axis.java
@@ -17,9 +17,13 @@
package org.eclipse.jet.internal.xpath.ast;
+import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
+import java.util.List;
import java.util.Map;
import org.eclipse.jet.internal.xpath.NodeSetImpl;
@@ -80,17 +84,34 @@ public abstract class Axis
public NodeSet evaluate(NodeTest nodeTest, Context context)
{
- Object obj = context.getContextNode();
+ NodeSet result = new NodeSetImpl(1);
+
+ addParent(context, result);
- INodeInspector inspector = context.getContextNodeInspector();
- Object parent = inspector.getParent(obj);
+ nodeTest.filter(result);
- NodeSet result = NodeSetImpl.EMPTY_SET;
- if (parent != null)
- {
- result = new NodeSetImpl(1);
- result.add(parent);
- }
+ return result;
+ }
+
+ public NodeKind principalNodeKind()
+ {
+ return NodeKind.ELEMENT;
+ }
+
+ public String getAxisName()
+ {
+ return "parent"; //$NON-NLS-1$
+ }
+ }
+
+ private static final class AncestorAxis extends Axis
+ {
+
+ public NodeSet evaluate(NodeTest nodeTest, Context context)
+ {
+ final NodeSet result = new NodeSetImpl();
+
+ addAncestors(context, result);
nodeTest.filter(result);
@@ -104,7 +125,34 @@ public abstract class Axis
public String getAxisName()
{
- return "parent"; //$NON-NLS-1$
+ return "ancestor"; //$NON-NLS-1$
+ }
+ }
+
+ private static final class AncestorOrSelfAxis extends Axis
+ {
+
+ public NodeSet evaluate(NodeTest nodeTest, Context context)
+ {
+ final NodeSet result = new NodeSetImpl();
+
+ result.add(context.getContextNode());
+
+ addAncestors(context, result);
+
+ nodeTest.filter(result);
+
+ return result;
+ }
+
+ public NodeKind principalNodeKind()
+ {
+ return NodeKind.ELEMENT;
+ }
+
+ public String getAxisName()
+ {
+ return "ancestor-of-self"; //$NON-NLS-1$
}
}
@@ -242,6 +290,32 @@ public abstract class Axis
}
+ private static final class DescendantAxis extends Axis
+ {
+
+ public NodeSet evaluate(NodeTest nodeTest, Context context)
+ {
+ final NodeSet result = new NodeSetImpl();
+
+ addAllDescendants(context, result);
+
+ nodeTest.filter(result);
+
+ return result;
+ }
+
+ public NodeKind principalNodeKind()
+ {
+ return NodeKind.ELEMENT;
+ }
+
+ public String getAxisName()
+ {
+ return "descendant"; //$NON-NLS-1$
+ }
+
+ }
+
private static final class DescendantOrSelfAxis extends Axis
{
@@ -249,33 +323,154 @@ public abstract class Axis
{
NodeSet result = new NodeSetImpl();
result.add(context.getContextNode());
+
+ addAllDescendants(context, result);
+
nodeTest.filter(result);
+ return result;
+ }
+
+ public NodeKind principalNodeKind()
+ {
+ return NodeKind.ELEMENT;
+ }
+
+ public String getAxisName()
+ {
+ return "descendant-or-self"; //$NON-NLS-1$
+ }
+
+ }
+
+ private static final class FollowingSiblingAxis extends Axis {
+
+ public NodeSet evaluate(NodeTest nodeTest, Context context)
+ {
+ final NodeSet result = new NodeSetImpl();
+ addFollowingSiblings(context, result);
- NodeSet children = childAxis().evaluate(NodeTest.allNodes(), context);
+ nodeTest.filter(result);
+ return result;
+ }
+
- int contextPos = 1;
- for (Iterator i = children.iterator(); i.hasNext(); contextPos += 1)
+ public String getAxisName()
+ {
+ return "following-sibling"; //$NON-NLS-1$
+ }
+
+ public NodeKind principalNodeKind()
+ {
+ return NodeKind.ELEMENT;
+ }
+
+ }
+
+ private static final class FollowingAxis extends Axis {
+
+ public NodeSet evaluate(NodeTest nodeTest, Context context)
+ {
+ final List ancestors = new ArrayList();
+ addAncestors(context, ancestors);
+ final NodeSet result = new NodeSetImpl();
+ for (Iterator i = ancestors.iterator(); i.hasNext();)
{
- Object object = (Object)i.next();
- Context subcontext = context.newSubContext(object, contextPos, children.size());
-
- result.addAll(evaluate(nodeTest, subcontext));
+ // XXX It's not this simple. For each node in the following
+ // siblings, need to add the sibling, and then all the
+ // descendants.
+// addFollowingSiblings(context, result);
+ final List siblings = new ArrayList();
+ addFollowingSiblings(context, siblings);
+ for (Iterator j = siblings.iterator(); j.hasNext();)
+ {
+ Object sibling = (Object)j.next();
+ result.add(sibling);
+ addAllDescendants(context.newSubContext(sibling, 1, 1), result);
+ }
+ context = context.newSubContext((Object)i.next(), 1, 1);
}
+ nodeTest.filter(result);
return result;
}
+
+ public String getAxisName()
+ {
+ return "following"; //$NON-NLS-1$
+ }
+
public NodeKind principalNodeKind()
{
return NodeKind.ELEMENT;
}
+
+ }
+
+ private static final class PrecedingSiblingAxis extends Axis {
+
+ public NodeSet evaluate(NodeTest nodeTest, Context context)
+ {
+ final NodeSet result = new NodeSetImpl();
+ addPrecedingSiblings(context, result);
+
+ nodeTest.filter(result);
+ return result;
+ }
+
public String getAxisName()
{
- return "descendant-or-self"; //$NON-NLS-1$
+ return "preceding-sibling"; //$NON-NLS-1$
}
+ public NodeKind principalNodeKind()
+ {
+ return NodeKind.ELEMENT;
+ }
+
+ }
+
+ private static final class PrecedingAxis extends Axis {
+
+ public NodeSet evaluate(NodeTest nodeTest, Context context)
+ {
+ final List ancestors = new ArrayList();
+ addAncestors(context, ancestors);
+ final NodeSet result = new NodeSetImpl();
+ for (Iterator i = ancestors.iterator(); i.hasNext();)
+ {
+ // XXX It's not this simple. For each node in the following
+ // siblings, need to add the sibling, and then all the
+ // descendants.
+// addFollowingSiblings(context, result);
+ final List siblings = new ArrayList();
+ addPrecedingSiblings(context, siblings);
+ for (Iterator j = siblings.iterator(); j.hasNext();)
+ {
+ Object sibling = (Object)j.next();
+ addAllDescendantsReversed(context.newSubContext(sibling, 1, 1), result);
+ result.add(sibling);
+ }
+ context = context.newSubContext((Object)i.next(), 1, 1);
+ }
+
+ nodeTest.filter(result);
+ return result;
+ }
+
+
+ public String getAxisName()
+ {
+ return "preceding"; //$NON-NLS-1$
+ }
+
+ public NodeKind principalNodeKind()
+ {
+ return NodeKind.ELEMENT;
+ }
+
}
private static final Axis childAxis = new ChildAxis();
@@ -286,7 +481,21 @@ public abstract class Axis
private static final Axis selfAxis = new SelfAxis();
+ private static final Axis descendantAxis = new DescendantAxis();
+
private static final Axis descendantOrSelfAxis = new DescendantOrSelfAxis();
+
+ private static final Axis ancestorAxis = new AncestorAxis();
+
+ private static final Axis ancestorOrSelfAxis = new AncestorOrSelfAxis();
+
+ private static final Axis followingSiblingAxis = new FollowingSiblingAxis();
+
+ private static final Axis followingAxis = new FollowingAxis();
+
+ private static final Axis precedingSiblingAxis = new PrecedingSiblingAxis();
+
+ private static final Axis precedingAxis = new PrecedingAxis();
private static final Map axisByNameMap = new HashMap(15);
static
@@ -296,6 +505,103 @@ public abstract class Axis
axisByNameMap.put(parentAxis.getAxisName(), parentAxis);
axisByNameMap.put(selfAxis.getAxisName(), selfAxis);
axisByNameMap.put(descendantOrSelfAxis.getAxisName(), descendantOrSelfAxis);
+ axisByNameMap.put(descendantAxis.getAxisName(), descendantAxis);
+ axisByNameMap.put(ancestorAxis.getAxisName(), ancestorAxis);
+ axisByNameMap.put(ancestorOrSelfAxis.getAxisName(), ancestorOrSelfAxis);
+ axisByNameMap.put(followingSiblingAxis.getAxisName(), followingSiblingAxis);
+ axisByNameMap.put(followingAxis.getAxisName(), followingAxis);
+ axisByNameMap.put(precedingSiblingAxis.getAxisName(), precedingSiblingAxis);
+ axisByNameMap.put(precedingAxis.getAxisName(), precedingAxis);
+
+ }
+
+ protected static void addAllDescendants(Context context, Collection result)
+ {
+ final NodeSet children = childAxis().evaluate(NodeTest.allNodes(), context);
+
+ int position = 1;
+ for (Iterator i = children.iterator(); i.hasNext();position++)
+ {
+ final Object object = (Object)i.next();
+ result.add(object);
+ final Context subContext = context.newSubContext(object, position, children.size());
+ addAllDescendants(subContext,result);
+
+ }
+ }
+
+ protected static void addAllDescendantsReversed(Context context, Collection result)
+ {
+ final List children = new ArrayList(childAxis().evaluate(NodeTest.allNodes(), context));
+ Collections.reverse(children);
+
+ int position = 1;
+ for (Iterator i = children.iterator(); i.hasNext();position++)
+ {
+ final Object object = (Object)i.next();
+ final Context subContext = context.newSubContext(object, position, children.size());
+ addAllDescendantsReversed(subContext,result);
+ result.add(object);
+
+ }
+ }
+
+ protected static void addParent(Context context, final Collection result)
+ {
+ Object obj = context.getContextNode();
+
+ INodeInspector inspector = context.getContextNodeInspector();
+ Object parent = inspector.getParent(obj);
+
+ if (parent != null)
+ {
+ result.add(parent);
+ }
+ return;
+ }
+
+ protected static void addAncestors(Context context, final Collection result)
+ {
+ Object obj = context.getContextNode();
+
+ INodeInspector inspector = context.getContextNodeInspector();
+
+ Object parent = inspector.getParent(obj);
+
+ if (parent != null)
+ {
+ result.add(parent);
+ addAncestors(context.newSubContext(parent, 1, 1), result);
+ }
+ return;
+ }
+
+ protected static void addFollowingSiblings(Context context, final Collection result)
+ {
+ final INodeInspector inspector = context.getContextNodeInspector();
+ final Object contextNode = context.getContextNode();
+ final NodeKind nodeKind = inspector.getNodeKind(contextNode);
+ if(nodeKind != NodeKind.ATTRIBUTE && nodeKind != NodeKind.NAMESPACE) {
+ final Object parent = inspector.getParent(contextNode);
+ final List children = Arrays.asList(inspector.getChildren(parent));
+ int index = children.indexOf(contextNode);
+ result.addAll(children.subList(index + 1, children.size()));
+ }
+ }
+
+ protected static void addPrecedingSiblings(Context context, final Collection result)
+ {
+ final INodeInspector inspector = context.getContextNodeInspector();
+ final Object contextNode = context.getContextNode();
+ final NodeKind nodeKind = inspector.getNodeKind(contextNode);
+ if(nodeKind != NodeKind.ATTRIBUTE && nodeKind != NodeKind.NAMESPACE) {
+ final Object parent = inspector.getParent(contextNode);
+ final List children = Arrays.asList(inspector.getChildren(parent));
+ int index = children.indexOf(contextNode);
+ final List precedingChildren = children.subList(0, index);
+ Collections.reverse(precedingChildren);
+ result.addAll(precedingChildren);
+ }
}
/**
@@ -327,6 +633,11 @@ public abstract class Axis
return parentAxis;
}
+ public static Axis descendant()
+ {
+ return descendantAxis;
+ }
+
public static Axis descendantOrSelf()
{
return descendantOrSelfAxis;
@@ -362,4 +673,34 @@ public abstract class Axis
return result;
}
+ public static Axis ancestor()
+ {
+ return ancestorAxis;
+ }
+
+ public static Axis ancestorOrSelf()
+ {
+ return ancestorOrSelfAxis;
+ }
+
+ public static Axis followingSibling()
+ {
+ return followingSiblingAxis;
+ }
+
+ public static Axis following()
+ {
+ return followingAxis;
+ }
+
+ public static Axis precedingSibling()
+ {
+ return precedingSiblingAxis;
+ }
+
+ public static Axis preceding()
+ {
+ return precedingAxis;
+ }
+
}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/xpath/inspector/ExpandedName.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/xpath/inspector/ExpandedName.java
index dade395..c35a341 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/xpath/inspector/ExpandedName.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/xpath/inspector/ExpandedName.java
@@ -29,7 +29,7 @@ public final class ExpandedName
/**
* Defines the expanded name that matches all other expanded names.
*/
- public static final Object ALL = new ExpandedName(STAR);
+ public static final ExpandedName ALL = new ExpandedName(STAR);
private final String namespaceURI;
diff --git a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/AllTests.java b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/AllTests.java
index 6d10fc7..f41f8d9 100644
--- a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/AllTests.java
+++ b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/AllTests.java
@@ -8,34 +8,41 @@ public class AllTests {
public static Test suite() {
TestSuite suite = new TestSuite("Test for org.eclipse.jet.tests.xpath");
//$JUnit-BEGIN$
- suite.addTestSuite(XPathParserTests.class);
- suite.addTestSuite(EStructuralFeatureSettingTests.class);
- suite.addTestSuite(VariableTests.class);
+ suite.addTestSuite(EMFResourceInspectorTests.class);
+ suite.addTestSuite(AncestorTests.class);
suite.addTestSuite(RelOpTests.class);
- suite.addTest(ResourceAttributeInspectorTests.suite());
- suite.addTestSuite(SelfStepTests.class);
- suite.addTestSuite(ParentStepTests.class);
- suite.addTestSuite(DescendantOrSelfTests.class);
suite.addTestSuite(LogicalOpTests.class);
- suite.addTestSuite(DOMInspectorTests.class);
- suite.addTestSuite(ExpandedNameTests.class);
- suite.addTestSuite(XPathOverDOMTests.class);
+ suite.addTestSuite(XPathTokenScannerTests.class);
suite.addTestSuite(BooleanFunctionTests.class);
- suite.addTestSuite(EMFResourceInspectorTests.class);
- suite.addTestSuite(NumberBinaryOpTests.class);
+ suite.addTestSuite(XPathOverDOMTests.class);
+ suite.addTest(ResourceAttributeInspectorTests.suite());
+ suite.addTestSuite(PrecedingTests.class);
+ suite.addTestSuite(FollowingSiblingTests.class);
+ suite.addTestSuite(XPathParserDefects.class);
+ suite.addTestSuite(EObjectInspectorTests.class);
suite.addTest(ResourceInspectorTests.suite());
- suite.addTestSuite(StringFunctionTests.class);
- suite.addTestSuite(BuiltinStringFunctionsTests.class);
+ suite.addTestSuite(XPathOverEcoreTests.class);
+ suite.addTestSuite(XPathInspectorManagerTests.class);
suite.addTestSuite(NumberFunctionTests.class);
+ suite.addTestSuite(BuiltinStringFunctionsTests.class);
+ suite.addTestSuite(EStructuralFeatureSettingTests.class);
suite.addTestSuite(XPathOverEmfXmlXsdTests.class);
- suite.addTestSuite(XPathParserDefects.class);
+ suite.addTestSuite(AncestorOrSelfTests.class);
+ suite.addTestSuite(NumberBinaryOpTests.class);
+ suite.addTestSuite(RootTests.class);
+ suite.addTestSuite(DOMInspectorTests.class);
+ suite.addTestSuite(DescendantTests.class);
+ suite.addTestSuite(SelfStepTests.class);
+ suite.addTestSuite(DescendantOrSelfTests.class);
+ suite.addTestSuite(FollowingTests.class);
+ suite.addTestSuite(StringFunctionTests.class);
+ suite.addTestSuite(ParentStepTests.class);
+ suite.addTestSuite(XPathParserTests.class);
suite.addTestSuite(ChildStepTests.class);
+ suite.addTestSuite(PrecedingSiblingTests.class);
+ suite.addTestSuite(VariableTests.class);
suite.addTestSuite(AttributeStepTests.class);
- suite.addTestSuite(XPathOverEcoreTests.class);
- suite.addTestSuite(EObjectInspectorTests.class);
- suite.addTestSuite(XPathInspectorManagerTests.class);
- suite.addTestSuite(XPathTokenScannerTests.class);
- suite.addTestSuite(RootTests.class);
+ suite.addTestSuite(ExpandedNameTests.class);
//$JUnit-END$
return suite;
}
diff --git a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/AncestorOrSelfTests.java b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/AncestorOrSelfTests.java
new file mode 100644
index 0000000..86f3786
--- /dev/null
+++ b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/AncestorOrSelfTests.java
@@ -0,0 +1,86 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2006 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM - Initial API and implementation
+ *
+ * </copyright>
+ *
+ * $Id: AncestorOrSelfTests.java,v 1.1 2008/05/04 23:17:40 pelder Exp $
+ */
+
+package org.eclipse.jet.tests.xpath;
+
+import java.util.Iterator;
+import java.util.Set;
+
+import org.eclipse.jet.internal.xpath.ast.Axis;
+import org.eclipse.jet.internal.xpath.ast.ContextNode;
+import org.eclipse.jet.internal.xpath.ast.NodeTest;
+import org.eclipse.jet.internal.xpath.ast.Step;
+import org.eclipse.jet.xpath.Context;
+
+/**
+ * Test the 'descendant-of-self' axis.
+ *
+ */
+public class AncestorOrSelfTests extends AbstractExprTest {
+
+ public void testEvalAsNodeSet_root() {
+ Step stepExpr = new Step(
+ new ContextNode(),
+ Axis.ancestorOrSelf(),
+ NodeTest.allNodes());
+
+ Context context = new Context(document, null);
+
+ Set resultSet = stepExpr.evalAsNodeSet(context);
+
+ assertNotNull(resultSet);
+ assertEquals(1, resultSet.size());
+ assertEquals(document, resultSet.iterator().next());
+ }
+
+ public void testEvalAsNodeSet_rootElement() {
+ Step stepExpr = new Step (
+ new ContextNode(),
+ Axis.ancestorOrSelf(),
+ NodeTest.allNodes());
+
+ Context context = new Context(rootElement, null);
+
+ Set resultSet = stepExpr.evalAsNodeSet(context);
+
+ assertNotNull(resultSet);
+ assertEquals(2, resultSet.size());
+ Iterator i = resultSet.iterator();
+ assertEquals(rootElement, i.next());
+ assertEquals(document, i.next());
+ }
+
+ public void testEvalAsNodeSet_bElement() {
+ Step stepExpr = new Step (
+ new ContextNode(),
+ Axis.ancestorOrSelf(),
+ NodeTest.allNodes());
+
+ Context context = new Context(this.elementB, null);
+
+ Set resultSet = stepExpr.evalAsNodeSet(context);
+
+ assertNotNull(resultSet);
+// assertEquals(4, resultSet.size());
+ Iterator i = resultSet.iterator();
+ assertEquals(elementB, i.next());
+ assertEquals(elementA, i.next());
+ assertEquals(rootElement, i.next());
+ assertEquals(document, i.next());
+ }
+
+}
diff --git a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/AncestorTests.java b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/AncestorTests.java
new file mode 100644
index 0000000..8d97eac
--- /dev/null
+++ b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/AncestorTests.java
@@ -0,0 +1,82 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2006 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM - Initial API and implementation
+ *
+ * </copyright>
+ *
+ * $Id: AncestorTests.java,v 1.1 2008/05/04 23:17:40 pelder Exp $
+ */
+
+package org.eclipse.jet.tests.xpath;
+
+import java.util.Iterator;
+import java.util.Set;
+
+import org.eclipse.jet.internal.xpath.ast.Axis;
+import org.eclipse.jet.internal.xpath.ast.ContextNode;
+import org.eclipse.jet.internal.xpath.ast.NodeTest;
+import org.eclipse.jet.internal.xpath.ast.Step;
+import org.eclipse.jet.xpath.Context;
+
+/**
+ * Test the 'descendant-of-self' axis.
+ *
+ */
+public class AncestorTests extends AbstractExprTest {
+
+ public void testEvalAsNodeSet_root() {
+ Step stepExpr = new Step(
+ new ContextNode(),
+ Axis.ancestor(),
+ NodeTest.allNodes());
+
+ Context context = new Context(document, null);
+
+ Set resultSet = stepExpr.evalAsNodeSet(context);
+
+ assertNotNull(resultSet);
+ assertEquals(0, resultSet.size());
+ }
+
+ public void testEvalAsNodeSet_rootElement() {
+ Step stepExpr = new Step (
+ new ContextNode(),
+ Axis.ancestor(),
+ NodeTest.allNodes());
+
+ Context context = new Context(rootElement, null);
+
+ Set resultSet = stepExpr.evalAsNodeSet(context);
+
+ assertNotNull(resultSet);
+ assertEquals(1, resultSet.size());
+ assertEquals(document, resultSet.iterator().next());
+ }
+
+ public void testEvalAsNodeSet_bElement() {
+ Step stepExpr = new Step (
+ new ContextNode(),
+ Axis.ancestor(),
+ NodeTest.allNodes());
+
+ Context context = new Context(this.elementB, null);
+
+ Set resultSet = stepExpr.evalAsNodeSet(context);
+
+ assertNotNull(resultSet);
+// assertEquals(3, resultSet.size());
+ Iterator i = resultSet.iterator();
+ assertEquals(elementA, i.next());
+ assertEquals(rootElement, i.next());
+ assertEquals(document, i.next());
+ }
+
+}
diff --git a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/DescendantTests.java b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/DescendantTests.java
new file mode 100644
index 0000000..32d29e7
--- /dev/null
+++ b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/DescendantTests.java
@@ -0,0 +1,60 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2006 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM - Initial API and implementation
+ *
+ * </copyright>
+ *
+ * $Id: DescendantTests.java,v 1.1 2008/05/04 23:17:40 pelder Exp $
+ */
+
+package org.eclipse.jet.tests.xpath;
+
+import java.util.Set;
+
+import org.eclipse.jet.internal.xpath.ast.Axis;
+import org.eclipse.jet.internal.xpath.ast.NodeTest;
+import org.eclipse.jet.internal.xpath.ast.Root;
+import org.eclipse.jet.internal.xpath.ast.Step;
+import org.eclipse.jet.xpath.Context;
+import org.eclipse.jet.xpath.inspector.ExpandedName;
+import org.eclipse.jet.xpath.inspector.INodeInspector.NodeKind;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+/**
+ * Test the 'descendant-of-self' axis.
+ *
+ */
+public class DescendantTests extends AbstractExprTest {
+
+ public void testEvalAsNodeSet_root_slash_slash_b() {
+ // /child::root/descendant::b
+ Step stepExpr = new Step(new Step(new Root(), Axis.childAxis(),
+ NodeTest.nameTest(NodeKind.ELEMENT, new ExpandedName("root"))),
+ Axis.descendant(), NodeTest.nameTest(NodeKind.ELEMENT,
+ new ExpandedName("b")));
+ assertEquals("<root>/child::root/descendant::b",stepExpr.toString());
+
+ Context context = new Context(document, null);
+
+ Set resultSet = stepExpr.evalAsNodeSet(context);
+
+ assertNotNull(resultSet);
+ assertEquals(2, resultSet.size());
+ Element elementA2 = (Element) rootElement.getElementsByTagName("a")
+ .item(1);
+ Node elementB2 = (Element) elementA2.getElementsByTagName("b").item(0);
+
+ assertTrue(resultSet.contains(elementB));
+ assertTrue(resultSet.contains(elementB2));
+
+ }
+}
diff --git a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/FollowingSiblingTests.java b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/FollowingSiblingTests.java
new file mode 100644
index 0000000..3aa0742
--- /dev/null
+++ b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/FollowingSiblingTests.java
@@ -0,0 +1,79 @@
+/**
+ *
+ */
+package org.eclipse.jet.tests.xpath;
+
+import java.util.Iterator;
+
+import org.eclipse.jet.internal.xpath.ast.Axis;
+import org.eclipse.jet.internal.xpath.ast.ContextNode;
+import org.eclipse.jet.internal.xpath.ast.Step;
+import org.eclipse.jet.internal.xpath.ast.NodeTest.NameTest;
+import org.eclipse.jet.xpath.Context;
+import org.eclipse.jet.xpath.NodeSet;
+import org.eclipse.jet.xpath.inspector.ExpandedName;
+import org.eclipse.jet.xpath.inspector.INodeInspector.NodeKind;
+import org.w3c.dom.Element;
+import org.w3c.dom.Text;
+
+/**
+ * Test following-sibling axes
+ *
+ */
+public class FollowingSiblingTests extends AbstractExprTest {
+
+ public void testEvalAsNodeSet_ElementA() {
+ final Step stepExpr = new Step(new ContextNode(),
+ Axis.followingSibling(),
+ NameTest.nameTest(NodeKind.ELEMENT, new ExpandedName("a")));
+
+ final NodeSet result = stepExpr.evalAsNodeSet(new Context(elementA, null));
+
+ assertNotNull(result);
+ assertEquals(1, result.size());
+ Iterator i = result.iterator();
+ assertTrue(i.next() instanceof Element);
+
+ }
+
+ public void testEvalAsNodeSet_ElementA_all() {
+ final Step stepExpr = new Step(new ContextNode(),
+ Axis.followingSibling(),
+ NameTest.allNodes());
+
+ final NodeSet result = stepExpr.evalAsNodeSet(new Context(elementA, null));
+
+ assertNotNull(result);
+ assertEquals(3, result.size());
+ Iterator i = result.iterator();
+ assertTrue(i.next() instanceof Text);
+ assertTrue(i.next() instanceof Element);
+ assertTrue(i.next() instanceof Text);
+
+ }
+
+ public void testEvalAsNodeSet_ElementB() {
+ final Step stepExpr = new Step(new ContextNode(),
+ Axis.followingSibling(),
+ NameTest.nameTest(NodeKind.ELEMENT, ExpandedName.ALL));
+
+ final NodeSet result = stepExpr.evalAsNodeSet(new Context(elementB, null));
+
+ assertNotNull(result);
+ assertEquals(0, result.size());
+
+ }
+ public void testEvalAsNodeSet_ElementB_all() {
+ final Step stepExpr = new Step(new ContextNode(),
+ Axis.followingSibling(),
+ NameTest.allNodes());
+
+ final NodeSet result = stepExpr.evalAsNodeSet(new Context(elementB, null));
+
+ assertNotNull(result);
+ assertEquals(1, result.size());
+ Iterator i = result.iterator();
+ assertTrue(i.next() instanceof Text);
+
+ }
+}
diff --git a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/FollowingTests.java b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/FollowingTests.java
new file mode 100644
index 0000000..74aee60
--- /dev/null
+++ b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/FollowingTests.java
@@ -0,0 +1,102 @@
+/**
+ *
+ */
+package org.eclipse.jet.tests.xpath;
+
+import java.util.Iterator;
+
+import org.eclipse.jet.internal.xpath.ast.Axis;
+import org.eclipse.jet.internal.xpath.ast.ContextNode;
+import org.eclipse.jet.internal.xpath.ast.Step;
+import org.eclipse.jet.internal.xpath.ast.NodeTest.NameTest;
+import org.eclipse.jet.xpath.Context;
+import org.eclipse.jet.xpath.NodeSet;
+import org.eclipse.jet.xpath.inspector.ExpandedName;
+import org.eclipse.jet.xpath.inspector.INodeInspector.NodeKind;
+import org.w3c.dom.Element;
+import org.w3c.dom.Text;
+
+/**
+ * Test following-sibling axes
+ *
+ */
+public class FollowingTests extends AbstractExprTest {
+
+ public void testEvalAsNodeSet_ElementA() {
+ final Step stepExpr = new Step(new ContextNode(),
+ Axis.following(),
+ NameTest.nameTest(NodeKind.ELEMENT, new ExpandedName("a")));
+
+ final NodeSet result = stepExpr.evalAsNodeSet(new Context(elementA, null));
+
+ assertNotNull(result);
+ assertEquals(1, result.size());
+ Iterator i = result.iterator();
+ Object next1 = i.next();
+ assertTrue(next1 instanceof Element);
+ assertEquals("a",((Element)next1).getNodeName());
+
+ }
+
+ public void testEvalAsNodeSet_ElementA_all() {
+ final Step stepExpr = new Step(new ContextNode(),
+ Axis.following(),
+ NameTest.allNodes());
+
+ final NodeSet result = stepExpr.evalAsNodeSet(new Context(elementA, null));
+
+ assertNotNull(result);
+ Iterator i = result.iterator();
+ assertTrue(i.next() instanceof Text); //1
+ Element a = (Element)i.next(); //2
+ assertEquals("a", a.getNodeName());
+ assertTrue(i.next() instanceof Text); //3
+ Element b = (Element) i.next(); //4
+ assertEquals("b", b.getNodeName());
+ assertTrue(i.next() instanceof Text); //5
+ assertTrue(i.next() instanceof Text); //6
+ assertTrue(i.next() instanceof Text); //7
+ assertEquals(7, result.size());
+
+ }
+
+ public void testEvalAsNodeSet_ElementB() {
+ final Step stepExpr = new Step(new ContextNode(),
+ Axis.following(),
+ NameTest.nameTest(NodeKind.ELEMENT, ExpandedName.ALL));
+
+ final NodeSet result = stepExpr.evalAsNodeSet(new Context(elementB, null));
+
+ assertNotNull(result);
+ Iterator i = result.iterator();
+ Element a = (Element) i.next();
+ assertEquals("a", a.getNodeName());
+ Element b = (Element) i.next();
+ assertEquals("b", b.getNodeName());
+ assertEquals(2, result.size());
+
+
+ }
+ public void testEvalAsNodeSet_ElementB_all() {
+ final Step stepExpr = new Step(new ContextNode(),
+ Axis.following(),
+ NameTest.allNodes());
+
+ final NodeSet result = stepExpr.evalAsNodeSet(new Context(elementB, null));
+
+ assertNotNull(result);
+ Iterator i = result.iterator();
+ assertTrue(i.next() instanceof Text); //1
+ assertTrue(i.next() instanceof Text); //2
+ Element a = (Element)i.next(); //3
+ assertEquals("a", a.getNodeName());
+ assertTrue(i.next() instanceof Text); //4
+ Element b = (Element) i.next(); //5
+ assertEquals("b", b.getNodeName());
+ assertTrue(i.next() instanceof Text); //6
+ assertTrue(i.next() instanceof Text); //7
+ assertTrue(i.next() instanceof Text); //8
+ assertEquals(8, result.size());
+
+ }
+}
diff --git a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/PrecedingSiblingTests.java b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/PrecedingSiblingTests.java
new file mode 100644
index 0000000..6422847
--- /dev/null
+++ b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/PrecedingSiblingTests.java
@@ -0,0 +1,76 @@
+/**
+ *
+ */
+package org.eclipse.jet.tests.xpath;
+
+import java.util.Iterator;
+
+import org.eclipse.jet.internal.xpath.ast.Axis;
+import org.eclipse.jet.internal.xpath.ast.ContextNode;
+import org.eclipse.jet.internal.xpath.ast.Step;
+import org.eclipse.jet.internal.xpath.ast.NodeTest.NameTest;
+import org.eclipse.jet.xpath.Context;
+import org.eclipse.jet.xpath.NodeSet;
+import org.eclipse.jet.xpath.inspector.ExpandedName;
+import org.eclipse.jet.xpath.inspector.INodeInspector.NodeKind;
+import org.w3c.dom.Comment;
+import org.w3c.dom.Text;
+
+/**
+ * Test following-sibling axes
+ *
+ */
+public class PrecedingSiblingTests extends AbstractExprTest {
+
+ public void testEvalAsNodeSet_ElementA() {
+ final Step stepExpr = new Step(new ContextNode(),
+ Axis.precedingSibling(),
+ NameTest.nameTest(NodeKind.ELEMENT, new ExpandedName("a")));
+
+ final NodeSet result = stepExpr.evalAsNodeSet(new Context(elementA, null));
+
+ assertNotNull(result);
+ assertEquals(0, result.size());
+ }
+
+ public void testEvalAsNodeSet_ElementA_all() {
+ final Step stepExpr = new Step(new ContextNode(),
+ Axis.precedingSibling(),
+ NameTest.allNodes());
+
+ final NodeSet result = stepExpr.evalAsNodeSet(new Context(elementA, null));
+
+ assertNotNull(result);
+ assertEquals(3, result.size());
+ Iterator i = result.iterator();
+ assertTrue(i.next() instanceof Text);
+ assertTrue(i.next() instanceof Comment);
+ assertTrue(i.next() instanceof Text);
+
+ }
+
+ public void testEvalAsNodeSet_ElementB() {
+ final Step stepExpr = new Step(new ContextNode(),
+ Axis.precedingSibling(),
+ NameTest.nameTest(NodeKind.ELEMENT, ExpandedName.ALL));
+
+ final NodeSet result = stepExpr.evalAsNodeSet(new Context(elementB, null));
+
+ assertNotNull(result);
+ assertEquals(0, result.size());
+
+ }
+ public void testEvalAsNodeSet_ElementB_all() {
+ final Step stepExpr = new Step(new ContextNode(),
+ Axis.precedingSibling(),
+ NameTest.allNodes());
+
+ final NodeSet result = stepExpr.evalAsNodeSet(new Context(elementB, null));
+
+ assertNotNull(result);
+ assertEquals(1, result.size());
+ Iterator i = result.iterator();
+ assertTrue(i.next() instanceof Text);
+
+ }
+}
diff --git a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/PrecedingTests.java b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/PrecedingTests.java
new file mode 100644
index 0000000..610761f
--- /dev/null
+++ b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/xpath/PrecedingTests.java
@@ -0,0 +1,116 @@
+/**
+ *
+ */
+package org.eclipse.jet.tests.xpath;
+
+import java.util.Iterator;
+
+import org.eclipse.jet.internal.xpath.ast.Axis;
+import org.eclipse.jet.internal.xpath.ast.ContextNode;
+import org.eclipse.jet.internal.xpath.ast.Step;
+import org.eclipse.jet.internal.xpath.ast.NodeTest.NameTest;
+import org.eclipse.jet.xpath.Context;
+import org.eclipse.jet.xpath.NodeSet;
+import org.eclipse.jet.xpath.inspector.ExpandedName;
+import org.eclipse.jet.xpath.inspector.INodeInspector.NodeKind;
+import org.w3c.dom.Comment;
+import org.w3c.dom.Element;
+import org.w3c.dom.ProcessingInstruction;
+import org.w3c.dom.Text;
+
+/**
+ * Test following-sibling axes
+ *
+ */
+public class PrecedingTests extends AbstractExprTest {
+
+ public void testEvalAsNodeSet_ElementA() {
+ final Step stepExpr = new Step(new ContextNode(),
+ Axis.preceding(),
+ NameTest.nameTest(NodeKind.ELEMENT, new ExpandedName("a")));
+
+ Element elementA2 = (Element) rootElement.getElementsByTagName("a").item(1);
+ final NodeSet result = stepExpr.evalAsNodeSet(new Context(elementA2, null));
+
+ assertNotNull(result);
+ assertEquals(1, result.size());
+ Iterator i = result.iterator();
+ Object next1 = i.next();
+ assertTrue(next1 instanceof Element);
+ assertEquals("a",((Element)next1).getNodeName());
+
+ }
+
+ public void testEvalAsNodeSet_ElementA_all() {
+ final Step stepExpr = new Step(new ContextNode(),
+ Axis.preceding(),
+ NameTest.allNodes());
+
+ Element elementA2 = (Element) rootElement.getElementsByTagName("a").item(1);
+ final NodeSet result = stepExpr.evalAsNodeSet(new Context(elementA2, null));
+
+ assertNotNull(result);
+ Iterator i = result.iterator();
+ assertTrue(i.next() instanceof Text); //1
+ assertTrue(i.next() instanceof Text); //2
+ assertTrue(i.next() instanceof Text); //3
+ Element b = (Element)i.next(); //4
+ assertEquals("b", b.getNodeName());
+ assertTrue(i.next() instanceof Text); //5
+ Element a = (Element)i.next(); //6
+ assertEquals("a", a.getNodeName());
+ assertTrue(i.next() instanceof Text); //7
+ assertTrue(i.next() instanceof Comment); //8
+ assertTrue(i.next() instanceof Text); //9
+ assertTrue(i.next() instanceof ProcessingInstruction); //10
+ assertEquals(10, result.size());
+
+ }
+
+ public void testEvalAsNodeSet_ElementB() {
+ final Step stepExpr = new Step(new ContextNode(),
+ Axis.preceding(),
+ NameTest.nameTest(NodeKind.ELEMENT, ExpandedName.ALL));
+
+ Element elementA2 = (Element) rootElement.getElementsByTagName("a").item(1);
+ Element elementB2 = (Element) elementA2.getElementsByTagName("b").item(0);
+ final NodeSet result = stepExpr.evalAsNodeSet(new Context(elementB2, null));
+
+ assertNotNull(result);
+ Iterator i = result.iterator();
+ Element a = (Element) i.next();
+ assertEquals("b", a.getNodeName());
+ Element b = (Element) i.next();
+ assertEquals("a", b.getNodeName());
+ assertEquals(2, result.size());
+
+
+ }
+ public void testEvalAsNodeSet_ElementB_all() {
+ final Step stepExpr = new Step(new ContextNode(),
+ Axis.preceding(),
+ NameTest.allNodes());
+
+ Element elementA2 = (Element) rootElement.getElementsByTagName("a").item(1);
+ Element elementB2 = (Element) elementA2.getElementsByTagName("b").item(0);
+ final NodeSet result = stepExpr.evalAsNodeSet(new Context(elementB2, null));
+
+ assertNotNull(result);
+ Iterator i = result.iterator();
+ assertTrue(i.next() instanceof Text); //1
+ assertTrue(i.next() instanceof Text); //2
+ assertTrue(i.next() instanceof Text); //3
+ assertTrue(i.next() instanceof Text); //4
+ Element b = (Element)i.next(); //5
+ assertEquals("b", b.getNodeName());
+ assertTrue(i.next() instanceof Text); //6
+ Element a = (Element)i.next(); //7
+ assertEquals("a", a.getNodeName());
+ assertTrue(i.next() instanceof Text); //8
+ assertTrue(i.next() instanceof Comment); //9
+ assertTrue(i.next() instanceof Text); //10
+ assertTrue(i.next() instanceof ProcessingInstruction); //11
+ assertEquals(11, result.size());
+
+ }
+}

Back to the top