Class DataUtils


  • public final class DataUtils
    extends Object
    Utilities for modification of data trees.
    Author:
    Damien Carbonne
    • Method Detail

      • setNameOfMatchingAttributes

        public static void setNameOfMatchingAttributes​(Parent parent,
                                                       AttributePredicate predicate,
                                                       AttributeNameConverter converter,
                                                       Evaluator<? super Parent> evaluator)
        Sets names of matching attributes.

        Uses a Depth First traversal for recursion.
        Change is applied on parent before its children.

        Parameters:
        parent - The initial node. MUST NOT be null.
        predicate - The predicate of matching attributes. MUST NOT be null.
        converter - The attribute name converter. MUST NOT be null.
        evaluator - The evaluator. MUST NOT be null.
      • setValueOfMatchingAttributes

        public static void setValueOfMatchingAttributes​(Parent parent,
                                                        AttributePredicate predicate,
                                                        AttributeValueConverter converter,
                                                        Evaluator<? super Parent> evaluator)
        Sets value of matching attributes.

        Uses a Depth First traversal for recursion.
        Change is applied on parent before its children.

        Parameters:
        parent - The initial node. MUST NOT be null.
        predicate - The predicate of matching attributes. MUST NOT be null.
        converter - The attribute value converter. MUST NOT be null.
        evaluator - The evaluator. MUST NOT be null.
      • removeMatchingAttributes

        public static void removeMatchingAttributes​(Parent parent,
                                                    AttributePredicate predicate,
                                                    Evaluator<? super Parent> evaluator)
        Removes matching attributes.

        Uses a Depth First traversal for recursion.
        Removal is applied on parent before its children.

        Parameters:
        parent - The initial node. MUST NOT be null.
        predicate - The predicate of matching attributes. MUST NOT be null.
        evaluator - The evaluator. MUST NOT be null.
      • removeEmptyAttributes

        public static void removeEmptyAttributes​(Parent parent,
                                                 Evaluator<? super Parent> evaluator)
        Recursively removes empty attributes.

        Uses a Depth First traversal for recursion.
        Removal is applied on parent before its children.

        Parameters:
        parent - The initial node. MUST NOT be null.
        evaluator - The evaluator. MUST NOT be null.
      • sortAttributes

        public static void sortAttributes​(Parent parent,
                                          Comparator<Attribute> comparator,
                                          Evaluator<? super Parent> evaluator)
        Sorts attributes of elements using a user defined sorter.

        Uses a Depth First traversal for recursion.
        Sort is applied on parent before its children.

        Parameters:
        parent - The initial node. MUST NOT be null.
        comparator - The attribute name comparator. MUST NOT be null.
        evaluator - The evaluator. MUST NOT be null.
      • moveAttributeFirst

        public static void moveAttributeFirst​(Parent parent,
                                              Predicate<? super Element> predicate,
                                              String name,
                                              Evaluator<? super Parent> evaluator)
        Moves the attribute that has a given name to first position, if it exists.

        Uses a Depth First traversal for recursion.
        Move is applied on parent before its children.

        Parameters:
        parent - The initial node. MUST NOT be null.
        predicate - The predicate of matching elements. MUST NOT be null.
        name - The attribute name.
        evaluator - The evaluator. MUST NOT be null.
      • setNameOfMatchingElements

        public static void setNameOfMatchingElements​(Parent parent,
                                                     Predicate<? super Element> predicate,
                                                     ElementNameConverter converter,
                                                     Evaluator<? super Parent> evaluator)
        Sets the name of matching elements.

        Uses a Depth First traversal for recursion.
        Change is applied on parent before its children.

        Parameters:
        parent - The initial node. MUST NOT be null.
        predicate - The predicate of matching elements. MUST NOT be null.
        converter - The name converter. MUST NOT be null.
        evaluator - The evaluator. MUST NOT be null.
      • setContentOfMatchingTexts

        public static void setContentOfMatchingTexts​(Parent parent,
                                                     TextPredicate predicate,
                                                     TextContentConverter converter,
                                                     Evaluator<? super Parent> evaluator)
        Sets the content of matching texts.

        Uses a Depth First traversal for recursion.

        Parameters:
        parent - The initial node. MUST NOT be null.
        predicate - The predicate of matching texts. MUST NOT be null.
        converter - The content converter.
        evaluator - The evaluator. MUST NOT be null.
      • removeMatchingChildren

        public static void removeMatchingChildren​(Parent parent,
                                                  Predicate<? super Child> predicate,
                                                  Evaluator<? super Parent> evaluator)
        Removes matching children.

        Uses a Depth First traversal for recursion.
        Removal is applied on children before parent.

        Parameters:
        parent - The initial node. MUST NOT be null.
        predicate - The predicate of children nodes to remove. MUST NOT be null.
        evaluator - The evaluator. MUST NOT be null.
      • removeComments

        public static void removeComments​(Parent parent,
                                          Evaluator<? super Parent> evaluator)
        Removes comments nodes.

        Uses a Depth First traversal for recursion.
        Removal is applied on children before parent.

        Parameters:
        parent - The initial node. MUST NOT be null.
        evaluator - The evaluator. MUST NOT be null.
      • removeIgnorableText

        public static void removeIgnorableText​(Parent parent,
                                               Evaluator<? super Parent> evaluator)
        Removes text that can be ignored.

        Uses a Depth First traversal for recursion.
        Removal is applied on children before parent.

        Parameters:
        parent - The initial node. MUST NOT be null.
        evaluator - The evaluator. MUST NOT be null.
      • removePureElements

        public static void removePureElements​(Parent parent,
                                              Evaluator<? super Parent> evaluator)
        Removes pure elements (they have no children and no attributes).

        Uses a Depth First traversal for recursion.
        Removal is applied on children before parent.

        Parameters:
        parent - The initial node. MUST NOT be null.
        evaluator - The evaluator. MUST NOT be null.
      • sortChildren

        public static void sortChildren​(Parent parent,
                                        Predicate<? super Parent> predicate,
                                        Comparator<? super Child> comparator,
                                        Evaluator<? super Parent> evaluator)
        Sorts children nodes using a user-defined comparator.

        Uses a Depth First traversal for recursion.
        Sort is applied on parent before its children.

        Parameters:
        parent - The initial node. MUST NOT be null.
        predicate - The predicate of nodes whose children must be sorted. MUST NOT be null.
        comparator - The children comparator.
        evaluator - The evaluator. MUST NOT be null.
      • hasAncestorMatching

        public static boolean hasAncestorMatching​(Node node,
                                                  Predicate<Node> predicate)
      • hasDescendantMatching

        public static boolean hasDescendantMatching​(Node node,
                                                    Predicate<Node> predicate,
                                                    boolean strict)
      • hasAllDescendantsMatching

        public static boolean hasAllDescendantsMatching​(Node node,
                                                        Predicate<Node> predicate,
                                                        boolean strict)