private List getTail() { List list = parent().children(); int afterIndex = list.indexOf(this); List tail = new ArrayList(list.subList(afterIndex + 1, list.size())); list.subList(afterIndex + 1, list.size()).clear(); return tail; }
/** * Adds sibling nodes (defined using builder-style notation via a Closure) after the current node. * * @param c A Closure defining the new sibling nodes to add using builder-style notation. */ public void plus(Closure c) { if (parent() == null) { throw new UnsupportedOperationException("Adding sibling nodes to the root node is not supported"); } appendNodes(c); }
/** * Replaces the current node with nodes defined using builder-style notation via a Closure. * * @param c A Closure defining the new nodes using builder-style notation. * @return the original now replaced node */ public Node replaceNode(Closure c) { if (parent() == null) { throw new UnsupportedOperationException("Replacing the root node is not supported"); } appendNodes(c); getParentList(parent()).remove(this); this.setParent(null); return this; }
/** * Replaces the current node with the supplied node. * * @param n the new Node * @return the original now replaced node */ public Node replaceNode(Node n) { if (parent() == null) { throw new UnsupportedOperationException("Replacing the root node is not supported"); } List tail = getTail(); parent().appendNode(n.name(), n.attributes(), n.value()); parent().children().addAll(tail); getParentList(parent()).remove(this); this.setParent(null); return this; }
/** * Provides lookup of elements by non-namespaced name * * @param key the name (or shortcut key) of the node(s) of interest * @return the nodes which match key */ public Object get(String key) { if (key != null && key.charAt(0) == '@') { String attributeName = key.substring(1); return attributes().get(attributeName); } if ("..".equals(key)) { return parent(); } if ("*".equals(key)) { return children(); } if ("**".equals(key)) { return depthFirst(); } return getByName(key); }
/** * Adds sibling nodes (defined using builder-style notation via a Closure) after the current node. * * @param c A Closure defining the new sibling nodes to add using builder-style notation. */ public void plus(Closure c) { if (parent() == null) { throw new UnsupportedOperationException("Adding sibling nodes to the root node is not supported"); } appendNodes(c); }
int height(Node n){ int h=-1; while(n!=null){h++;n=n.parent;} return h; } Node LCA(Node n1, Node n2){ int discrepancy=height(n1)-height(n2); while(discrepancy>0) {n1=n1.parent;discrepancy--;} while(discrepancy<0) {n2=n2.parent;discrepancy++;} while(n1!=n2){n1=n1.parent();n2=n2.parent();} return n1; }
/** * Replaces the current node with nodes defined using builder-style notation via a Closure. * * @param c A Closure defining the new nodes using builder-style notation. * @return the last appended node */ public Node replaceNode(Closure c) { if (parent() == null) { throw new UnsupportedOperationException("Replacing the root node is not supported"); } Node result = appendNodes(c); getParentList(parent()).remove(this); // this.setParent(null); // return this; return result; }
private Node appendNodes(Closure c) { List list = parent().children(); int afterIndex = list.indexOf(this); List leftOvers = new ArrayList(list.subList(afterIndex + 1, list.size())); list.subList(afterIndex + 1, list.size()).clear(); Node lastAppended = null; for (Node child : buildChildrenFromClosure(c)) { lastAppended = parent().appendNode(child.name(), child.attributes(), child.value()); } parent().children().addAll(leftOvers); return lastAppended; }
/** * Associates node in original tree with target tree node to avoid overriding already inserted content. * * @param root current source node * @param child current child (which parent's id will be associated with root node) */ private static void associate(Node root, Node child) { root.attributes().put(NID_ATTR, nid(child.parent())); }
String html = "<html><body><div><p>Test Data</p> <div> <p>HELLO World</p></div></div> other text</body></html>"; Document doc = Jsoup.parse(html); List<Node> children = doc.childNodes(); // We will search nodes in a breadth-first way Queue<Node> nodes = new ArrayDeque<>(); nodes.addAll(doc.childNodes()); while (!nodes.isEmpty()) { Node n = nodes.remove(); if (n instanceof TextNode && ((TextNode) n).text().trim().length() > 0) { // Do whatever you want with n. // Here we just print its text... System.out.println(n.parent().nodeName()+" contains text: "+((TextNode) n).text().trim()); } else { nodes.addAll(n.childNodes()); } }
/** * Provides lookup of elements by non-namespaced name * * @param key the name (or shortcut key) of the node(s) of interest * @return the nodes which match key */ public Object get(String key) { if (key != null && key.charAt(0) == '@') { String attributeName = key.substring(1); return attributes().get(attributeName); } if ("..".equals(key)) { return parent(); } if ("*".equals(key)) { return children(); } if ("**".equals(key)) { return depthFirst(); } return getByName(key); }
/** * Provides lookup of elements by non-namespaced name * * @param key the name (or shortcut key) of the node(s) of interest * @return the nodes which match key */ public Object get(String key) { if (key != null && key.charAt(0) == '@') { String attributeName = key.substring(1); return attributes().get(attributeName); } if ("..".equals(key)) { return parent(); } if ("*".equals(key)) { return children(); } if ("**".equals(key)) { return depthFirst(); } return getByName(key); }
/** * Provides lookup of elements by non-namespaced name * * @param key the name (or shortcut key) of the node(s) of interest * @return the nodes which match key */ public Object get(String key) { if (key != null && key.charAt(0) == '@') { String attributeName = key.substring(1); return attributes().get(attributeName); } if ("..".equals(key)) { return parent(); } if ("*".equals(key)) { return children(); } if ("**".equals(key)) { return depthFirst(); } return getByName(key); }
/** * Provides lookup of elements by non-namespaced name * * @param key the name (or shortcut key) of the node(s) of interest * @return the nodes which match key */ public Object get(String key) { if (key != null && key.charAt(0) == '@') { String attributeName = key.substring(1); return attributes().get(attributeName); } if ("..".equals(key)) { return parent(); } if ("*".equals(key)) { return children(); } if ("**".equals(key)) { return depthFirst(); } return getByName(key); }