org.vmguys.vmtools.utils
Class XPath

java.lang.Object
  |
  +--org.vmguys.vmtools.utils.XPath

public class XPath
extends java.lang.Object

This class implements a subset of XPath, which allows an element in an XML document to be specified by giving a path to it. In this subset, all paths must be absolute (they must start at the root), and they must lead to a single element (the resulting nodeset must contain just one node).

In Xpath, the index numbers of children start at 1. However, all the indexing done in computer data structures starts at zero. To assist in this, all the methods in this class automatically adjust, so the calling application will always use origin-0 indexes.


Method Summary
static java.lang.String getChildXPath(org.jdom.Element parent, java.lang.Object target)
          This method generates an XPath that leads from the parent element to the target element.
static java.lang.Object getElement(org.jdom.Element root, java.lang.String xpath)
          This method follows the xpath from the root element and returns the element specified by the path.
static int getIndex(java.lang.String pathPart)
          Given a part of a path like "foobar[5]" or "self::node()[5]" return the index value.
static java.lang.String getNodeName(java.lang.String pathPart)
          Given a part of a path like "foobar[5]" return the part before the left bracket.
static org.jdom.Element getParentElement(org.jdom.Element root, java.lang.String xpath)
          Given an xpath to a node in a tree, returns the parent of that node.
static java.lang.String getParentXpath(java.lang.String xpath)
          Given an xpath to a node in a tree, returns an xpath to that node's parent.
static java.lang.String getXPath(org.jdom.Element root, org.jdom.Element parent, java.lang.Object target)
          This method generates an XPath that leads from the root element of the document to the target element.
static java.lang.String getXPath(org.jdom.Element root, java.lang.Object target)
          This method generates an XPath that leads from the root element of the document to the target element.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getXPath

public static java.lang.String getXPath(org.jdom.Element root,
                                        java.lang.Object target)
                                 throws org.jdom.JDOMException
This method generates an XPath that leads from the root element of the document to the target element.

getXPath

public static java.lang.String getXPath(org.jdom.Element root,
                                        org.jdom.Element parent,
                                        java.lang.Object target)
                                 throws org.jdom.JDOMException
This method generates an XPath that leads from the root element of the document to the target element. If the caller knows the target's parent, it can reduce the amount of work.

getChildXPath

public static java.lang.String getChildXPath(org.jdom.Element parent,
                                             java.lang.Object target)
                                      throws org.jdom.JDOMException
This method generates an XPath that leads from the parent element to the target element. This is a relative path, not an absolute path.

getElement

public static java.lang.Object getElement(org.jdom.Element root,
                                          java.lang.String xpath)
                                   throws org.jdom.JDOMException

This method follows the xpath from the root element and returns the element specified by the path. It throws an exception if there is an error. It returns null if the path does not lead to an element.

This subset implementation recognizes only three of the XPath operators: '/', '[]', and ::node. It is sufficient to select one node from the XML document based on its heritage and position, and no more.

The root parameter is assumed to be the root of the XML document, but this method has no way to verify that. It does check to be sure the path is absolute by verifying the first character is '/'.


getParentElement

public static org.jdom.Element getParentElement(org.jdom.Element root,
                                                java.lang.String xpath)
                                         throws org.jdom.JDOMException
Given an xpath to a node in a tree, returns the parent of that node. Returns null if the parent can't be determined. The parent should be an Element, because if it isn't it can't have children.

getParentXpath

public static java.lang.String getParentXpath(java.lang.String xpath)
Given an xpath to a node in a tree, returns an xpath to that node's parent. Returns null if the result can't be generated.

getNodeName

public static java.lang.String getNodeName(java.lang.String pathPart)
Given a part of a path like "foobar[5]" return the part before the left bracket.

getIndex

public static int getIndex(java.lang.String pathPart)
                    throws org.jdom.JDOMException

Given a part of a path like "foobar[5]" or "self::node()[5]" return the index value.

NOTE: The value will be converted from the origin-1 index used by Xpath to an origin-0 value.



Copyright © 2001 VM Systems, Inc. All Rights Reserved.