Java XPath Create getXPathFromVector(Vector path)

Here you can find the source of getXPathFromVector(Vector path)

Description

get X Path From Vector

License

Open Source License

Declaration

private static String getXPathFromVector(Vector path) 

Method Source Code

//package com.java2s;
import java.util.Vector;
import org.w3c.dom.*;

public class Main {
    private static String getXPathFromVector(Vector path) {
        StringBuffer strBuf = new StringBuffer();
        int length = path.size();

        for (int i = 0; i < length; i++) {
            Node tempNode = (Node) path.elementAt(i);
            short nodeType = getNodeType(tempNode);
            String targetValue = getValue(tempNode, nodeType);
            int position = 1;

            tempNode = getPreviousTypedNode(tempNode, nodeType);

            while (tempNode != null) {
                if (nodeType == Node.ELEMENT_NODE) {
                    if (getValue(tempNode, nodeType).equals(targetValue)) {
                        position++;//from   w  w w  . ja  va2  s .  co  m
                    }
                } else {
                    position++;
                }

                tempNode = getPreviousTypedNode(tempNode, nodeType);
            }

            boolean hasMatchingSiblings = (position > 1);

            if (!hasMatchingSiblings) {
                tempNode = (Node) path.elementAt(i);
                tempNode = getNextTypedNode(tempNode, nodeType);

                while (!hasMatchingSiblings && tempNode != null) {
                    if (nodeType == Node.ELEMENT_NODE) {
                        if (getValue(tempNode, nodeType)
                                .equals(targetValue)) {
                            hasMatchingSiblings = true;
                        } else {
                            tempNode = getNextTypedNode(tempNode, nodeType);
                        }
                    } else {
                        hasMatchingSiblings = true;
                    }
                }
            }

            String step;

            switch (nodeType) {
            case Node.TEXT_NODE:
                step = "text()";
                break;
            case Node.PROCESSING_INSTRUCTION_NODE:
                step = "processing-instruction()";
                break;
            default:
                step = targetValue;
                break;
            }

            if (step != null && step.length() > 0) {
                strBuf.append('/' + step);
            }

            if (hasMatchingSiblings) {
                strBuf.append("[" + position + "]");
            }
        }

        return strBuf.toString();
    }

    private static short getNodeType(Node node) {
        return (node != null ? node.getNodeType() : -1);
    }

    private static String getValue(Node node, short nodeType) {
        switch (nodeType) {
        case Node.ELEMENT_NODE:
            return ((Element) node).getTagName();

        case Node.TEXT_NODE:
            return ((Text) node).getData();

        case Node.PROCESSING_INSTRUCTION_NODE:
            return ((ProcessingInstruction) node).getData();

        default:
            return "";
        }
    }

    private static Node getPreviousTypedNode(Node node, short nodeType) {
        node = node.getPreviousSibling();

        while (node != null && node.getNodeType() != nodeType) {
            node = node.getPreviousSibling();
        }

        return node;
    }

    private static Node getNextTypedNode(Node node, short nodeType) {
        node = node.getNextSibling();

        while (node != null && node.getNodeType() != nodeType) {
            node = node.getNextSibling();
        }

        return node;
    }
}

Related

  1. getXPathForElement(Node e, NamespaceContext ctx)
  2. getXPathForNode(Node node)
  3. getXPathForNode(Node node)
  4. getXPathFromVector(Vector path)
  5. getXPathFromVector(Vector path)
  6. getXPathListForNode(Node n)
  7. getXPathNoCache(String exp)
  8. getXPathNodeIndex(Node node, boolean ignoreWhitespace)
  9. getXPathNodes(XPath xpath, Object inic, String name)