Class Gagawa


  • public final class Gagawa
    extends Object
    Helper functions for Gagawa DOM Node, such as iterating in depth-first pre-order, finding elements by certain conditions, or reading or setting node data.

    Compared with the (terrible) Gagawa API, this class provides a proper abstraction that takes care of underlying low-level concerns, such as the string representation of a DOM tree.

    Since:
    8.1.0
    Author:
    XIMA MEDIA GmbH
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void addClass​(com.hp.gagawa.java.Node node, String... classes)
      Adds one or more classes to the list of classes.
      static <T extends com.hp.gagawa.java.Node>
      Iterable<T>
      allByClassName​(com.hp.gagawa.java.Node context, Class<T> type, String className)
      Finds elements in the subtree of the given context that have the given class.
      static <T extends com.hp.gagawa.java.Node>
      Iterable<T>
      allByName​(com.hp.gagawa.java.Node context, Class<T> type, String name)
      Finds all elements in the subtree of the given context that have a name attribute with the given value.
      static Iterable<com.hp.gagawa.java.Node> allByTagName​(com.hp.gagawa.java.Node context, String tagName)
      Find all elements in the subtree of the given context that have the given tag name.
      static <T extends com.hp.gagawa.java.Node>
      Iterable<T>
      allByType​(com.hp.gagawa.java.Node context, Class<T> type)
      Finds all element in the subtree of the given context that have the given type.
      static <T extends com.hp.gagawa.java.Node>
      Iterable<T>
      allMatching​(com.hp.gagawa.java.Node context, Class<T> type, Predicate<? super T> filter)
      Finds elements in the subtree of the given context that match a predicate.
      static void appendChild​(com.hp.gagawa.java.Node parent, com.hp.gagawa.java.Node... children)
      Adds a node as a child to another node.
      static String attr​(com.hp.gagawa.java.Node node, String name)
      Gets the value of an attribute with a given name.
      static void attr​(com.hp.gagawa.java.Node node, String name, String value)
      Sets an attribute with a given name to a given value.
      static <T extends com.hp.gagawa.java.Node>
      T
      byClassName​(com.hp.gagawa.java.Node context, Class<T> type, String className)
      Finds an element in the subtree of the given context that has the given class.
      static <T extends com.hp.gagawa.java.Node>
      T
      byName​(com.hp.gagawa.java.Node context, Class<T> type, String name)
      Find the first element in the subtree of the given context that has a name attribute with the given value.
      static com.hp.gagawa.java.Node byTagName​(com.hp.gagawa.java.Node context, String tagName)
      Find the first element in the subtree of the given context that has the given tag name.
      static <T extends com.hp.gagawa.java.Node>
      T
      byType​(com.hp.gagawa.java.Node context, Class<T> type)
      Finds an element in the subtree of the given context that has the given type.
      static String[] classList​(com.hp.gagawa.java.Node node)
      Gets a list of classes of a node.
      static String data​(com.hp.gagawa.java.Node node, String name)
      Gets the value of a data attribute with a given name.
      static void data​(com.hp.gagawa.java.Node node, String name, boolean value)
      Sets a data attribute with a given name to a given value.
      static void data​(com.hp.gagawa.java.Node node, String name, int value)
      Sets a data attribute with a given name to a given value.
      static void data​(com.hp.gagawa.java.Node node, String name, long value)
      Sets a data attribute with a given name to a given value.
      static void data​(com.hp.gagawa.java.Node node, String name, String value)
      Sets a data attribute with a given name to a given value.
      static void empty​(com.hp.gagawa.java.Node node)
      Empties the given node, i.e.
      static boolean hasAttr​(com.hp.gagawa.java.Node node, String name)
      Checks whether a node has an attribute with a given name.
      static boolean hasClass​(com.hp.gagawa.java.Node node, String className)
      Checks whether a node has a given class.
      static boolean hasData​(com.hp.gagawa.java.Node node, String name)
      Checks whether a node has a data attribute with a given name.
      static boolean hasTagName​(com.hp.gagawa.java.Node node, String tagName)
      Checks whether the node has the given tag name.
      static <T extends com.hp.gagawa.java.Node>
      T
      matching​(com.hp.gagawa.java.Node context, Class<T> type, Predicate<? super T> filter)
      Finds an element in the subtree of the given context that matches a predicate.
      static com.hp.gagawa.java.elements.Script newInlineScript​(String js)
      Creates a new script tag for an inline script.
      static com.hp.gagawa.java.elements.Script newInlineScript​(String js, String name)
      Creates a new script tag for an inline script.
      static com.hp.gagawa.java.elements.Style newInlineStyle​(String css)
      Creates a new style tag for an inline stylesheet.
      static com.hp.gagawa.java.elements.Style newInlineStyle​(String css, String name)
      Creates a new style tag for an inline stylesheet.
      static <T extends com.hp.gagawa.java.Node>
      Iterable<T>
      parents​(com.hp.gagawa.java.Node node, Class<T> type)
      Finds all parents of a node, not including the node itself.
      static com.hp.gagawa.java.Node remove​(com.hp.gagawa.java.Node node)
      Detaches a node from its tree, if it is part of a tree structure.
      static void removeAttr​(com.hp.gagawa.java.Node node, String name)
      Removes an attribute with a given name, if the node has such an attribute.
      static String tagName​(com.hp.gagawa.java.Node node)
      Gets the tag name of a node, which is always upper case, e.g.
      static String text​(com.hp.gagawa.java.Node node)
      Gets the text content of the given node.
      static void text​(com.hp.gagawa.java.Node node, String text)
      Sets the text content of the given node to a given value.
    • Method Detail

      • addClass

        public static void addClass​(com.hp.gagawa.java.Node node,
                                    String... classes)
        Adds one or more classes to the list of classes.
        Parameters:
        node - A node to which to add classes.
        classes - Classes to add.
      • allByClassName

        public static <T extends com.hp.gagawa.java.Node> Iterable<T> allByClassName​(com.hp.gagawa.java.Node context,
                                                                                     Class<T> type,
                                                                                     String className)
        Finds elements in the subtree of the given context that have the given class. The given context node is never returned.
        Type Parameters:
        T - Type of the nodes to which to limit the search.
        Parameters:
        context - Context to search.
        type - Type of the nodes to which to limit the search. Use Node.class to not limit the search to a certain type.
        className - Class name to look for.
        Returns:
        All elements in the subtree of the given context with given class name, in depth-first pre-order.
      • allByName

        public static <T extends com.hp.gagawa.java.Node> Iterable<T> allByName​(com.hp.gagawa.java.Node context,
                                                                                Class<T> type,
                                                                                String name)
        Finds all elements in the subtree of the given context that have a name attribute with the given value. The given context node is never returned.
        Type Parameters:
        T - Type of the nodes to which to limit the search.
        Parameters:
        context - Context to search.
        type - Type of the nodes to which to limit the search. Use Node.class to not limit the search to a certain type.
        name - Name to search for.
        Returns:
        All elements in the subtree of the given context with given value for the name attribute, in depth-first pre-order.
      • allByTagName

        public static Iterable<com.hp.gagawa.java.Node> allByTagName​(com.hp.gagawa.java.Node context,
                                                                     String tagName)
        Find all elements in the subtree of the given context that have the given tag name. The given context node is never returned.

        The tag name is case-insensitive, all of DIV, div, and dIv yield the same result.

        Parameters:
        context - Context to search.
        tagName - Tag name to search for.
        Returns:
        All elements in the subtree of the given context with given tag name, in depth-first pre-order.
      • allByType

        public static <T extends com.hp.gagawa.java.Node> Iterable<T> allByType​(com.hp.gagawa.java.Node context,
                                                                                Class<T> type)
        Finds all element in the subtree of the given context that have the given type. The given context node is never returned.
        Type Parameters:
        T - Type of the nodes to which to limit the search.
        Parameters:
        context - Context to search.
        type - Type of the nodes to which to limit the search. Use Node.class to not limit the search to a certain type.
        Returns:
        All elements in the subtree of the given context that have the given type, in depth-first pre-order.
      • allMatching

        public static <T extends com.hp.gagawa.java.Node> Iterable<T> allMatching​(com.hp.gagawa.java.Node context,
                                                                                  Class<T> type,
                                                                                  Predicate<? super T> filter)
        Finds elements in the subtree of the given context that match a predicate. The given context node is never returned.
        Type Parameters:
        T - Type of the nodes to which to limit the search.
        Parameters:
        context - Context to search.
        type - Type of the nodes to which to limit the search. Use Node.class to not limit the search to a certain type.
        filter - Filter to apply to each node.
        Returns:
        All elements in the subtree of the given context that match the given filter, in depth-first pre-order.
      • appendChild

        public static void appendChild​(com.hp.gagawa.java.Node parent,
                                       com.hp.gagawa.java.Node... children)
        Adds a node as a child to another node.
        Parameters:
        parent - Parent to which to add the child.
        children - Children to add.
        Throws:
        IllegalArgumentException - When the parent is equal to one of the given children.
      • attr

        public static void attr​(com.hp.gagawa.java.Node node,
                                String name,
                                String value)
        Sets an attribute with a given name to a given value.
        Parameters:
        node - Node on which to set an attribute.
        name - Name of the attribute.
        value - Value of the attribute. Use null for boolean attributes such as selected.
        Throws:
        IllegalArgumentException - If the name is not a valid attribute according to the HTML spec.
      • attr

        public static String attr​(com.hp.gagawa.java.Node node,
                                  String name)
        Gets the value of an attribute with a given name.
        Parameters:
        node - Node from which to read an attribute.
        name - Name of the attribute to read.
        Returns:
        Value of the attribute, never null, but can be empty.
        Throws:
        IllegalArgumentException - If the name is not a valid attribute according to the HTML spec.
      • byClassName

        public static <T extends com.hp.gagawa.java.Node> T byClassName​(com.hp.gagawa.java.Node context,
                                                                        Class<T> type,
                                                                        String className)
        Finds an element in the subtree of the given context that has the given class. The given context node is never returned.
        Type Parameters:
        T - Type of the nodes to which to limit the search.
        Parameters:
        context - Context to search.
        type - Type of the nodes to which to limit the search. Use Node.class to not limit the search to a certain type.
        className - Class name to look for.
        Returns:
        The first element in the subtree of the given context, when traversed in depth-first pre-order, that has the given class name.
      • byName

        public static <T extends com.hp.gagawa.java.Node> T byName​(com.hp.gagawa.java.Node context,
                                                                   Class<T> type,
                                                                   String name)
        Find the first element in the subtree of the given context that has a name attribute with the given value. The given context node is never returned.
        Type Parameters:
        T - Type of the nodes to which to limit the search.
        Parameters:
        context - Context to search.
        type - Type of the nodes to which to limit the search. Use Node.class to not limit the search to a certain type.
        name - Name to search for.
        Returns:
        The first element in the subtree of the given context, when traversed in depth-first pre-order, with given value for the name attribute,
      • byTagName

        public static com.hp.gagawa.java.Node byTagName​(com.hp.gagawa.java.Node context,
                                                        String tagName)
        Find the first element in the subtree of the given context that has the given tag name. The given context node is never returned.

        The tag name is case-insensitive, all of DIV, div, and dIv yield the same result.

        Parameters:
        context - Context to search.
        tagName - Tag name to search for.
        Returns:
        The first element in the subtree of the given context, when traversed in depth-first pre-order, with given tag name.
      • byType

        public static <T extends com.hp.gagawa.java.Node> T byType​(com.hp.gagawa.java.Node context,
                                                                   Class<T> type)
        Finds an element in the subtree of the given context that has the given type. The given context node is never returned.
        Type Parameters:
        T - Type of the nodes to which to limit the search.
        Parameters:
        context - Context to search.
        type - Type of the nodes to which to limit the search. Use Node.class to not limit the search to a certain type.
        Returns:
        The first element in the subtree of the given context, when traversed in depth-first pre-order, that has the given type.
      • classList

        public static String[] classList​(com.hp.gagawa.java.Node node)
        Gets a list of classes of a node.
        Parameters:
        node - Node to process.
        Returns:
        The classes of the node.
      • data

        public static void data​(com.hp.gagawa.java.Node node,
                                String name,
                                String value)
        Sets a data attribute with a given name to a given value.
        Parameters:
        node - Node on which to set the attribute.
        name - Name of the data attribute in camel case style, e.g. fooBar
        value - Value of the attribute.
        Throws:
        IllegalArgumentException - When the given name is not a valid data attribute name.
      • data

        public static void data​(com.hp.gagawa.java.Node node,
                                String name,
                                int value)
        Sets a data attribute with a given name to a given value.
        Parameters:
        node - Node on which to set the attribute.
        name - Name of the data attribute in camel case style, e.g. fooBar
        value - Value of the attribute.
        Throws:
        IllegalArgumentException - When the given name is not a valid data attribute name.
      • data

        public static void data​(com.hp.gagawa.java.Node node,
                                String name,
                                long value)
        Sets a data attribute with a given name to a given value.
        Parameters:
        node - Node on which to set the attribute.
        name - Name of the data attribute in camel case style, e.g. fooBar
        value - Value of the attribute.
        Throws:
        IllegalArgumentException - When the given name is not a valid data attribute name.
      • data

        public static void data​(com.hp.gagawa.java.Node node,
                                String name,
                                boolean value)
        Sets a data attribute with a given name to a given value.
        Parameters:
        node - Node on which to set the attribute.
        name - Name of the data attribute in camel case style, e.g. fooBar
        value - Value of the attribute.
        Throws:
        IllegalArgumentException - When the given name is not a valid data attribute name.
      • data

        public static String data​(com.hp.gagawa.java.Node node,
                                  String name)
        Gets the value of a data attribute with a given name.
        Parameters:
        node - Node on which to set the attribute.
        name - Name of the data attribute in camel case style, e.g. fooBar
        Returns:
        Value of the attribute. Empty string when the data attribute does not exist.
        Throws:
        IllegalArgumentException - When the given name is not a valid data attribute name.
      • empty

        public static void empty​(com.hp.gagawa.java.Node node)
        Empties the given node, i.e. removes all children.
        Parameters:
        node - Node to empty.
      • hasAttr

        public static boolean hasAttr​(com.hp.gagawa.java.Node node,
                                      String name)
        Checks whether a node has an attribute with a given name.
        Parameters:
        node - Node from which to read an attribute.
        name - Name of the attribute to read.
        Returns:
        Whether the node has an attribute with the given name (which may have an empty value).
        Throws:
        IllegalArgumentException - If the name is not a valid attribute according to the HTML spec.
      • hasClass

        public static boolean hasClass​(com.hp.gagawa.java.Node node,
                                       String className)
        Checks whether a node has a given class.
        Parameters:
        node - Node to check.
        className - Class name to check.
        Returns:
        Whether the node has the given class.
      • hasData

        public static boolean hasData​(com.hp.gagawa.java.Node node,
                                      String name)
        Checks whether a node has a data attribute with a given name.
        Parameters:
        node - Node on which to set the attribute.
        name - Name of the data attribute in camel case style, e.g. fooBar
        Returns:
        Whether the node has a value for the data attribute (which may be the empty string).
        Throws:
        IllegalArgumentException - When the given name is not a valid data attribute name.
      • hasTagName

        public static boolean hasTagName​(com.hp.gagawa.java.Node node,
                                         String tagName)
        Checks whether the node has the given tag name.
        Parameters:
        node - A node to check.
        tagName - A tag name such as div or LEGEND. Can be lower, upper, or mixed case.
        Returns:
        Whether the node's tag name equals the given tag name.
      • matching

        public static <T extends com.hp.gagawa.java.Node> T matching​(com.hp.gagawa.java.Node context,
                                                                     Class<T> type,
                                                                     Predicate<? super T> filter)
        Finds an element in the subtree of the given context that matches a predicate. The given context node is never returned.
        Type Parameters:
        T - Type of the nodes to which to limit the search.
        Parameters:
        context - Context to search.
        type - Type of the nodes to which to limit the search. Use Node.class to not limit the search to a certain type.
        filter - Filter to apply to each node.
        Returns:
        The first element in the subtree of the given context, when traversed in depth-first pre-order, that matches the given filter.
      • newInlineStyle

        public static com.hp.gagawa.java.elements.Style newInlineStyle​(String css)
        Creates a new style tag for an inline stylesheet.
        Parameters:
        css - CSS content for the inline style sheet.
        Returns:
        A new inline style tag.
      • newInlineStyle

        public static com.hp.gagawa.java.elements.Style newInlineStyle​(String css,
                                                                       String name)
        Creates a new style tag for an inline stylesheet.
        Parameters:
        css - CSS content for the inline style sheet.
        name - Name of the style tag, can be empty or null.
        Returns:
        A new inline style tag.
      • newInlineScript

        public static com.hp.gagawa.java.elements.Script newInlineScript​(String js)
        Creates a new script tag for an inline script.
        Parameters:
        js - JavaScript content for the inline style sheet.
        Returns:
        A new inline script tag.
      • newInlineScript

        public static com.hp.gagawa.java.elements.Script newInlineScript​(String js,
                                                                         String name)
        Creates a new script tag for an inline script.
        Parameters:
        js - JavaScript content for the inline style sheet.
        name - Name of the script tag, can be empty or null.
        Returns:
        A new inline script tag.
      • parents

        public static <T extends com.hp.gagawa.java.Node> Iterable<T> parents​(com.hp.gagawa.java.Node node,
                                                                              Class<T> type)
        Finds all parents of a node, not including the node itself.
        Type Parameters:
        T - Type of the nodes to which to limit the search.
        Parameters:
        node - A node where to start.
        type - Type of the nodes to which to limit the search. Use Node.class to not limit the search to a certain type.
        Returns:
        A list of all parents of the node, starting with the closest (immediate) parent.
      • remove

        public static com.hp.gagawa.java.Node remove​(com.hp.gagawa.java.Node node)
        Detaches a node from its tree, if it is part of a tree structure.
        Parameters:
        node - Node to remove.
        Returns:
        The removed node.
      • removeAttr

        public static void removeAttr​(com.hp.gagawa.java.Node node,
                                      String name)
        Removes an attribute with a given name, if the node has such an attribute.
        Parameters:
        node - Node on which from which to remove an attribute.
        name - Name of the attribute to remove.
        Throws:
        IllegalArgumentException - If the name is not a valid attribute according to the HTML spec.
      • tagName

        public static String tagName​(com.hp.gagawa.java.Node node)
        Gets the tag name of a node, which is always upper case, e.g. LEGEND.
        Parameters:
        node - A node with a tag name.
        Returns:
        The tag name of the node.
      • text

        public static void text​(com.hp.gagawa.java.Node node,
                                String text)
        Sets the text content of the given node to a given value. Existing child nodes are removed.
        Parameters:
        node - A node to process.
        text - The new text content of the node.
      • text

        public static String text​(com.hp.gagawa.java.Node node)
        Gets the text content of the given node. Formally, the text content is the concatenation of the text contents of all nodes in the subtree of the given node.
        Parameters:
        node - A node from which to get its text content.
        Returns:
        The text content of the given node.