List of usage examples for org.w3c.dom Node getParentNode
public Node getParentNode();
From source file:org.dhatim.xml.DomUtils.java
/** * Replace one node with another node./* ww w . j av a 2 s . c o m*/ * @param newNode New node - added in same location as oldNode. * @param oldNode Old node - removed. */ public static void replaceNode(Node newNode, Node oldNode) { AssertArgument.isNotNull(newNode, "newNode"); AssertArgument.isNotNull(oldNode, "oldNode"); Node parentNode = oldNode.getParentNode(); if (parentNode == null) { logger.debug("Cannot replace node [" + oldNode + "] with [" + newNode + "]. [" + oldNode + "] has no parent."); } else { parentNode.replaceChild(newNode, oldNode); } }
From source file:org.dhatim.xml.DomUtils.java
/** * Replace one node with a list of nodes. * @param newNodes New nodes - added in same location as oldNode. * @param oldNode Old node - removed./*from ww w . j ava 2 s.c om*/ * @param clone Clone Nodelist Nodes. */ public static void replaceNode(NodeList newNodes, Node oldNode, boolean clone) { AssertArgument.isNotNull(newNodes, "newNodes"); AssertArgument.isNotNull(oldNode, "oldNode"); Node parentNode = oldNode.getParentNode(); if (parentNode == null) { logger.debug("Cannot replace [" + oldNode + "] with a NodeList. [" + oldNode + "] has no parent."); return; } int nodeCount = newNodes.getLength(); List nodeList = DomUtils.copyNodeList(newNodes); if (nodeCount == 0) { if (!(parentNode instanceof Document)) { parentNode.removeChild(oldNode); } return; } if (parentNode instanceof Document) { List elements = DomUtils.getElements(newNodes, "*", null); if (!elements.isEmpty()) { logger.debug( "Request to replace the Document root node with a 1+ in length NodeList. Replacing root node with the first element node from the NodeList."); parentNode.removeChild(oldNode); parentNode.appendChild((Node) elements.get(0)); } else { logger.debug( "Cannot replace document root element with a NodeList that doesn't contain an element node."); } } else { for (int i = 0; i < nodeCount; i++) { if (clone) { parentNode.insertBefore(((Node) nodeList.get(i)).cloneNode(true), oldNode); } else { parentNode.insertBefore((Node) nodeList.get(i), oldNode); } } parentNode.removeChild(oldNode); } }
From source file:org.dhatim.xml.DomUtils.java
/** * Insert the supplied node before the supplied reference node (refNode). * @param newNode Node to be inserted./*from w w w . j a v a2s . c o m*/ * @param refNode Reference node before which the supplied nodes should * be inserted. */ public static void insertBefore(Node newNode, Node refNode) { AssertArgument.isNotNull(newNode, "newNode"); AssertArgument.isNotNull(refNode, "refNode"); Node parentNode = refNode.getParentNode(); if (parentNode == null) { logger.debug( "Cannot insert [" + newNode + "] before [" + refNode + "]. [" + refNode + "] has no parent."); return; } if (parentNode instanceof Document && newNode.getNodeType() == Node.ELEMENT_NODE) { logger.debug( "Request to insert an element before the Document root node. This is not allowed. Replacing the Document root with the new Node."); parentNode.removeChild(refNode); parentNode.appendChild(newNode); } else { parentNode.insertBefore(newNode, refNode); } }
From source file:org.dhatim.xml.DomUtils.java
/** * Insert the supplied nodes before the supplied reference node (refNode). * @param newNodes Nodes to be inserted. * @param refNode Reference node before which the supplied nodes should * be inserted./*from w w w .ja v a2s . c o m*/ */ public static void insertBefore(NodeList newNodes, Node refNode) { AssertArgument.isNotNull(newNodes, "newNodes"); AssertArgument.isNotNull(refNode, "refNode"); Node parentNode = refNode.getParentNode(); if (parentNode == null) { logger.debug("Cannot insert a NodeList before [" + refNode + "]. [" + refNode + "] has no parent."); return; } int nodeCount = newNodes.getLength(); List nodeList = DomUtils.copyNodeList(newNodes); if (nodeCount == 0) { return; } if (parentNode instanceof Document) { List elements = DomUtils.getElements(newNodes, "*", null); if (!elements.isEmpty()) { logger.debug( "Request to insert a NodeList before the Document root node. Will replace the root element with the 1st element node from the NodeList."); parentNode.removeChild(refNode); parentNode.appendChild((Node) elements.get(0)); } else { logger.debug( "Cannot insert beforen the document root element from a NodeList that doesn't contain an element node."); } for (int i = 0; i < nodeCount; i++) { Node node = (Node) nodeList.get(i); if (node.getNodeType() != Node.ELEMENT_NODE) { System.out.println("****" + node); parentNode.insertBefore(node, refNode); } } } else { for (int i = 0; i < nodeCount; i++) { parentNode.insertBefore((Node) nodeList.get(i), refNode); } } }
From source file:org.dhatim.xml.DomUtils.java
/** * Get the parent element of the supplied element having the * specified tag name./*w ww . j a v a2s.c o m*/ * @param child Child element. * @param parentLocalName Parent element local name. * @param namespaceURI Namespace URI of the required parent element, * or null if a non-namespaced get is to be performed. * @return The first parent element of "child" having the tagname "parentName", * or null if no such parent element exists. */ public static Element getParentElement(Element child, String parentLocalName, String namespaceURI) { AssertArgument.isNotNull(child, "child"); AssertArgument.isNotNullAndNotEmpty(parentLocalName, "parentLocalName"); Node parentNode = child.getParentNode(); while (parentNode != null && parentNode.getNodeType() == Node.ELEMENT_NODE) { Element parentElement = (Element) parentNode; if (getName(parentElement).equalsIgnoreCase(parentLocalName)) { if (namespaceURI == null) { return parentElement; } else if (parentElement.getNamespaceURI().equals(namespaceURI)) { return parentElement; } } parentNode = parentNode.getParentNode(); } return null; }
From source file:org.dhatim.xml.DomUtils.java
public static Node getPreviousSibling(Node node, short nodeType) { AssertArgument.isNotNull(node, "node"); Node parent = node.getParentNode(); if (parent == null) { logger.debug("Cannot get node [" + node + "] previous sibling. [" + node + "] has no parent."); return null; }// ww w. j a v a 2 s . c o m NodeList siblings = parent.getChildNodes(); int siblingCount = siblings.getLength(); int nodeIndex = 0; // Locate the node for (int i = 0; i < siblingCount; i++) { Node sibling = siblings.item(i); if (sibling == node) { nodeIndex = i; break; } } if (nodeIndex == 0) { return null; } // Wind back to sibling for (int i = nodeIndex - 1; i >= 0; i--) { Node sibling = siblings.item(i); if (sibling.getNodeType() == nodeType) { return sibling; } } return null; }
From source file:org.dhatim.xml.DomUtils.java
/** * Count the DOM nodes of the supplied type (nodeType) before the supplied * node, not including the node itself.//from w w w . j a v a 2 s .co m * <p/> * Counts the sibling nodes. * @param node Node whose siblings are to be counted. * @param nodeType The DOM {@link Node} type of the siblings to be counted. * @return The number of siblings of the supplied type before the supplied node. */ public static int countNodesBefore(Node node, short nodeType) { AssertArgument.isNotNull(node, "node"); Node parent = node.getParentNode(); if (parent == null) { logger.debug("Cannot count nodes before [" + node + "]. [" + node + "] has no parent."); return 0; } NodeList siblings = parent.getChildNodes(); int count = 0; int siblingCount = siblings.getLength(); for (int i = 0; i < siblingCount; i++) { Node sibling = siblings.item(i); if (sibling == node) { break; } if (sibling.getNodeType() == nodeType) { count++; } } return count; }
From source file:org.dhatim.xml.DomUtils.java
/** * Count the DOM nodes of the supplied type (nodeType) between the supplied * sibling nodes, not including the nodes themselves. * <p/>//w ww . ja v a 2 s .c o m * Counts the sibling nodes. * @param node1 First sibling node. * @param node2 Second sibling node. * @param nodeType The DOM {@link Node} type of the siblings to be counted. * @return The number of siblings of the supplied type between the supplied * sibling nodes. * @throws UnsupportedOperationException if the supplied {@link Node Nodes} * don't have the same parent node i.e. are not sibling nodes. */ public static int countNodesBetween(Node node1, Node node2, short nodeType) { AssertArgument.isNotNull(node1, "node1"); AssertArgument.isNotNull(node2, "node2"); Node parent1 = node1.getParentNode(); if (parent1 == null) { logger.debug("Cannot count nodes between [" + node1 + "] and [" + node2 + "]. [" + node1 + "] has no parent."); return 0; } Node parent2 = node2.getParentNode(); if (parent2 == null) { logger.debug("Cannot count nodes between [" + node1 + "] and [" + node2 + "]. [" + node2 + "] has no parent."); return 0; } if (parent1 != parent2) { logger.debug("Cannot count nodes between [" + node1 + "] and [" + node2 + "]. These nodes do not share the same sparent."); return 0; } int countBeforeNode1 = countNodesBefore(node1, nodeType); int countBeforeNode2 = countNodesBefore(node2, nodeType); int count = countBeforeNode2 - countBeforeNode1; if (node1.getNodeType() == nodeType) { count--; } return count; }
From source file:org.dhatim.xml.DomUtils.java
/** * Count the DOM nodes before the supplied node, not including the node itself. * <p/>/*from ww w . j ava2 s. c o m*/ * Counts the sibling nodes. * @param node Node whose siblings are to be counted. * @return The number of siblings before the supplied node. */ public static int countNodesBefore(Node node) { AssertArgument.isNotNull(node, "node"); Node parent = node.getParentNode(); if (parent == null) { logger.debug("Cannot count nodes before [" + node + "]. [" + node + "] has no parent."); return 0; } NodeList siblings = parent.getChildNodes(); int count = 0; int siblingCount = siblings.getLength(); for (int i = 0; i < siblingCount; i++) { Node sibling = siblings.item(i); if (sibling == node) { break; } count++; } return count; }
From source file:org.dhatim.xml.DomUtils.java
/** * Count the DOM nodes between the supplied sibling nodes, not including * the nodes themselves.//from w w w .j a va 2s . com * <p/> * Counts the sibling nodes. * @param node1 First sibling node. * @param node2 Second sibling node. * @return The number of siblings between the supplied sibling nodes. * @throws UnsupportedOperationException if the supplied {@link Node Nodes} * don't have the same parent node i.e. are not sibling nodes. */ public static int countNodesBetween(Node node1, Node node2) { AssertArgument.isNotNull(node1, "node1"); AssertArgument.isNotNull(node2, "node2"); Node parent1 = node1.getParentNode(); if (parent1 == null) { logger.debug("Cannot count nodes between [" + node1 + "] and [" + node2 + "]. [" + node1 + "] has no parent."); return 0; } Node parent2 = node2.getParentNode(); if (parent2 == null) { logger.debug("Cannot count nodes between [" + node1 + "] and [" + node2 + "]. [" + node2 + "] has no parent."); return 0; } if (parent1 != parent2) { logger.debug("Cannot count nodes between [" + node1 + "] and [" + node2 + "]. These nodes do not share the same sparent."); return 0; } int countBeforeNode1 = countNodesBefore(node1); int countBeforeNode2 = countNodesBefore(node2); int count = countBeforeNode2 - countBeforeNode1 - 1; return count; }