FileDocCategorySizeDatePackage
XPathEngine.javaAPI DocGlassfish v2 API10111Tue May 22 16:54:32 BST 2007oracle.toplink.essentials.internal.ejb.cmp3.xml.parser

XPathEngine

public class XPathEngine extends Object
INTERNAL Utility class for finding XML nodes using XPath expressions.

Fields Summary
private static final String
ATTRIBUTE
private static final String
TEXT
private static final String
ALL_CHILDREN
private static final String
NAMESPACE_URI
private static XPathEngine
instance
Constructors Summary
private XPathEngine()


      
        super();
    
Methods Summary
public static oracle.toplink.essentials.internal.ejb.cmp3.xml.parser.XPathEnginegetInstance()
Return the XPathEngine singleton.

        if (instance == null) {
            instance = new XPathEngine();
        }
        return instance;
    
private org.w3c.dom.NodeListgetNodes(org.w3c.dom.Node contextNode, java.lang.String xPathFragment)

    	if (xPathFragment.startsWith(ATTRIBUTE)) {
            return selectAttributeNodes(contextNode, xPathFragment);
        } else if (TEXT.equals(xPathFragment)) {
            return selectTextNodes(contextNode);
        } else if (xPathFragment.equals(ALL_CHILDREN)) {
        	return selectChildElements(contextNode);
        }
        return selectElementNodes(contextNode, xPathFragment);

    
private org.w3c.dom.NodegetSingleNode(org.w3c.dom.Node contextNode, java.lang.String xPathFragment)

        if (xPathFragment.startsWith(ATTRIBUTE)) {
            return selectSingleAttribute(contextNode, xPathFragment);
        } else if (TEXT.equals(xPathFragment)) {
            return selectSingleText(contextNode);
        }
        return selectSingleElement(contextNode, xPathFragment);
    
private booleansameName(org.w3c.dom.Node node, java.lang.String name)

        return name.equals(node.getLocalName()) || name.equals(node.getNodeName());
    
private booleansameNamespaceURI(org.w3c.dom.Node node, java.lang.String namespaceURI)

        // HANDLE THE NULL CASE
        String nodeNamespaceURI = node.getNamespaceURI();
        if (nodeNamespaceURI == namespaceURI) {
            return true;
        }

        if ((nodeNamespaceURI == null) && namespaceURI.equals("")) {
            return true;
        }

        if ((namespaceURI == null) && nodeNamespaceURI.equals("")) {
            return true;
        }

        // HANDLE THE NON-NULL CASE
        return (null != nodeNamespaceURI) && nodeNamespaceURI.equals(namespaceURI);
    
private org.w3c.dom.NodeListselectAttributeNodes(org.w3c.dom.Node contextNode, java.lang.String xPathFragment)

        XMLNodeList xmlNodeList = new XMLNodeList();

        Node child = selectSingleAttribute(contextNode, xPathFragment);
        if (null != child) {
            xmlNodeList.add(child);
        }
        return xmlNodeList;
    
private org.w3c.dom.NodeListselectChildElements(org.w3c.dom.Node contextNode)

        XMLNodeList xmlNodeList = new XMLNodeList();
        Node child = contextNode.getFirstChild();

        while (child != null) {
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                xmlNodeList.add(child);
            }
            child = child.getNextSibling();
        }

        return xmlNodeList;
    
private org.w3c.dom.NodeListselectElementNodes(org.w3c.dom.Node contextNode, java.lang.String xPathFragment)

        XMLNodeList xmlNodeList = new XMLNodeList();
        Node child = contextNode.getFirstChild();

        while (null != child) {
            if ((child.getNodeType() == Node.ELEMENT_NODE) && sameName(child, xPathFragment) && sameNamespaceURI(child, NAMESPACE_URI)) {
                xmlNodeList.add(child);
            }

            child = child.getNextSibling();
        }

        return xmlNodeList;
    
public org.w3c.dom.NodeListselectNodes(org.w3c.dom.Node contextNode, java.lang.String[] xPathFragments)
Execute the XPath statement relative to the context node.

param
contextNode the node relative to which the XPath statement will be executed
param
xmlField the field containing the XPath statement to be executed
param
namespaceResolver used to resolve namespace prefixes to the corresponding namespace URI
return
a list of nodes matching the XPath statement
throws
XMLPlatformException

        if (contextNode == null) {
            return null;
        }

        return selectNodes(contextNode, xPathFragments, 0);
    
private org.w3c.dom.NodeListselectNodes(org.w3c.dom.Node contextNode, java.lang.String[] xPathFragments, int index)

    
        NodeList resultNodes = getNodes(contextNode, xPathFragments[index]);

        if (xPathFragments.length != index + 1) {
            Node resultNode;
            XMLNodeList result = new XMLNodeList();
            int numberOfResultNodes = resultNodes.getLength();
            for (int x = 0; x < numberOfResultNodes; x++) {
                resultNode = resultNodes.item(x);
                result.addAll(selectNodes(resultNode, xPathFragments, index + 1));
            }
            return result;
        }

        return resultNodes;
    
private org.w3c.dom.NodeselectSingleAttribute(org.w3c.dom.Node contextNode, java.lang.String xPathFragment)

        Element contextElement = (Element)contextNode;
        return contextElement.getAttributeNode(xPathFragment.substring(1));
    
private org.w3c.dom.NodeselectSingleElement(org.w3c.dom.Node contextNode, java.lang.String xPathFragment)

        Node child = contextNode.getFirstChild();
        while (null != child) {
            if ((child.getNodeType() == Node.ELEMENT_NODE) && sameName(child, xPathFragment) && sameNamespaceURI(child, NAMESPACE_URI)) {
                return child;
            }

            child = child.getNextSibling();
        }
        return null;
    
public org.w3c.dom.NodeselectSingleNode(org.w3c.dom.Node contextNode, java.lang.String[] xPathFragments)
Execute the XPath statement relative to the context node.

param
contextNode the node relative to which the XPath statement will be executed
param
xmlField the field containing the XPath statement to be executed
param
namespaceResolver used to resolve namespace prefixes to the corresponding namespace URI
return
the first node located matching the XPath statement
throws
XMLPlatformException

        if (contextNode == null) {
            return null;
        }

        return selectSingleNode(contextNode, xPathFragments, 0);
    
private org.w3c.dom.NodeselectSingleNode(org.w3c.dom.Node contextNode, java.lang.String[] xPathFragments, int index)

        Node resultNode = getSingleNode(contextNode, xPathFragments[index]);
        if ((resultNode == null) || (xPathFragments.length == (index + 1))) {
            return resultNode;
        }

        return selectSingleNode(resultNode, xPathFragments, index + 1);
    
private org.w3c.dom.NodeselectSingleText(org.w3c.dom.Node contextNode)

        NodeList childrenNodes = contextNode.getChildNodes();

        if (childrenNodes.getLength() == 0) {
            return null;
        }

        if (childrenNodes.getLength() == 1) {
            Node child = childrenNodes.item(0);
            if (child.getNodeType() == Node.TEXT_NODE) {
                return child;
            }
            return null;
        }

        String returnVal = null;
        for (int i = 0; i < childrenNodes.getLength(); i++) {
            Node next = childrenNodes.item(i);
            if (next.getNodeType() == Node.TEXT_NODE) {
                String val = ((Text)next).getNodeValue();
                if (val != null) {
                    if (returnVal == null) {
                        returnVal = new String();
                    }
                    returnVal += val;
                }
            }
        }

        //bug#4515249 a new text node was being created when null should have been returned
        //case where contextNode had several children but no Text children
        if (returnVal != null) {
            return contextNode.getOwnerDocument().createTextNode(returnVal);
        }
        return null;
    
private org.w3c.dom.NodeListselectTextNodes(org.w3c.dom.Node contextNode)

        Node n = selectSingleText(contextNode);

        XMLNodeList xmlNodeList = new XMLNodeList();
        if (n != null) {
            xmlNodeList.add(n);
        }
        return xmlNodeList;