Example usage for org.w3c.dom Node getPreviousSibling

List of usage examples for org.w3c.dom Node getPreviousSibling

Introduction

In this page you can find the example usage for org.w3c.dom Node getPreviousSibling.

Prototype

public Node getPreviousSibling();

Source Link

Document

The node immediately preceding this node.

Usage

From source file:Main.java

/**
 * Gets the index of the specified element amongst elements with the same
 * name/*from  w ww . ja v a 2s  .  co m*/
 * 
 * @param element
 *           the element to get for
 * @return the index of the element, will be >= 1
 */

public static int getElementIndex(Node element) {
    int result = 1;

    Node elm = element.getPreviousSibling();
    while (elm != null) {
        if (elm.getNodeType() == Node.ELEMENT_NODE && elm.getNodeName().equals(element.getNodeName()))
            result++;
        elm = elm.getPreviousSibling();
    }

    return result;
}

From source file:Main.java

public static int getElementIndex(Node node) {
    String s = node.getNodeName();
    int i = 0;/*  www.java 2s. c o  m*/
    for (Node node1 = node.getPreviousSibling(); node1 != null; node1 = node1.getPreviousSibling())
        if (node1.getNodeName().equals(s) && node1.getNodeType() == 1)
            i++;

    return i;
}

From source file:Main.java

public static Element getLastChild(Element e) {
    if (e == null)
        return null;
    Node n = e.getLastChild();
    while (n != null && n.getNodeType() != Node.ELEMENT_NODE)
        n = n.getPreviousSibling();
    return (Element) n;
}

From source file:Main.java

public static Node getPreviousSiblingElement(Node node) {
    if (node == null)
        return null;
    Node prevSibling = node.getPreviousSibling();
    while ((prevSibling != null) && (prevSibling.getNodeType() != Node.ELEMENT_NODE))
        prevSibling = prevSibling.getPreviousSibling();
    if ((prevSibling != null) && (prevSibling.getNodeType() == Node.ELEMENT_NODE))
        return prevSibling;
    return null;//from   ww  w .  j a va2  s  .c o m
}

From source file:Main.java

public static Element getPrevSibling(Element e) {
    Node n = e.getPreviousSibling();
    while (n != null && n.getNodeType() != Node.ELEMENT_NODE)
        n = n.getPreviousSibling();
    return (Element) n;
}

From source file:Main.java

private final static String getPathElement(final Node n) {
    final String name = getLocalName(n);
    int i = 1;//  w  w  w . j  a  v  a 2  s  .com

    if (n instanceof Attr) {
        return "@" + name; // Still need to check namespaces; could have 2 with same local name
    }

    // Could use attributes/namespaces to distinguish between nodes with the same local name
    // instead of just an index
    Node sib = n;
    while ((sib = sib.getPreviousSibling()) != null) {
        if (name.equals(getLocalName(sib))) {
            i++;
        }
    }
    if (i > 1) {
        return name + "[" + i + "]";
    }

    sib = n;
    while ((sib = sib.getNextSibling()) != null) {
        if (name.equals(getLocalName(sib))) {
            return name + "[1]";
        }
    }

    return name;
}

From source file:Main.java

/**
 * Builds an XPath string referencing the given node relative to it's parent.
 * /*from www.  j  av  a  2  s.c  o m*/
 * @param node
 * @param context namespace context used to determine correct namespace prefixes, see
 *            {@link SignavioNamespaceContext}
 * @return a relative XPath string or null (if no node given)
 */
private static String getNodeString(Node node, NamespaceContext context) {
    if (node == null) {
        return null;
    }

    // get qualified name
    String nodeName = node.getLocalName();
    if (nodeName == null)
        nodeName = node.getNodeName();

    if (node.getNamespaceURI() != null) {
        String prefix = context.getPrefix(node.getNamespaceURI());
        nodeName = prefix + ":" + node.getLocalName();
    }

    if (node instanceof Attr) {
        return "@" + nodeName;
    } else if (node instanceof Text) {
        nodeName = "text()";
    }

    // determine position
    Node current = node;
    while (current.getPreviousSibling() != null) {
        current = current.getPreviousSibling();
    }
    int position = 1;

    while (current != node) {
        if (current.getNodeName().equals(node.getNodeName()))
            position++;
        current = current.getNextSibling();
    }

    return nodeName + "[" + position + "]";
}

From source file:Main.java

public static Node getPrevious(final Node current, final String name, final int type) {
    Node prev = current.getPreviousSibling();
    if (prev == null) {
        return null;
    }/*from  w  w  w . j a v  a  2 s  .  c  o m*/

    for (Node node = prev; node != null; node = node.getPreviousSibling()) {

        if (type >= 0 && node.getNodeType() != type) {
            continue;
        } else {
            if (name == null) {
                return node;
            }
            if (name.equals(node.getNodeName())) {
                return node;
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * Get the previous node in a DFS preorder traversal
 *
 * @param node the current node//from  www .  ja  v a2  s  . c  om
 * @return the previous node in the preorder traversal
 */
public static Node getPrevious(Node node) {
    Node previous;

    if (node == null)
        return null;

    if ((previous = node.getPreviousSibling()) != null) {
        for (; previous.getLastChild() != null; previous = previous.getLastChild()) {
        }

        return previous;
    }

    return node.getParentNode();
}

From source file:Main.java

/**
 * Returns the index of element in the list of all elements with the same name in its parent node.
 * If element's parent node is null, this function returns 0.
 *//*from  ww  w.  ja  v  a2  s.c om*/
public static int getElementIndex(Element element) {
    int index = 1;
    Node sibling = element;
    while ((sibling = sibling.getPreviousSibling()) != null) {
        if (sibling instanceof Element) {
            Element siblingElement = (Element) sibling;

            // check if element names and element namespaces match 
            if (element.getLocalName().equals(siblingElement.getLocalName())
                    && (element.getNamespaceURI() == null ? siblingElement.getNamespaceURI() == null
                            : element.getNamespaceURI().equals(siblingElement.getNamespaceURI()))) {
                ++index;
            }
        }
    }
    return index;
}