/** * @return An Iterable of children elements. */ public default Iterable<Element> getElements() { return getChildren(Element.class); }
/** * @return An Iterable if text children. */ public default Iterable<Text> getTexts() { return getChildren(Text.class); }
/** * @return An Iterable if comment children. */ public default Iterable<Comment> getComments() { return getChildren(Comment.class); }
/** * Returns the child at a given, index or null. * <p> * {@code null} is returned when index is invalid. * * @param index The index. * @return The child at {@code index}. * @throws IndexOutOfBoundsException When index is out of range ({@code index < 0 || index >= getChildrenCount()}). */ public default Child getChildAt(int index) { return getChildren().get(index); }
/** * @return The number of children. */ public default int getChildrenCount() { return getChildren().size(); }
/** * @return The last child, if any, or {@code null}. */ public default Child getLastChild() { final List<? extends Child> children = getChildren(); if (children.isEmpty()) { return null; } else { return children.get(children.size() - 1); } }
/** * Returns the child that is an instance of a given class and is at a given position. * * @param <T> Type of searched children. * @param childClass The class of searched children. * @param index The index of the searched child. * This index takes into account elements that match {@code childClass}. * @return The element matching {@code childClass} and at {@code index}, or {@code null}. */ public default <T extends Node> T getChildAt(Class<T> childClass, int index) { return Iterables.getAtOrNull(getChildren(childClass), index); }
/** * Returns an Iterable of children elements that have a given name, ignoring case. * * @param name The name. * @return An Iterable containing elements named {@code name} ignoring case. */ public default Iterable<Element> getElementsNamedIgnoreCase(String name) { return getChildren(Element.class, Element.namedIgnoreCase(name)); }
/** * Returns the number of children that match a class and predicate. * * @param <T> Type of counted children. * @param childClass The class of counted children. * @param predicate The predicate. * @return The number of children that match {@code childClass} and {@code predicate}. */ public default <T extends Node> int getChildrenCount(Class<T> childClass, Predicate<? super T> predicate) { return Iterables.size(getChildren(childClass, predicate)); }
/** * Returns {@code true} if this parent has children of a given class. * * @param childClass The child class. * @return {@code true} if this parent has children of {@code childClass} class. */ public default boolean hasChildren(Class<? extends Node> childClass) { return !Iterables.isEmpty(getChildren(childClass)); }
/** * @return {@code true} if this parent has children. */ public default boolean hasChildren() { return !Iterables.isEmpty(getChildren()); }
public default <T extends Node> T getChild(Class<T> childClass, Predicate<? super T> predicate) { return Iterables.getFirstOrNull(getChildren(childClass, predicate)); }
/** * Returns an Iterable of children elements that have a given name. * * @param name The name. * @return An Iterable containing elements named {@code name}. */ public default Iterable<Element> getElementsNamed(String name) { return getChildren(Element.class, Element.named(name)); }
/** * Returns the number of children that match a class. * * @param childClass The class of counted children. * @return The number of children that match {@code childClass}. */ public default int getChildrenCount(Class<? extends Node> childClass) { return Iterables.size(getChildren(childClass)); }
/** * Sorts children locally. * * @param comparator The comparator. */ public default void sortChildren(Comparator<? super Child> comparator) { if (hasChildren()) { Collections.sort(getChildren(), comparator); } }
/** * @return The index of this child or -1. */ public default int getIndex() { if (getParent() != null) { return getParent().getChildren().indexOf(this); } else { return -1; } }
/** * Returns an Iterable of children that are instance of a given class. * * @param <T> Type of searched children. * @param childClass The class of searched children. * @return An Iterable of children that are instance of {@code childClass}. */ public default <T extends Node> Iterable<T> getChildren(Class<T> childClass) { return Iterables.filterAndConvert(childClass, getChildren(), Predicates.isInstanceOf(childClass)); }
/** * Set the index of this child. * * @param to The new index of this child. * @throws IllegalArgumentException When this child has no parent,<br> * or {@code to < 0 or to >= getParent().getChildrenCount()}. */ public default void setIndex(int to) { CollectionsUtil.setIndex(getParent().getChildren(), this, to); }
private static void print(Node node, Logger logger, Level level, int depth) { logger.log(level, indent(depth) + toString(node)); if (node instanceof Parent) { final Parent parent = (Parent) node; for (final Node child : parent.getChildren()) { print(child, logger, level, depth + 1); } } }
private void count(Node node) { counts[node.getType().ordinal()]++; if (node instanceof Element) { atts += ((Element) node).getAttributesCount(); } if (node instanceof Parent) { for (final Node child : ((Parent) node).getChildren()) { count(child); } } } }