private Element findFirstElementByTagName(String tag, Node node) { if (node.nodeName().equals(tag)) return (Element) node; else { int size = node.childNodeSize(); for (int i = 0; i < size; i++) { Element found = findFirstElementByTagName(tag, node.childNode(i)); if (found != null) return found; } } return null; }
private List<Comment> getComments(Node node) { List<Comment> comments = new ArrayList<Comment>(); int i = 0; while (i < node.childNodes().size()) { Node child = node.childNode(i); if (child.nodeName().equals("#comment")) comments.add((Comment) child); else { comments.addAll(getComments(child)); } i++; } return comments; }
public void fixFigrefListItem(Element element) { Node next = element.nextSibling(); String trailingTxt; if (next != null && next instanceof TextNode) { trailingTxt = ((TextNode) next).getWholeText(); } else if (next != null && next instanceof Element) { trailingTxt = ((Element) next).text(); } else { return; } if (trailingTxt.matches("^(, |,? and )")) { next = element.nextSibling().nextSibling(); if (next.nodeName().toLowerCase().equals("b")) { String containedTxt = ((TextNode) next.childNode(0)).getWholeText(); if (containedTxt.matches("[0-9]{1,2}[A-z]?")) { Element newEl = element.clone(); newEl.attr("id", "FR-" + Strings.padStart(containedTxt, 4, '0')); newEl.attr("idref", ReferenceTagger.createFigId(containedTxt)); newEl.tagName("a"); newEl.addClass("figref"); newEl.text(containedTxt); next.replaceWith(newEl); fixFigrefListItem(newEl); } } } }
/** * Start a depth-first traverse of the root and all of its descendants. * @param visitor Node visitor. * @param root the root node point to traverse. */ public static void traverse(NodeVisitor visitor, Node root) { Node node = root; int depth = 0; while (node != null) { visitor.head(node, depth); if (node.childNodeSize() > 0) { node = node.childNode(0); depth++; } else { while (node.nextSibling() == null && depth > 0) { visitor.tail(node, depth); node = node.parentNode(); depth--; } visitor.tail(node, depth); if (node == root) break; node = node.nextSibling(); } } }
private static void removeComments(Node node) { for (int i = 0; i < node.childNodes().size();) { Node child = node.childNode(i); if (child.nodeName().equals("#comment")) Log.printAndRemove(child, "removeComments"); else { removeComments(child); i++; } } } }
node = node.childNode(0); ++depth; continue;
node = node.childNode(0); ++depth; continue;
if (node.parent().childNode(nodeCount - 1) == node) { transferIdToParent();
private static void removeCommentsRecursively(Node node) { int i = 0; while (i < node.childNodeSize()) { Node child = node.childNode(i); if (child instanceof Comment) { child.remove(); } else { removeCommentsRecursively(child); i++; } } }
Node node1 = null; while (node1 == null && index < parent.childNodeSize()) { node1 = parent.childNode(index++); if (node.equals(node1)) { break;
public void traverse(Node root) { Node node = root; int depth = 0; while (node != null) { if (filter(node) || replace(node)) { node = node.nextSibling(); continue; } head(node, depth); if (node.childNodeSize() > 0) { node = node.childNode(0); depth++; } else { while (node.nextSibling() == null && depth > 0) { tail(node, depth); node = node.parentNode(); depth--; } tail(node, depth); if (node == root) { break; } node = node.nextSibling(); } } }