Class Node

    • Field Detail

      • nodeName

        protected final java.lang.String nodeName
      • nodeRawName

        protected final java.lang.String nodeRawName
      • ownerDocument

        protected Document ownerDocument
      • nodeValue

        protected java.lang.String nodeValue
      • attributes

        protected java.util.List<Attribute> attributes
      • parentNode

        protected Node parentNode
      • childNodes

        protected java.util.List<Node> childNodes
      • childElementNodesCount

        protected int childElementNodesCount
      • childElementNodes

        protected Element[] childElementNodes
      • siblingIndex

        protected int siblingIndex
      • siblingElementIndex

        protected int siblingElementIndex
      • siblingNameIndex

        protected int siblingNameIndex
    • Constructor Detail

      • Node

        protected Node​(Document document,
                       Node.NodeType nodeType,
                       java.lang.String nodeName)
        Creates new node.
    • Method Detail

      • cloneTo

        protected <T extends Node> T cloneTo​(T dest)
        Copies all non-final values to the empty cloned object. Cache-related values are not copied.
      • clone

        public abstract Node clone()
        Overrides:
        clone in class java.lang.Object
      • getNodeName

        public java.lang.String getNodeName()
        Returns nodes name or null if name is not available.
      • getNodeRawName

        public java.lang.String getNodeRawName()
        Returns nodes raw name - exactly as it was given in the input.
      • getNodeValue

        public java.lang.String getNodeValue()
        Returns node value or null if value is not available.
      • setNodeValue

        public void setNodeValue​(java.lang.String value)
        Sets node value.
      • getOwnerDocument

        public Document getOwnerDocument()
        Returns owner document, root node for this DOM tree.
      • detachFromParent

        public void detachFromParent()
        Removes this node from DOM tree.
      • addChild

        public void addChild​(Node node)
        Appends child node. Don't use this node in the loop, since it might be slow due to reindexChildren().
      • addChild

        public void addChild​(Node... nodes)
        Appends several child nodes at once. Reindex is done only once, after all children are added.
      • insertChild

        public void insertChild​(Node node,
                                int index)
        Inserts node at given index.
      • insertChild

        public void insertChild​(Node[] nodes,
                                int index)
        Inserts several nodes at ones. Reindex is done onl once, after all children are added.
      • insertBefore

        public void insertBefore​(Node newChild,
                                 Node refChild)
        Inserts node before provided node.
      • insertBefore

        public void insertBefore​(Node[] newChilds,
                                 Node refChild)
        Inserts several child nodes before provided node.
      • insertAfter

        public void insertAfter​(Node newChild,
                                Node refChild)
        Inserts node after provided node.
      • insertAfter

        public void insertAfter​(Node[] newChilds,
                                Node refChild)
        Inserts several child nodes after referent node.
      • removeChild

        public Node removeChild​(int index)
        Removes child node at given index. Returns removed node or null if index is invalid.
      • removeChild

        public void removeChild​(Node childNode)
        Removes child node. It works only with direct children, i.e. if provided child node is not a child nothing happens.
      • removeAllChilds

        public void removeAllChilds()
        Removes all child nodes. Each child node will be detached from this parent.
      • getParentNode

        public Node getParentNode()
        Returns parent node or null if no parent exist.
      • hasAttributes

        public boolean hasAttributes()
        Returns true if node has attributes.
      • getAttributesCount

        public int getAttributesCount()
        Returns total number of attributes.
      • getAttribute

        public Attribute getAttribute​(int index)
        Returns attribute at given index or null if index not found.
      • hasAttribute

        public boolean hasAttribute​(java.lang.String name)
        Returns true if node contains an attribute.
      • getAttribute

        public java.lang.String getAttribute​(java.lang.String name)
        Returns attribute value. Returns null when attribute doesn't exist or when attribute exist but doesn't specify a value.
      • getAttributeInstance

        protected Attribute getAttributeInstance​(java.lang.String name)
      • indexOfAttributeInstance

        protected int indexOfAttributeInstance​(java.lang.String name)
      • removeAttribute

        public boolean removeAttribute​(java.lang.String name)
      • setAttribute

        public void setAttribute​(java.lang.String name,
                                 java.lang.String value)
        Sets attribute value. Value may be null.
      • setAttribute

        public void setAttribute​(java.lang.String name)
        Sets attribute that doesn't need a value.
      • isAttributeContaining

        public boolean isAttributeContaining​(java.lang.String name,
                                             java.lang.String word)
        Returns true if attribute containing some word.
      • hasChildNodes

        public boolean hasChildNodes()
        Returns true if node has child nodes.
      • getChildNodesCount

        public int getChildNodesCount()
        Returns number of all child nodes.
      • getChildElementsCount

        public int getChildElementsCount()
        Returns number of child elements.
      • getChildElementsCount

        public int getChildElementsCount​(java.lang.String elementName)
        Returns number of child elements with given name.
      • getChildNodes

        public Node[] getChildNodes()
        Returns an array of all children nodes. Returns an empty array if there are no children.
      • findChildNodeWithName

        public Node findChildNodeWithName​(java.lang.String name)
        Finds the first child node with given node name.
      • filterChildNodes

        public Node[] filterChildNodes​(java.util.function.Predicate<Node> nodePredicate)
        Filters child nodes.
      • getChildElements

        public Element[] getChildElements()
        Returns an array of all children elements.
      • getChild

        public Node getChild​(int index)
        Returns a child node at given index or null if child doesn't exist for that index.
      • getChild

        public Node getChild​(int... indexes)
        Returns a child node with given hierarchy. Just a shortcut for successive calls of getChild(int).
      • getChildElement

        public Element getChildElement​(int index)
        Returns a child element node at given index. If index is out of bounds, null is returned.
      • getFirstChild

        public Node getFirstChild()
        Returns first child or null if no children exist.
      • getFirstChildElement

        public Element getFirstChildElement()
        Returns first child element node or null if no element children exist.
      • getFirstChildElement

        public Element getFirstChildElement​(java.lang.String elementName)
        Returns first child element with given name or null if no such children exist.
      • getLastChild

        public Node getLastChild()
        Returns last child or null if no children exist.
      • getLastChildElement

        public Element getLastChildElement()
        Returns last child element node or null if no such child node exist.
      • getLastChildElement

        public Element getLastChildElement​(java.lang.String elementName)
        Returns last child element with given name or null if no such child node exist.
      • check

        public boolean check()
        Checks the health of child nodes. Useful during complex tree manipulation, to check if everything is OK. Not optimized for speed, should be used just for testing purposes.
      • reindexChildren

        protected void reindexChildren()
        Reindex children nodes. Must be called on every children addition/removal. Iterates childNodes list and:
        • calculates three different sibling indexes,
        • calculates total child element node count,
        • resets child element nodes array (will be init lazy later by @{#initChildElementNodes}.
      • reindexChildrenOnAdd

        protected void reindexChildrenOnAdd​(int addedCount)
        Optimized variant of reindexChildren() for addition. Only added children are optimized.
      • initChildElementNodes

        protected void initChildElementNodes()
        Initializes list of child elements.
      • initSiblingNames

        protected void initSiblingNames()
        Initializes siblings elements of the same name.
      • initAttributes

        protected void initAttributes()
        Initializes attributes when needed.
      • initChildNodes

        protected void initChildNodes​(Node newNode)
        Initializes child nodes list when needed. Also fix owner document for new node, if needed.
      • changeOwnerDocument

        protected void changeOwnerDocument​(Node node,
                                           Document ownerDocument)
        Changes owner document for given node and all its children.
      • getSiblingIndex

        public int getSiblingIndex()
        Get the list index of this node in its node sibling list. For example, if this is the first node sibling, returns 0. Index address all nodes, i.e. of all node types.
      • getSiblingElementIndex

        public int getSiblingElementIndex()
      • getSiblingNameIndex

        public int getSiblingNameIndex()
      • getNextSibling

        public Node getNextSibling()
        Returns this node's next sibling of any type or null if this is the last sibling.
      • getNextSiblingElement

        public Node getNextSiblingElement()
        Returns this node's next element.
      • getNextSiblingName

        public Node getNextSiblingName()
        Returns this node's next element with the same name.
      • getPreviousSibling

        public Node getPreviousSibling()
        Returns this node's previous sibling of any type or null if this is the first sibling.
      • getPreviousSiblingElement

        public Node getPreviousSiblingElement()
        Returns this node's previous sibling of element type or null if this is the first sibling.
      • getPreviousSiblingName

        public Node getPreviousSiblingName()
        Returns this node's previous sibling element with the same name.
      • getTextContent

        public java.lang.String getTextContent()
        Returns the text content of this node and its descendants.
        See Also:
        appendTextContent(Appendable)
      • appendTextContent

        public void appendTextContent​(java.lang.Appendable appendable)
        Appends the text content to an Appendable (StringBuilder, CharBuffer...). This way we can reuse the Appendable instance during the creation of text content and have better performances.
      • getHtml

        public java.lang.String getHtml()
        Generates HTML.
      • getInnerHtml

        public java.lang.String getInnerHtml()
        Generates inner HTML.
      • visit

        public void visit​(NodeVisitor nodeVisitor)
        Visits the DOM tree.
      • visitChildren

        public void visitChildren​(NodeVisitor nodeVisitor)
        Visits children nodes.
      • visitNode

        protected abstract void visitNode​(NodeVisitor nodeVisitor)
        Visits single node. Implementations just needs to call the correct visitor callback function.
      • getCssPath

        public java.lang.String getCssPath()
        Returns CSS path to this node from document root.