List of usage examples for org.w3c.dom Node getPreviousSibling
public Node getPreviousSibling();
From source file:edu.indiana.lib.twinpeaks.util.DomUtils.java
/** * Search earlier siblings for a given node * @param currentNode Starting point for our search * @param tagName Node name to look up// ww w . jav a 2 s .c o m * @return matching Node (null if none) */ public static Node getPreviousSiblingByName(Node currentNode, String tagName) { Node node = currentNode.getPreviousSibling(); while ((node != null) && (!node.getNodeName().equals(tagName))) { node = node.getPreviousSibling(); } return node; }
From source file:edu.virginia.speclab.juxta.author.model.JuxtaXMLParser.java
private static String nodeToSimpleXPath(Node node, Node root) { // recursion ahoy if (node == null) return ""; // need to get my index by looping through previous siblings for nodes with the same name as me Node sibling = node.getPreviousSibling(); int index = 1; while (sibling != null) { if (sibling.getNodeType() == node.getNodeType() && sibling.getNodeName().equals(node.getNodeName())) index++;/*from www .j av a2s. c o m*/ sibling = sibling.getPreviousSibling(); } String path = "/" + node.getNodeName(); if (!node.isSameNode(root)) path = path + "[" + index + "]"; else return path; // If we've picked anything other than an element node, ignore it, and walk up // the tree for other things. We get this when someone asks for /blah/blah/text() or something, // which we don't support. if (node.getNodeType() != Node.ELEMENT_NODE) path = ""; return (nodeToSimpleXPath(node.getParentNode(), root) + path); }
From source file:de.betterform.xml.dom.DOMUtil.java
/** * Returns the previous sibling element of the specified node. * <p/>/* w w w.j av a 2s . c om*/ * If there is no such element, this method returns <code>null</code>. * * @param node the node to process. * @return the previous sibling element of the specified node. */ public static Element getPreviousSiblingElement(Node node) { Node sibling = node.getPreviousSibling(); if ((sibling == null) || (sibling.getNodeType() == Node.ELEMENT_NODE)) { return (Element) sibling; } return getPreviousSiblingElement(sibling); }
From source file:org.alfresco.web.config.ConfigRuntime.java
/** * @param parent//from w w w . j a v a 2 s .c om * @param child */ protected void removeChild(Node child) { Node previousNode = child.getPreviousSibling(); if (previousNode != null && previousNode instanceof org.w3c.dom.Text) { previousNode.getParentNode().removeChild(previousNode); } child.getParentNode().removeChild(child); }
From source file:org.alfresco.web.config.ConfigRuntime.java
/** * @param parent//ww w .j a v a 2 s.c om * @param child */ protected void appendChild(Node parent, Node child) { parent.appendChild(child); Node previousNode = child.getPreviousSibling(); if (previousNode != null && previousNode instanceof org.w3c.dom.Text) { previousNode.getParentNode().removeChild(previousNode); } }
From source file:de.betterform.xml.dom.DOMUtil.java
/** * returns an element's position in the list of its siblings. * * @param refNode the element to get the index for * @return the position starting with 1, or -1 if refNode was null *//*from w w w . j a v a 2s .c om*/ public static int getCurrentPosition(Node refNode) { if (refNode == null) { return -1; } int counter = 0; Node current = refNode; while (current != null) { if (current.getNodeType() == Node.ELEMENT_NODE) { counter++; } current = current.getPreviousSibling(); } return counter; }
From source file:de.betterform.xml.dom.DOMUtil.java
public static int getCurrentNodesetPosition(Node refNode) { if (refNode == null) { return -1; }/* ww w. ja v a 2 s. c o m*/ int counter = 0; Node current = refNode; String nodeName = refNode.getNodeName(); while (current != null) { if (current.getNodeType() == Node.ELEMENT_NODE && nodeName.equals(current.getNodeName())) { counter++; } current = current.getPreviousSibling(); } return counter; }
From source file:com.dinochiesa.edgecallouts.EditXmlNode.java
private void remove(NodeList nodes) { Node currentNode = nodes.item(0); // delete adjacent empty text node if it is empty/whitespace Node prevSibling = currentNode.getPreviousSibling(); if (prevSibling != null && prevSibling.getNodeType() == Node.TEXT_NODE && prevSibling.getNodeValue().trim().isEmpty()) { currentNode.getParentNode().removeChild(prevSibling); }//from w w w. j ava 2s . c om currentNode.getParentNode().removeChild(currentNode); }
From source file:importer.handler.post.stages.Discriminator.java
/** * Is the given element the first sibling of its type in sequence? * @param elem the sibling to test/* w w w . j a v a 2 s. c o m*/ * @param s the SPlitter object * @return true if it is NOT preceded by another sibling of its type */ boolean isFirstAdjacentSibling(Element elem, Splitter s) { Node start = elem; String sName = start.getNodeName(); String sibName = getSibling(sName); while (start != null) { Node prev = start.getPreviousSibling(); if (prev != null) { if (prev.getNodeType() == Node.ELEMENT_NODE) { String pName = prev.getNodeName(); if ((pName.equals(sName) || pName.equals(sibName))) return false; } else if (prev.getNodeType() != Node.TEXT_NODE || !isWhitespace(prev.getTextContent())) return true; } start = prev; } return true; }
From source file:Main.java
public static String getXPath(Node node) { if (null == node) return null; // declarations Node parent = null;/*from www .j a v a2 s. c om*/ Stack<Node> hierarchy = new Stack<Node>(); StringBuilder buffer = new StringBuilder(); // push element on stack hierarchy.push(node); parent = node.getParentNode(); while (null != parent && parent.getNodeType() != Node.DOCUMENT_NODE) { // push on stack hierarchy.push(parent); // get parent of parent parent = parent.getParentNode(); } // construct xpath Object obj = null; while (!hierarchy.isEmpty() && null != (obj = hierarchy.pop())) { Node n = (Node) obj; boolean handled = false; // only consider elements if (n.getNodeType() == Node.ELEMENT_NODE) { Element e = (Element) n; // is this the root element? if (buffer.length() == 0) { // root element - simply append element name buffer.append(n.getNodeName()); } else { // child element - append slash and element name buffer.append("/"); buffer.append(n.getNodeName()); if (n.hasAttributes()) { // see if the element has a name or id attribute if (e.hasAttribute("id")) { // id attribute found - use that buffer.append("[@id='" + e.getAttribute("id") + "']"); handled = true; } else if (e.hasAttribute("name")) { // name attribute found - use that buffer.append("[@name='" + e.getAttribute("name") + "']"); handled = true; } } if (!handled) { // no known attribute we could use - get sibling index int prev_siblings = 1; Node prev_sibling = n.getPreviousSibling(); while (null != prev_sibling) { if (prev_sibling.getNodeType() == n.getNodeType()) { if (prev_sibling.getNodeName().equalsIgnoreCase(n.getNodeName())) { prev_siblings++; } } prev_sibling = prev_sibling.getPreviousSibling(); } buffer.append("[" + prev_siblings + "]"); } } } } // return buffer return buffer.toString(); }