FileDocCategorySizeDatePackage
XPathDisplayNode.javaAPI DocExample5991Sat Sep 01 16:06:44 BST 2001javaxml2

XPathDisplayNode

public class XPathDisplayNode extends Object
XPathDisplayNode is a wrapper for {@link JDOMNode}s to display the XPath path to the decorated node.

Fields Summary
JDOMNode
node
The JDOMNode this xpath is based on
Constructors Summary
public XPathDisplayNode(JDOMNode node)

Constructor to wrap a node.

param
node {@link JDOMNode} to wrap.

        this.node = node;
    
Methods Summary
private java.lang.StringgetElementXPath(JDOMNode currentNode)

This creates an XPath expression representing this element node relative to the supplied currentNode.

param
currentNode JDOMNode representing "start" location.
return
String - the XPath expression

        StringBuffer buf = new StringBuffer("/")
            .append(currentNode.getQName());
        Element current = (Element)currentNode.getNode();
        Element parent = current.getParent();

        // See if we're at the root element
        if (parent == null ) {
            return buf.toString();
        }

        // Check for other siblings of the same name and namespace
        Namespace ns = current.getNamespace();
        List siblings = parent.getChildren(current.getName(), ns);
		
        int total = 0;
        Iterator i = siblings.iterator();
        while (i.hasNext()) {
            total++;
            if (current == i.next()) {
                break;
            }
        }

        // No selector needed if this is the only element
        if ((total == 1) && (!i.hasNext())) {
            return buf.toString();
        }

        return buf.append("[")
                  .append(String.valueOf(total))
                  .append("]").toString();
    
public java.lang.StringgetXPath()

This returns the complete XPath expression for this node.

return
String - complete XPath.

        // Handle elements
        if (node.getNode() instanceof Element) {
            JDOMNode parent = node.getParentNode();

            // If this is null, we're at the root
            if (parent == null) {
                return "/" + node.getQName();
            }

            // Otherwise, build a path back to the root
            Stack stack = new Stack();
            stack.add(node);
            do {
                stack.add(parent);
                parent = parent.getParentNode();
            } while (parent != null);

            // Build the path
            StringBuffer xpath = new StringBuffer();
            while (!stack.isEmpty()) {
                xpath.append(getElementXPath((JDOMNode)stack.pop()));
            }
            return xpath.toString();
        }	
		
        // Handle attributes
        if (node.getNode() instanceof Attribute) {
            Attribute attribute = (Attribute)node.getNode();
            JDOMNode parent = node.getParentNode();
            StringBuffer xpath = new StringBuffer("//")
                .append(parent.getQName())
                .append("[@")
                .append(node.getQName())
                .append("='")
                .append(attribute.getValue())
                .append("']");

            return xpath.toString();
        }

        // Handle text
        if (node.getNode() instanceof String) {
            StringBuffer xpath = new StringBuffer(
                new XPathDisplayNode(node.getParentNode()).getXPath())
                    .append("[child::text()]");
            return xpath.toString();
        }
					
        // Other node types could follow here
        return "Node type not supported yet.";