Class TextNode

  • All Implemented Interfaces:
    java.lang.Cloneable

    public class TextNode
    extends Node
    A text node.
    Author:
    Jonathan Hedley, jonathan@hedley.net
    • Constructor Detail

      • TextNode

        public TextNode​(java.lang.String text,
                        java.lang.String baseUri)
        Create a new TextNode representing the supplied (unencoded) text).
        Parameters:
        text - raw text
        baseUri - base uri
        See Also:
        createFromEncoded(String, String)
    • Method Detail

      • nodeName

        public java.lang.String nodeName()
        Description copied from class: Node
        Get the node name of this node. Use for debugging purposes and not logic switching (for that, use instanceof).
        Specified by:
        nodeName in class Node
        Returns:
        node name
      • text

        public java.lang.String text()
        Get the text content of this text node.
        Returns:
        Unencoded, normalised text.
        See Also:
        getWholeText()
      • text

        public TextNode text​(java.lang.String text)
        Set the text content of this text node.
        Parameters:
        text - unencoded text
        Returns:
        this, for chaining
      • getWholeText

        public java.lang.String getWholeText()
        Get the (unencoded) text of this text node, including any newlines and spaces present in the original.
        Returns:
        text
      • isBlank

        public boolean isBlank()
        Test if this text node is blank -- that is, empty or only whitespace (including newlines).
        Returns:
        true if this document is empty or only whitespace, false if it contains any text content.
      • splitText

        public TextNode splitText​(int offset)
        Split this text node into two nodes at the specified string offset. After splitting, this node will contain the original text up to the offset, and will have a new text node sibling containing the text after the offset.
        Parameters:
        offset - string offset point to split node at.
        Returns:
        the newly created text node containing the text after the offset.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class Node
      • createFromEncoded

        public static TextNode createFromEncoded​(java.lang.String encodedText,
                                                 java.lang.String baseUri)
        Create a new TextNode from HTML encoded (aka escaped) data.
        Parameters:
        encodedText - Text containing encoded HTML (e.g. <)
        baseUri - Base uri
        Returns:
        TextNode containing unencoded data (e.g. <)
      • attr

        public java.lang.String attr​(java.lang.String attributeKey)
        Description copied from class: Node
        Get an attribute's value by its key. Case insensitive

        To get an absolute URL from an attribute that may be a relative URL, prefix the key with abs, which is a shortcut to the Node.absUrl(java.lang.String) method.

        E.g.:
        String url = a.attr("abs:href");
        Overrides:
        attr in class Node
        Parameters:
        attributeKey - The attribute key.
        Returns:
        The attribute, or empty string if not present (to avoid nulls).
        See Also:
        Node.attributes(), Node.hasAttr(String), Node.absUrl(String)
      • attributes

        public Attributes attributes()
        Description copied from class: Node
        Get all of the element's attributes.
        Overrides:
        attributes in class Node
        Returns:
        attributes (which implements iterable, in same order as presented in original HTML).
      • attr

        public Node attr​(java.lang.String attributeKey,
                         java.lang.String attributeValue)
        Description copied from class: Node
        Set an attribute (key=value). If the attribute already exists, it is replaced.
        Overrides:
        attr in class Node
        Parameters:
        attributeKey - The attribute key.
        attributeValue - The attribute value.
        Returns:
        this (for chaining)
      • hasAttr

        public boolean hasAttr​(java.lang.String attributeKey)
        Description copied from class: Node
        Test if this element has an attribute. Case insensitive
        Overrides:
        hasAttr in class Node
        Parameters:
        attributeKey - The attribute key to check.
        Returns:
        true if the attribute exists, false if not.
      • removeAttr

        public Node removeAttr​(java.lang.String attributeKey)
        Description copied from class: Node
        Remove an attribute from this element.
        Overrides:
        removeAttr in class Node
        Parameters:
        attributeKey - The attribute to remove.
        Returns:
        this (for chaining)
      • absUrl

        public java.lang.String absUrl​(java.lang.String attributeKey)
        Description copied from class: Node
        Get an absolute URL from a URL attribute that may be relative (i.e. an <a href> or <img src>).

        E.g.: String absUrl = linkEl.absUrl("href");

        If the attribute value is already absolute (i.e. it starts with a protocol, like http:// or https:// etc), and it successfully parses as a URL, the attribute is returned directly. Otherwise, it is treated as a URL relative to the element's Node.baseUri, and made absolute using that.

        As an alternate, you can use the Node.attr(java.lang.String) method with the abs: prefix, e.g.: String absUrl = linkEl.attr("abs:href");

        Overrides:
        absUrl in class Node
        Parameters:
        attributeKey - The attribute key
        Returns:
        An absolute URL if one could be made, or an empty string (not null) if the attribute was missing or could not be made successfully into a URL.
        See Also:
        Node.attr(java.lang.String), URL(java.net.URL, String)