List of usage examples for org.w3c.dom Node getParentNode
public Node getParentNode();
From source file:Main.java
public static void deleteNodes(Node node, String express) { NodeList dels = selectNodes(node, express); Node del; if (dels != null) { for (int i = 0; i < dels.getLength(); i++) { del = dels.item(i);//from www . j a v a 2 s .c o m if (del != null) del.getParentNode().removeChild(del); } } }
From source file:Main.java
/** * @param n//from ww w. j av a 2 s . co m * the node * @return the first element in the ancestor tree of {@code n}. If * {@code n} is an {@link Element}, {@code n} is returned. If * {@code n} is <code>null</code>, this method returns * <code>null</code>. */ public static Element getAncestorElement(Node n) { if (n == null) { return null; } if (n.getNodeType() == Node.ELEMENT_NODE) { return (Element) n; } Node parent = n.getParentNode(); if (parent == null) { Document doc = (Document) (n instanceof Document ? n : n.getOwnerDocument()); return doc == null ? null : doc.getDocumentElement(); } return getAncestorElement(parent); }
From source file:Main.java
private static int findNodeIndex(Node node) { String nm = node.getLocalName(); String ns = node.getNamespaceURI(); short nt = node.getNodeType(); Node parentNode = node.getParentNode(); if (parentNode.getNodeType() != Node.ELEMENT_NODE) return 1; Node child = parentNode.getFirstChild(); int ix = 0;//from ww w . j ava 2 s . co m while (child != null) { if (child == node) return ix + 1; if (child.getNodeType() == nt && nm.equals(child.getLocalName()) && ((ns == null && child.getNamespaceURI() == null) || (ns != null && ns.equals(child.getNamespaceURI())))) ix++; child = child.getNextSibling(); } throw new RuntimeException("Child node not found in parent!?"); }
From source file:Main.java
/** * Get parent element of specified element *//* w w w . j a v a 2s .c om*/ public static Element getParentElement(Element element) { Node parent = element.getParentNode(); while (parent != null) { if (parent instanceof Element) return (Element) parent; parent = parent.getParentNode(); } return null; }
From source file:Main.java
public static void moveDown(Node currentN) { Node nextSibling = findNextElement(currentN, false); Node nextNextSibling = findNextElement(nextSibling, false); Node parent = currentN.getParentNode(); parent.removeChild(currentN);//from www . ja v a 2 s . c om if (nextNextSibling != null) { parent.insertBefore(currentN, nextNextSibling); } else parent.appendChild(currentN); }
From source file:Main.java
public static void moveDown(final Node currentN) { Node nextSibling = findNextElement(currentN, false); Node nextNextSibling = findNextElement(nextSibling, false); Node parent = currentN.getParentNode(); parent.removeChild(currentN);/*from w w w.jav a 2s.co m*/ if (nextNextSibling != null) { parent.insertBefore(currentN, nextNextSibling); } else { parent.appendChild(currentN); } }
From source file:Main.java
/** * This method is a tree-search to help prevent against wrapping attacks. It checks that no * two Elements have ID Attributes that match the "value" argument, if this is the case then * "false" is returned. Note that a return value of "true" does not necessarily mean that * a matching Element has been found, just that no wrapping attack has been detected. */// ww w.j a v a 2 s. c o m public static boolean protectAgainstWrappingAttack(Node startNode, String value) { Node startParent = startNode.getParentNode(); Node processedNode = null; Element foundElement = null; String id = value.trim(); if (id.charAt(0) == '#') { id = id.substring(1); } while (startNode != null) { if (startNode.getNodeType() == Node.ELEMENT_NODE) { Element se = (Element) startNode; NamedNodeMap attributes = se.getAttributes(); if (attributes != null) { for (int i = 0; i < attributes.getLength(); i++) { Attr attr = (Attr) attributes.item(i); if (attr.isId() && id.equals(attr.getValue())) { if (foundElement == null) { // Continue searching to find duplicates foundElement = attr.getOwnerElement(); } else { //log.debug("Multiple elements with the same 'Id' attribute value!"); return false; } } } } } processedNode = startNode; startNode = startNode.getFirstChild(); // no child, this node is done. if (startNode == null) { // close node processing, get sibling startNode = processedNode.getNextSibling(); } // no more siblings, get parent, all children // of parent are processed. while (startNode == null) { processedNode = processedNode.getParentNode(); if (processedNode == startParent) { return true; } // close parent node processing (processed node now) startNode = processedNode.getNextSibling(); } } return true; }
From source file:Main.java
/** * Get the next node in a depth first preorder traversal. * * <ul>// w w w .ja v a 2 s. com * <li>If the node has a child, return the child * <li>Else if the node has a following sibling, return that sibling * <li>Else if the node has a following uncle, return that uncle * </ul> * * @param node the current node * @return the next node in the preorder traversal */ public static Node getNext(Node node) { if (node == null) return null; if (node.getFirstChild() != null) return node.getFirstChild(); for (; node != null; node = node.getParentNode()) { if (node.getNextSibling() != null) return node.getNextSibling(); } return null; }
From source file:Main.java
/** * This method is a tree-search to help prevent against wrapping attacks. It checks that no other * Element than the given "knownElement" argument has an ID attribute that matches the "value" * argument, which is the ID value of "knownElement". If this is the case then "false" is returned. *//* w w w . ja v a 2s. c o m*/ public static boolean protectAgainstWrappingAttack(Node startNode, Element knownElement, String value) { Node startParent = startNode.getParentNode(); Node processedNode = null; String id = value.trim(); if (id.charAt(0) == '#') { id = id.substring(1); } while (startNode != null) { if (startNode.getNodeType() == Node.ELEMENT_NODE) { Element se = (Element) startNode; NamedNodeMap attributes = se.getAttributes(); if (attributes != null) { for (int i = 0; i < attributes.getLength(); i++) { Attr attr = (Attr) attributes.item(i); if (attr.isId() && id.equals(attr.getValue()) && se != knownElement) { //log.debug("Multiple elements with the same 'Id' attribute value!"); return false; } } } } processedNode = startNode; startNode = startNode.getFirstChild(); // no child, this node is done. if (startNode == null) { // close node processing, get sibling startNode = processedNode.getNextSibling(); } // no more siblings, get parent, all children // of parent are processed. while (startNode == null) { processedNode = processedNode.getParentNode(); if (processedNode == startParent) { return true; } // close parent node processing (processed node now) startNode = processedNode.getNextSibling(); } } return true; }
From source file:Main.java
public static String getNodeHierarchy(Node node) { StringBuffer sb = new StringBuffer(); if (node != null) { Stack<Node> st = new Stack<Node>(); st.push(node);//from w ww . j a v a 2 s . co m Node parent = node.getParentNode(); while ((parent != null) && (parent.getNodeType() != Node.DOCUMENT_NODE)) { st.push(parent); parent = parent.getParentNode(); } // constructs node hierarchy Node n = null; while (!st.isEmpty() && null != (n = st.pop())) { if (n instanceof Element) { Element e = (Element) n; if (sb.length() == 0) { sb.append(e.getTagName()); } else { sb.append("/" + e.getTagName()); } } } } return sb.toString(); }