Example usage for org.apache.commons.jxpath JXPathContext getNamespaceContextPointer

List of usage examples for org.apache.commons.jxpath JXPathContext getNamespaceContextPointer

Introduction

In this page you can find the example usage for org.apache.commons.jxpath JXPathContext getNamespaceContextPointer.

Prototype

public Pointer getNamespaceContextPointer() 

Source Link

Document

Returns the namespace context pointer set with #setNamespaceContextPointer(Pointer) setNamespaceContextPointer() or, if none has been specified, the context pointer otherwise.

Usage

From source file:org.commonjava.maven.galley.maven.model.view.JXPathContextAncestryTest.java

@Test
@Ignore// w  w w .j  a v  a2 s .c  om
public void basicJXPathTest() throws Exception {
    final InputStream is = Thread.currentThread().getContextClassLoader()
            .getResourceAsStream("jxpath/simple.pom.xml");

    final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);

    final JXPathContext ctx = JXPathContext.newContext(document);

    document.getDocumentElement().removeAttribute("xmlns");

    final String projectGroupIdPath = "ancestor::project/groupId";

    // NOT what's failing...just populating the node set to traverse in order to feed the ancestor:: axis test.
    final List<?> nodes = ctx.selectNodes("/project/dependencies/dependency");
    for (final Object object : nodes) {
        final Node node = (Node) object;
        dump(node);

        final Stack<Node> revPath = new Stack<Node>();

        Node parent = node;
        while (parent != null) {
            revPath.push(parent);
            parent = parent.getParentNode();
        }

        JXPathContext nodeCtx = null;
        while (!revPath.isEmpty()) {
            final Node part = revPath.pop();
            if (nodeCtx == null) {
                nodeCtx = JXPathContext.newContext(part);
            } else {
                nodeCtx = JXPathContext.newContext(nodeCtx, part);
            }
        }

        System.out
                .println("Path derived from context: '" + nodeCtx.getNamespaceContextPointer().asPath() + "'");

        // brute-force approach...try to force population of the parent pointers by painstakingly constructing contexts for all intermediate nodes.
        System.out.println("Selecting groupId for declaring project using path-derived context...");
        System.out.println(nodeCtx.getValue(projectGroupIdPath));

        // Naive approach...this has all the context info it needs to get parent contexts up to and including the document!
        System.out.println("Selecting groupId for declaring project using non-derived context...");
        System.out.println(JXPathContext.newContext(node).getValue(projectGroupIdPath));
    }
}